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

AdminBundle/Command/CreateRoleCommand.php (1 issue)

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\Role;
8
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
9
use Symfony\Component\Console\Input\InputArgument;
10
11
use Symfony\Component\Console\Input\InputInterface;
12
13
use Symfony\Component\Console\Output\OutputInterface;
14
15
/**
16
 * Symfony CLI command to create a group using bin/console kuma:role:create <NAME_OF_THE_ROLE>
17
 *
18
 * @final since 5.1
19
 * NEXT_MAJOR extend from `Command` and remove `$this->getContainer` usages
20
 */
21
class CreateRoleCommand extends ContainerAwareCommand
22
{
23
    /**
24
     * @var EntityManagerInterface
25
     */
26
    private $em;
27
28
    /**
29
     * @param EntityManagerInterface|null $em
30
     */
31
    public function __construct(/* EntityManagerInterface */ $em = null)
32
    {
33
        parent::__construct();
34
35
        if (!$em instanceof EntityManagerInterface) {
36
            @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);
37
38
            $this->setName(null === $em ? 'kuma:role:create' : $em);
39
40
            return;
41
        }
42
43
        $this->em = $em;
44
    }
45
46
    /**
47
     * Configures the current command.
48
     */
49
    protected function configure()
50
    {
51
        $this->setName('kuma:role:create')
52
            ->setDescription('Create a role.')
53
            ->setDefinition(array(
54
                new InputArgument('role', InputArgument::REQUIRED, 'The role'),
55
            ))
56
            ->setHelp(<<<EOT
57
The <info>kuma:role:create</info> command creates a role:
58
59
  <info>php bin/console kuma:role:create ROLE_ADMIN</info>
60
61
<comment>Note:</comment> The ROLE_ prefix will be added if you don't provide it
62
63
  <info>php bin/console kuma:role:create ADMIN</info>
64
65
will create ROLE_ADMIN.
66
67
EOT
68
            );
69
    }
70
71
    /**
72
     * Executes the current command.
73
     *
74
     * @param InputInterface  $input  The input
75
     * @param OutputInterface $output The output
76
     *
77
     * @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...
78
     */
79
    protected function execute(InputInterface $input, OutputInterface $output)
80
    {
81
        if (null === $this->em) {
82
            $this->em = $this->getContainer()->get('doctrine.orm.entity_manager');
83
        }
84
85
        $roleName = strtoupper($input->getArgument('role'));
86
        if ('ROLE_' != substr($roleName, 0, 5)) {
87
            $roleName = 'ROLE_' . $roleName;
88
        }
89
90
        $role = new Role($roleName);
91
        $this->em->persist($role);
92
        $this->em->flush();
93
94
        $output->writeln(sprintf('Created role <comment>%s</comment>', $roleName));
95
    }
96
}
97