UserCreateCommand   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 58
rs 10
c 0
b 0
f 0
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 7 1
A __construct() 0 10 1
A createUser() 0 10 1
A configure() 0 8 1
1
<?php
2
3
namespace PiedWeb\CMSBundle\Command;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
use Symfony\Component\Console\Command\Command;
7
use Symfony\Component\Console\Input\InputArgument;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
11
12
class UserCreateCommand extends Command
13
{
14
    /**
15
     * @var EntityManagerInterface
16
     */
17
    private $em;
18
19
    /**
20
     * @var string
21
     */
22
    private $userClass;
23
24
    /**
25
     * @var UserPasswordEncoderInterface
26
     */
27
    private $passwordEncoder;
28
29
    public function __construct(
30
        EntityManagerInterface $em,
31
        UserPasswordEncoderInterface $passwordEncoder,
32
        $userClass
33
    ) {
34
        $this->em = $em;
35
        $this->passwordEncoder = $passwordEncoder;
36
        $this->userClass = $userClass;
37
38
        parent::__construct();
39
    }
40
41
    protected function configure()
42
    {
43
        $this
44
            ->setName('piedweb:user:create')
45
            ->setDescription('Create a new user')
46
            ->addArgument('email', InputArgument::REQUIRED)
47
            ->addArgument('password', InputArgument::REQUIRED)
48
            ->addArgument('role', InputArgument::REQUIRED);
49
    }
50
51
    protected function createUser($email, $password, $role)
52
    {
53
        $userClass = $this->userClass;
54
        $user = new $userClass();
55
        $user->setEmail($email);
56
        $user->setPassword($this->passwordEncoder->encodePassword($user, $password));
57
        $user->setRoles([$role]);
58
59
        $this->em->persist($user);
60
        $this->em->flush();
61
    }
62
63
    protected function execute(InputInterface $input, OutputInterface $output)
64
    {
65
        $this->createUser($input->getArgument('email'), $input->getArgument('password'), $input->getArgument('role'));
66
67
        $output->writeln('<info>User `'.$input->getArgument('email').'` created with success.</info>');
0 ignored issues
show
Bug introduced by
Are you sure $input->getArgument('email') 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

67
        $output->writeln('<info>User `'./** @scrutinizer ignore-type */ $input->getArgument('email').'` created with success.</info>');
Loading history...
68
69
        return 0;
70
    }
71
}
72