Test Failed
Push — master ( 063ee2...142df8 )
by Jafar
02:45
created

PasswordEncryptorCommand   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 33
c 1
b 0
f 0
dl 0
loc 58
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A execute() 0 31 5
A configure() 0 6 1
1
<?php
2
3
namespace Jafar\Bundle\GuardedAuthenticationBundle\Command;
4
5
use Symfony\Component\Console\Style\SymfonyStyle;
6
use Symfony\Component\Console\Command\Command;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Output\OutputInterface;
9
use Symfony\Component\Console\Question\Question;
10
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
11
use \Exception;
12
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 $encoder */
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
    protected function execute(InputInterface $input, OutputInterface $output)
47
    {
48
        $io = new SymfonyStyle($input, $output);
49
        $io->title('Thank you for using Jafar:GuardedAuthenticationBundle');
50
        $question = new Question('Please enter password to be encrypted ? ');
51
        $question->setHiddenFallback(false);
52
        $question->setValidator(function ($answer) {
53
            if (strlen($answer) < 6) {
54
                throw new RuntimeException('The password can not be less than 6 characters');
0 ignored issues
show
Bug introduced by
The type Jafar\Bundle\GuardedAuth...ommand\RuntimeException was not found. Did you mean RuntimeException? If so, make sure to prefix the type with \.
Loading history...
55
            } elseif (strlen($answer) > 50) {
56
                throw new RuntimeException('The password can not be more than 50 characters');
57
            }
58
            return $answer;
59
        });
60
        $helper        = $this->getHelper('question');
61
        $plainPassword = $helper->ask($input, $output, $question);
62
        $user = null;
63
        if(class_exists('\App\Entity\User')){
64
            $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...
65
        } elseif(class_exists('\App\Entity\Users')){
66
            $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...
67
        }else{
68
            throw new Exception('No User Entity found, searched in \'App\Entity\User\' and \'App\Entity\Users\'');
69
        }
70
        $enc = $this->encoder->encodePassword(
71
            $user,
72
            $plainPassword
73
        );
74
        $output->writeln('<info>'.$enc.'</info>');
75
76
        return 0;
77
    }
78
}
79