CreateAdminCommand::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 3
c 1
b 0
f 1
nc 1
nop 3
dl 0
loc 5
rs 10
1
<?php
2
3
namespace PiouPiou\RibsAdminBundle\Command;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
use PiouPiou\RibsAdminBundle\Entity\Account;
7
use PiouPiou\RibsAdminBundle\Entity\User;
8
use Symfony\Component\Console\Command\Command;
9
use Symfony\Component\Console\Input\InputArgument;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Output\OutputInterface;
12
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
13
14
class CreateAdminCommand extends Command
15
{
16
    /** @var EntityManagerInterface */
17
    private $em;
18
19
    /** @var UserPasswordEncoderInterface */
20
    private $passwordEncoder;
21
22
    /**
23
     * CreateAdminCommand constructor.
24
     * @param EntityManagerInterface $em
25
     * @param UserPasswordEncoderInterface $passwordEncoder
26
     * @param string|null $name
27
     */
28
    public function __construct(EntityManagerInterface $em, UserPasswordEncoderInterface $passwordEncoder, string $name = null)
29
    {
30
        parent::__construct($name);
31
        $this->em = $em;
32
        $this->passwordEncoder = $passwordEncoder;
33
    }
34
35
    protected function configure()
36
    {
37
        $this
38
            ->setName('ribsadmin:create-admin')
39
            ->setDescription('Create an admin in ribs admin')
40
            ->addArgument(
41
                'firstname',
42
                InputArgument::REQUIRED,
43
                'Firstname of admin to create'
44
            )
45
            ->addArgument(
46
                'lastname',
47
                InputArgument::REQUIRED,
48
                'Lastname of admin to create'
49
            )
50
            ->addArgument(
51
                'email',
52
                InputArgument::REQUIRED,
53
                'email of admin to create'
54
            )
55
            ->addArgument(
56
                'password',
57
                InputArgument::REQUIRED,
58
                'password of admin to create'
59
            )
60
        ;
61
    }
62
63
    protected function execute(InputInterface $input, OutputInterface $output)
64
    {
65
        $firstname = $input->getArgument('firstname');
66
        $lastname = $input->getArgument('lastname');
67
        $email = $input->getArgument('email');
68
        $password = $input->getArgument('password');
69
70
        $output->writeln("Create admin " . $firstname . " " . $lastname);
0 ignored issues
show
Bug introduced by
Are you sure $firstname of type null|string|string[] can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

70
        $output->writeln("Create admin " . /** @scrutinizer ignore-type */ $firstname . " " . $lastname);
Loading history...
Bug introduced by
Are you sure $lastname of type null|string|string[] can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

70
        $output->writeln("Create admin " . $firstname . " " . /** @scrutinizer ignore-type */ $lastname);
Loading history...
71
72
        $user = new User();
73
        $user->setFirstname($firstname);
0 ignored issues
show
Bug introduced by
It seems like $firstname can also be of type string[]; however, parameter $firstname of PiouPiou\RibsAdminBundle...ty\User::setFirstname() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

73
        $user->setFirstname(/** @scrutinizer ignore-type */ $firstname);
Loading history...
74
        $user->setLastname($lastname);
0 ignored issues
show
Bug introduced by
It seems like $lastname can also be of type string[]; however, parameter $lastname of PiouPiou\RibsAdminBundle...ity\User::setLastname() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

74
        $user->setLastname(/** @scrutinizer ignore-type */ $lastname);
Loading history...
75
        $user->setAccessRights("*");
76
        $this->em->persist($user);
77
78
        $account = new Account();
79
        $temp_password = $this->passwordEncoder->encodePassword($account, $password);
0 ignored issues
show
Bug introduced by
It seems like $password can also be of type null and string[]; however, parameter $plainPassword of Symfony\Component\Securi...rface::encodePassword() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

79
        $temp_password = $this->passwordEncoder->encodePassword($account, /** @scrutinizer ignore-type */ $password);
Loading history...
80
        $account->setPassword($temp_password);
81
        $account->setUser($user);
82
        $account->setEmail($email);
83
        $account->setUsername(substr($firstname, 0, 1).".".$lastname);
0 ignored issues
show
Bug introduced by
It seems like $firstname can also be of type string[]; however, parameter $string of substr() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

83
        $account->setUsername(substr(/** @scrutinizer ignore-type */ $firstname, 0, 1).".".$lastname);
Loading history...
84
        $this->em->persist($account);
85
86
        $this->em->flush();
87
88
        $output->writeln("End Create admin " . $firstname . " " . $lastname);
89
90
        return 0;
91
    }
92
}
93