UserGroupCommand::configure()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 7

Duplication

Lines 8
Ratio 100 %
Metric Value
dl 8
loc 8
rs 9.4285
cc 1
eloc 7
nc 1
nop 0
1
<?php
2
3
	namespace Vivait\AuthBundle\Command;
4
5
	use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
6
	use Symfony\Component\Console\Input\InputArgument;
7
	use Symfony\Component\Console\Input\InputInterface;
8
	use Symfony\Component\Console\Output\OutputInterface;
9
10 View Code Duplication
	class UserGroupCommand extends ContainerAwareCommand {
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
11
12
		protected function configure() {
13
			$this
14
				->setName('auth:user:roles')
15
				->setDescription('Add/Remove a user from a role')
16
				->addArgument('role', InputArgument::REQUIRED, 'Code of the role')
17
				->addArgument('action', InputArgument::REQUIRED, '[Add|Remove]')
18
				->addArgument('username', InputArgument::REQUIRED, 'Username of the User?');
19
		}
20
21
22
		protected function execute(InputInterface $input, OutputInterface $output) {
23
			$rolecode = $input->getArgument('role');
24
			$action     = $input->getArgument('action');
25
			$username   = $input->getArgument('username');
26
27
			#check modifer
28
			if(!strtolower($action) == "add" && !strtolower($action) == "remove") {
29
				$output->writeln(sprintf("Error: Invalid action: %s, should be [Add|Remove]", $action));
30
				return;
31
			}
32
33
			#find role
34
			$role = $this->getContainer()->get('doctrine')
35
			               ->getRepository('VivaitAuthBundle:Group')
36
			               ->findOneBy(array('role' => $rolecode));
37
38
			if(!$role) {
39
				$output->writeln(sprintf("Error: Could not find role with code: %s", $role));
40
				return null;
41
			}
42
43
			#find user
44
			$user = $this->getContainer()->get('doctrine')
45
			             ->getRepository('VivaitAuthBundle:User')
46
			             ->findOneBy(array('username' => $username));
47
48
			if(!$user) {
49
				$output->writeln(sprintf("Error: Could not find user with username: %s", $username));
50
				return null;
51
			}
52
53
			#should have now valid parameters
54
			if(strtolower($action) == 'add') {
55
				#check to see if already in role list
56
				if($user->getGroups()->contains($role)) {
57
					$output->writeln(sprintf("Error: %s is already associated with tenant: %s", $user->getFullname(),$role->getName()));
58
					return null;
59
				}
60
				$user->addGroup($role);
61
			} else {
62
				#check to see if already removed from role list
63
				if(!$user->getGroups()->contains($role)) {
64
					$output->writeln(sprintf("Error: %s not currently associated with tenant: %s", $user->getFullname(),$role->getName()));
65
					return null;
66
				}
67
				$user->removeGroup($role);
68
			}
69
70
			$em = $this->getContainer()->get('doctrine')->getManager();
71
			$em->persist($user);
72
			$em->flush();
73
74
			$output->writeln(sprintf("Success: %s has been %s %s role %s",
75
			                         $user->getFullname(),
76
			                         strtolower($action) == 'add' ? 'added' : 'removed',
77
			                         strtolower($action) == 'add' ? 'to' : 'from',
78
			                         $role->getName()
79
			                 ));
80
		}
81
	}