Completed
Push — 5.0 ( a48099...63af02 )
by
unknown
11:33
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 Kunstmaan\AdminBundle\Entity\Role;
7
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
8
use Symfony\Component\Console\Input\InputArgument;
9
10
use Symfony\Component\Console\Input\InputInterface;
11
12
use Symfony\Component\Console\Output\OutputInterface;
13
14
/**
15
 * Symfony CLI command to create a group using bin/console kuma:role:create <NAME_OF_THE_ROLE>
16
 */
17
class CreateRoleCommand extends ContainerAwareCommand
18
{
19
    /**
20
     * Configures the current command.
21
     */
22
    protected function configure()
23
    {
24
        $this->setName('kuma:role:create')
25
            ->setDescription('Create a role.')
26
            ->setDefinition(array(
27
                new InputArgument('role', InputArgument::REQUIRED, 'The role'),
28
            ))
29
            ->setHelp(<<<EOT
30
The <info>kuma:role:create</info> command creates a role:
31
32
  <info>php bin/console kuma:role:create ROLE_ADMIN</info>
33
34
<comment>Note:</comment> The ROLE_ prefix will be added if you don't provide it
35
36
  <info>php bin/console kuma:role:create ADMIN</info>
37
38
will create ROLE_ADMIN.
39
40
EOT
41
            );
42
    }
43
44
    /**
45
     * Executes the current command.
46
     *
47
     * @param InputInterface  $input  The input
48
     * @param OutputInterface $output The output
49
     *
50
     * @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...
51
     */
52
    protected function execute(InputInterface $input, OutputInterface $output)
53
    {
54
        /* @var EntityManager $em */
55
        $em = $this->getContainer()->get('doctrine.orm.entity_manager');
56
57
        $roleName = strtoupper($input->getArgument('role'));
58
        if ('ROLE_' != substr($roleName, 0, 5)) {
59
            $roleName = 'ROLE_' . $roleName;
60
        }
61
62
        $role = new Role($roleName);
63
        $em->persist($role);
64
        $em->flush();
65
66
        $output->writeln(sprintf('Created role <comment>%s</comment>', $roleName));
67
    }
68
}
69