Completed
Push — master ( aba493...5356ed )
by Ruud
315:38 queued 305:00
created

CreateGroupCommand::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 0
cts 8
cp 0
rs 9.7998
c 0
b 0
f 0
cc 3
nc 2
nop 1
crap 12
1
<?php
2
3
namespace Kunstmaan\AdminBundle\Command;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
use Kunstmaan\AdminBundle\Entity\Group;
7
use Kunstmaan\AdminBundle\Entity\Role;
8
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
9
use Symfony\Component\Console\Input\InputArgument;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Input\InputOption;
12
use Symfony\Component\Console\Output\OutputInterface;
13
14
/**
15
 * Symfony CLI command to create a group using bin/console kuma:group:create <name_of_the_group>
16
 *
17
 * @final since 5.1
18
 * NEXT_MAJOR extend from `Command` and remove `$this->getContainer` usages
19
 */
20
class CreateGroupCommand extends ContainerAwareCommand
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Bundle\Framework...d\ContainerAwareCommand has been deprecated with message: since Symfony 4.2, use {@see Command} instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
21
{
22
    /**
23
     * @var EntityManagerInterface
24
     */
25
    private $em;
26
27
    /**
28
     * @param EntityManagerInterface|null $em
29
     */
30
    public function __construct(/* EntityManagerInterface */ $em = null)
31
    {
32
        parent::__construct();
33
34
        if (!$em instanceof EntityManagerInterface) {
35
            @trigger_error(sprintf('Passing a command name as the first argument of "%s" is deprecated since version symfony 3.4 and will be removed in symfony 4.0. If the command was registered by convention, make it a service instead. ', __METHOD__), E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
36
37
            $this->setName(null === $em ? 'kuma:group:create' : $em);
38
39
            return;
40
        }
41
42
        $this->em = $em;
43
    }
44
45
    /**
46
     * Configures the current command
47
     */
48
    protected function configure()
49
    {
50
        parent::configure();
51
52
        $this->setName('kuma:group:create')
53
            ->setDescription('Create a user group.')
54
            ->setDefinition(array(
55
                new InputArgument('group', InputArgument::REQUIRED, 'The group'),
56
                new InputOption('role', null, InputOption::VALUE_OPTIONAL, 'Role(s) (comma separated list if you want to specifiy multiple roles)'),
57
            ))
58
            ->setHelp(<<<'EOT'
59
The <info>kuma:group:create</info> command creates a group:
60
61
  <info>php bin/console kuma:group:create Administrators</info>
62
63
You can specify a list of roles to attach to this group by specifying the
64
optional --roles parameter, providing a comma separated list of roles :
65
66
  <info>php bin/console kuma:group:create --role=admin,guest Administrators</info>
67
68
<comment>Note:</comment> The ROLE_ prefix will be added if you don't provide it AND you must make
69
sure the roles already exist!
70
71
EOT
72
            );
73
    }
74
75
    /**
76
     * Executes the current command
77
     *
78
     * @param InputInterface  $input  The input
79
     * @param OutputInterface $output The output
80
     *
81
     * @return int
0 ignored issues
show
Documentation introduced by
Should the return type not be integer|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
82
     */
83
    protected function execute(InputInterface $input, OutputInterface $output)
84
    {
85
        if (null === $this->em) {
86
            $this->em = $this->getContainer()->get('doctrine.orm.entity_manager');
87
        }
88
89
        $groupName = $input->getArgument('group');
90
        $roleNames = $input->getOption('role');
91
        $group = new Group($groupName);
92
93
        if (!empty($roleNames)) {
94
            // Roles were provided, so attach them to the group
95
            $roleNames = explode(',', strtoupper($roleNames));
96
            foreach ($roleNames as $roleName) {
97
                if ('ROLE_' != substr($roleName, 0, 5)) {
98
                    $roleName = 'ROLE_' . $roleName;
99
                }
100
                /* @var Role $role */
101
                $role = $this->em->getRepository('KunstmaanAdminBundle:Role')->findOneBy(array('role' => $roleName));
102
                $group->addRole($role);
103
            }
104
        }
105
        $this->em->persist($group);
106
        $this->em->flush();
107
108
        $output->writeln(sprintf('Created group <comment>%s</comment>', $groupName));
109
    }
110
}
111