Completed
Pull Request — 5.0 (#2104)
by Kevin
13:45 queued 04:21
created

AdminBundle/Command/CreateGroupCommand.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\Group;
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\Input\InputOption;
14
use Symfony\Component\Console\Output\OutputInterface;
15
16
/**
17
 * Symfony CLI command to create a group using bin/console kuma:group:create <name_of_the_group>
18
 */
19
class CreateGroupCommand extends ContainerAwareCommand
20
{
21
    /**
22
     * Configures the current command
23
     */
24
    protected function configure()
25
    {
26
        parent::configure();
27
28
        $this->setName('kuma:group:create')
29
            ->setDescription('Create a user group.')
30
            ->setDefinition(array(
31
                new InputArgument('group', InputArgument::REQUIRED, 'The group'),
32
                new InputOption('role', null, InputOption::VALUE_OPTIONAL, 'Role(s) (comma separated list if you want to specifiy multiple roles)'),
33
            ))
34
            ->setHelp(<<<EOT
35
The <info>kuma:group:create</info> command creates a group:
36
37
  <info>php bin/console kuma:group:create Administrators</info>
38
39
You can specify a list of roles to attach to this group by specifying the
40
optional --roles parameter, providing a comma separated list of roles :
41
42
  <info>php bin/console kuma:group:create --role=admin,guest Administrators</info>
43
44
<comment>Note:</comment> The ROLE_ prefix will be added if you don't provide it AND you must make
45
sure the roles already exist!
46
47
EOT
48
            );
49
    }
50
51
    /**
52
     * Executes the current command
53
     *
54
     * @param InputInterface  $input  The input
55
     * @param OutputInterface $output The output
56
     *
57
     * @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...
58
     */
59
    protected function execute(InputInterface $input, OutputInterface $output)
60
    {
61
        /* @var EntityManager $em */
62
        $em = $this->getContainer()->get('doctrine.orm.entity_manager');
63
        $groupName = $input->getArgument('group');
64
        $roleNames = $input->getOption('role');
65
        $group = new Group($groupName);
66
67
        if (!empty($roleNames)) {
68
            // Roles were provided, so attach them to the group
69
            $roleNames = explode(',', strtoupper($roleNames));
70
            foreach ($roleNames as $roleName) {
71
                if ('ROLE_' != substr($roleName, 0, 5)) {
72
                    $roleName = 'ROLE_' . $roleName;
73
                }
74
                /* @var Role $role */
75
                $role = $em->getRepository('KunstmaanAdminBundle:Role')->findOneBy(array('role' => $roleName));
76
                $group->addRole($role);
77
            }
78
        }
79
        $em->persist($group);
80
        $em->flush();
81
82
        $output->writeln(sprintf('Created group <comment>%s</comment>', $groupName));
83
    }
84
}
85