<?php 

namespace NPS\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Doctrine\ORM\EntityManagerInterface;
use NPS\Etc\Strings;
use NPS\Factory\UserFactory;
use NPS\Repository\UserRepository;

/**
 * Resets user's password
 * @author Lukas Korinek
 */
class ResetPasswordCommand extends Command
{

    /** @var UserRepository $repo  */
    private $repo;

    /** @var EntityManagerInterface $em */
    private $em;

    public function __construct(EntityManagerInterface $em, UserRepository $repo)
    {
        $this->repo = $repo;
        $this->em = $em;

        parent::__construct();
    }

    /**
     * @inheritdoc
     */
    protected function configure()
    {
        $this 
            ->setName('user:reset')
            ->setDescription('Resets user\'s password')
            ->addArgument('email', InputArgument::REQUIRED)
            ->addArgument('pass', InputArgument::REQUIRED);
    }   

    /**
     * @inheritdoc
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $email = trim($input->getArgument('email'));
        if(!$user = $this->repo->getByEmail($email))
        {
            $output->writeln('User not found');
            return 1;
        }

        // Update the password
        $pass = trim($input->getArgument('pass'));
        $user->updatePassword($pass);

        $this->em->flush();
        return 0;

    }
}


