PasswordEncryptorCommand::execute()   A
last analyzed

Complexity

Conditions 5
Paths 3

Size

Total Lines 32
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 5
eloc 24
c 3
b 0
f 0
nc 3
nop 2
dl 0
loc 32
rs 9.2248
1
<?php
2
3
namespace Jafar\Bundle\GuardedAuthenticationBundle\Command;
4
5
use Exception;
6
use RuntimeException;
7
use Symfony\Component\Console\Command\Command;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use Symfony\Component\Console\Question\Question;
11
use Symfony\Component\Console\Style\SymfonyStyle;
12
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
13
14
/**
15
 * Class PasswordEncryptorCommand.
16
 *
17
 * @author Jafar Jabr <[email protected]>
18
 */
19
class PasswordEncryptorCommand extends Command
20
{
21
    protected static $defaultName = 'jafar:encrypt-password';
22
23
    /** @var UserPasswordEncoderInterface */
24
    private $encoder;
25
26
    public function __construct(UserPasswordEncoderInterface $encoder)
27
    {
28
        $this->encoder = $encoder;
29
        parent::__construct();
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    protected function configure()
36
    {
37
        $this
38
            ->setName('jafar:encrypt-password')
39
            ->setDescription('encrypt a password for first use before to have the regisration')
40
            ->setHelp('encrypt a password for first use before to have the regisration');
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     *
46
     * @throws Exception
47
     */
48
    protected function execute(InputInterface $input, OutputInterface $output)
49
    {
50
        $io = new SymfonyStyle($input, $output);
51
        $io->title('Thank you for using Jafar:GuardedAuthenticationBundle');
52
        $question = new Question('Please enter password to be encrypted ? ');
53
        $question->setHiddenFallback(false);
54
        $question->setValidator(function ($answer) {
55
            if (strlen($answer) < 6) {
56
                throw new RuntimeException('The password can not be less than 6 characters');
57
            } elseif (strlen($answer) > 50) {
58
                throw new RuntimeException('The password can not be more than 50 characters');
59
            }
60
61
            return $answer;
62
        });
63
        $helper        = $this->getHelper('question');
64
        $plainPassword = $helper->ask($input, $output, $question);
65
        $user          = null;
66
        if (class_exists('\App\Entity\User')) {
67
            $user = new \App\Entity\User();
0 ignored issues
show
Bug introduced by
The type App\Entity\User was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
68
        } elseif (class_exists('\App\Entity\Users')) {
69
            $user = new \App\Entity\Users();
0 ignored issues
show
Bug introduced by
The type App\Entity\Users was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
70
        } else {
71
            throw new Exception('No User Entity found, searched in \'App\Entity\User\' and \'App\Entity\Users\'');
72
        }
73
        $enc = $this->encoder->encodePassword(
74
            $user,
75
            $plainPassword
76
        );
77
        $output->writeln('<info>'.$enc.'</info>');
78
79
        return 1;
80
    }
81
}
82