Completed
Push — master ( 91fdab...75a7b9 )
by
unknown
13:37
created

AdminBundle/Command/CreateGroupCommand.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\AdminBundle\Command;
4
5
use Doctrine\ORM\EntityManager;
6
use Doctrine\ORM\EntityManagerInterface;
7
use Kunstmaan\AdminBundle\Entity\Group;
8
use Kunstmaan\AdminBundle\Entity\Role;
9
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
10
use Symfony\Component\Console\Input\InputArgument;
11
12
use Symfony\Component\Console\Input\InputInterface;
13
14
use Symfony\Component\Console\Input\InputOption;
15
use Symfony\Component\Console\Output\OutputInterface;
16
17
/**
18
 * Symfony CLI command to create a group using bin/console kuma:group:create <name_of_the_group>
19
 *
20
 * @final since 5.1
21
 * NEXT_MAJOR extend from `Command` and remove `$this->getContainer` usages
22
 */
23
class CreateGroupCommand extends ContainerAwareCommand
24
{
25
    /**
26
     * @var EntityManagerInterface
27
     */
28
    private $em;
29
30
    /**
31
     * @param EntityManagerInterface|null $em
32
     */
33
    public function __construct(/* EntityManagerInterface */ $em = null)
34
    {
35
        parent::__construct();
36
37
        if (!$em instanceof EntityManagerInterface) {
38
            @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);
39
40
            $this->setName(null === $em ? 'kuma:group:create' : $em);
41
42
            return;
43
        }
44
45
        $this->em = $em;
46
    }
47
48
49
    /**
50
     * Configures the current command
51
     */
52
    protected function configure()
53
    {
54
        parent::configure();
55
56
        $this->setName('kuma:group:create')
57
            ->setDescription('Create a user group.')
58
            ->setDefinition(array(
59
                new InputArgument('group', InputArgument::REQUIRED, 'The group'),
60
                new InputOption('role', null, InputOption::VALUE_OPTIONAL, 'Role(s) (comma separated list if you want to specifiy multiple roles)'),
61
            ))
62
            ->setHelp(<<<EOT
63
The <info>kuma:group:create</info> command creates a group:
64
65
  <info>php bin/console kuma:group:create Administrators</info>
66
67
You can specify a list of roles to attach to this group by specifying the
68
optional --roles parameter, providing a comma separated list of roles :
69
70
  <info>php bin/console kuma:group:create --role=admin,guest Administrators</info>
71
72
<comment>Note:</comment> The ROLE_ prefix will be added if you don't provide it AND you must make
73
sure the roles already exist!
74
75
EOT
76
            );
77
    }
78
79
    /**
80
     * Executes the current command
81
     *
82
     * @param InputInterface  $input  The input
83
     * @param OutputInterface $output The output
84
     *
85
     * @return int
0 ignored issues
show
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...
86
     */
87
    protected function execute(InputInterface $input, OutputInterface $output)
88
    {
89
        if (null === $this->em) {
90
            $this->em = $this->getContainer()->get('doctrine.orm.entity_manager');
91
        }
92
93
        $groupName = $input->getArgument('group');
94
        $roleNames = $input->getOption('role');
95
        $group = new Group($groupName);
0 ignored issues
show
It seems like $groupName defined by $input->getArgument('group') on line 93 can also be of type array<integer,string> or null; however, Kunstmaan\AdminBundle\Entity\Group::__construct() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
96
97
        if (!empty($roleNames)) {
98
            // Roles were provided, so attach them to the group
99
            $roleNames = explode(',', strtoupper($roleNames));
100
            foreach ($roleNames as $roleName) {
101
                if ('ROLE_' != substr($roleName, 0, 5)) {
102
                    $roleName = 'ROLE_' . $roleName;
103
                }
104
                /* @var Role $role */
105
                $role = $this->em->getRepository('KunstmaanAdminBundle:Role')->findOneBy(array('role' => $roleName));
106
                $group->addRole($role);
107
            }
108
        }
109
        $this->em->persist($group);
110
        $this->em->flush();
111
112
        $output->writeln(sprintf('Created group <comment>%s</comment>', $groupName));
113
    }
114
}
115