Passed
Push — master ( 8232a2...33e29c )
by Dev
13:28
created

UserCommand::execute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
c 1
b 0
f 1
nc 1
nop 2
dl 0
loc 5
rs 10
1
<?php
2
3
namespace PiedWeb\CMSBundle\Command;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
use Liip\ImagineBundle\Imagine\Cache\CacheManager;
7
use Liip\ImagineBundle\Imagine\Data\DataManager;
8
use Liip\ImagineBundle\Imagine\Filter\FilterManager;
9
use PiedWeb\CMSBundle\EventListener\MediaCacheGeneratorTrait;
10
use Symfony\Component\Console\Command\Command;
11
use Symfony\Component\Console\Helper\ProgressBar;
12
use Symfony\Component\Console\Input\InputArgument;
13
use Symfony\Component\Console\Input\InputInterface;
14
use Symfony\Component\Console\Output\OutputInterface;
15
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
16
use Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface;
17
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoder;
18
19
class UserCommand extends Command
20
{
21
    use MediaCacheGeneratorTrait;
22
23
    /**
24
     * @var EntityManagerInterface
25
     */
26
    private $em;
27
    private $userClass;
28
29
    public function __construct(
30
        EntityManagerInterface $em,
31
        UserPasswordEncoder $passwordEncoder,
32
        $userClass
33
    ) {
34
        $this->em = $em;
35
        $this->passwordEncoder = $passwordEncoder;
0 ignored issues
show
Bug Best Practice introduced by
The property passwordEncoder does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
36
        $this->userClass = $userClass;
37
38
        parent::__construct();
39
    }
40
41
    protected function configure()
42
    {
43
        $this
44
            ->setName('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 getMedias(InputInterface $input)
52
    {
53
        $repo = $this->em->getRepository($this->params->get('pwc.entity_media'));
0 ignored issues
show
Bug Best Practice introduced by
The property params does not exist on PiedWeb\CMSBundle\Command\UserCommand. Did you maybe forget to declare it?
Loading history...
54
55
        if ($input->getArgument('media')) {
56
            return $repo->findBy(['media' => $input->getArgument('media')]);
57
        }
58
59
        return $repo->findAll();
60
    }
61
62
    protected function createUser($email, $password, $role) {
0 ignored issues
show
Unused Code introduced by
The parameter $password is not used and could be removed. ( Ignorable by Annotation )

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

62
    protected function createUser($email, /** @scrutinizer ignore-unused */ $password, $role) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
63
        $userClass = $this->userClass;
64
        $user = new $userClass();
65
        $user->setEmail($email);
66
        $user->setPassword($this->passwordEncoder->encodePassword($user, $user->getPlainPassword()));
67
        $user->setRoles([$role]);
68
69
        $this->em->persist($user);
70
        $this->em->flush();
71
    }
72
73
    protected function execute(InputInterface $input, OutputInterface $output)
74
    {
75
        $this->createUser($input->getArgument('email'), $input->getArgument('password'), $input->getArgument('role'));
76
77
        $output->writeln('<info>done...</info>');
78
    }
79
}
80