GenerateGroupCommand   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6
Metric Value
wmc 3
lcom 0
cbo 6
dl 0
loc 35
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 7 1
A execute() 0 23 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
	}