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\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
|
|||
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); |
||
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 |
||
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 | return 0; |
||
111 | } |
||
112 | } |
||
113 |
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.