GenerateGroupCommand::execute()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 23
rs 9.0856
cc 2
eloc 16
nc 2
nop 2
1
<?php
2
3
	namespace Vivait\AuthBundle\Command;
4
5
	use Vivait\AuthBundle\Entity\Group;
6
	use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
7
	use Symfony\Component\Console\Input\InputArgument;
8
	use Symfony\Component\Console\Input\InputInterface;
9
	use Symfony\Component\Console\Output\OutputInterface;
10
11
	class GenerateGroupCommand extends ContainerAwareCommand {
12
13
		protected function configure() {
14
			$this
15
				->setName('auth:role:add')
16
				->setDescription('Create a single role into the database')
17
				->addArgument('code', InputArgument::REQUIRED, 'Enter the code of the role?')
18
				->addArgument('name', InputArgument::REQUIRED, 'Enter the name of the role?');
19
		}
20
21
22
		protected function execute(InputInterface $input, OutputInterface $output) {
23
			$code = $input->getArgument('code');
24
			$name = $input->getArgument('name');
25
26
			$db = $this->getContainer()->get('doctrine')
27
			           ->getRepository('VivaitAuthBundle:Group')
28
			           ->findOneBy(array('role'=>$code));
29
30
			if($db) {
31
				$output->writeln(sprintf("There is already a role (%s) with code: %s",$db->getName(),$db->getRole()));
32
				return null;
33
			}
34
35
			$group = new Group();
36
			$group->setRole($code);
37
			$group->setName($name);
38
39
			$em = $this->getContainer()->get('doctrine')->getManager();
40
			$em->persist($group);
41
			$em->flush();
42
43
			$output->writeln(sprintf("Success: A new role has been created (%s: %s)", $group->getRole(),$group->getName()));
44
		}
45
	}