GenerateUserCommand   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 3
Bugs 0 Features 3
Metric Value
wmc 13
lcom 1
cbo 6
dl 0
loc 117
rs 10
c 3
b 0
f 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 11 1
C execute() 0 101 12
1
<?php
2
3
	namespace Vivait\AuthBundle\Command;
4
5
6
	use Vivait\AuthBundle\Entity\User;
7
	use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
8
	use Symfony\Component\Console\Input\InputArgument;
9
	use Symfony\Component\Console\Input\InputInterface;
10
	use Symfony\Component\Console\Output\OutputInterface;
11
12
	class GenerateUserCommand extends ContainerAwareCommand {
13
14
		protected function configure() {
15
			$this
16
				->setName('auth:user:add')
17
				->setDescription('Create a single user into the database')
18
				->addArgument('fullname', InputArgument::REQUIRED, 'Enter the name of the user?')
19
				->addArgument('password', InputArgument::REQUIRED, 'Enter the username of the user?')
20
				->addArgument('email', InputArgument::REQUIRED, 'Enter the email address of the user?')
21
				->addArgument('role', InputArgument::REQUIRED, 'Enter the role of the user?')
22
				->addArgument('tenant', InputArgument::REQUIRED, 'Enter the code of the tenant')
23
                ->addArgument('username', InputArgument::OPTIONAL, 'Enter the username of the user?');
24
		}
25
26
27
		protected function execute(InputInterface $input, OutputInterface $output) {
28
			$fullname   = ucwords($input->getArgument('fullname'));
29
			$password   = $input->getArgument('password');
30
			$email      = $input->getArgument('email');
31
			$role       = $input->getArgument('role');
32
			$tenantcode = $input->getArgument('tenant');
33
            $username   = $input->getArgument('username');
34
            $initials   = "";
35
36
            $names = explode(' ', $fullname);
37
38
            #create a user name if not specified
39
            if(!$username){
40
                $username = implode('', $names);
41
            }
42
43
            #create initials if not specified
44
            if(!$initials){
45
                foreach($names as $name){
46
                    $initials .= $name[0];
47
                }
48
            }
49
50
51
			#find user from username
52
			$user = $this->getContainer()->get('doctrine')
53
			              ->getRepository('VivaitAuthBundle:User')
54
			              ->findOneBy(array('username' => $username));
55
56
57
			if($user) {
58
				$output->writeln(sprintf("Error: There is already a user (%s) by this username: %s", $user->getFullname(), $user->getUsername()));
59
				return;
60
			}
61
62
			#find user from email address
63
			$user = $this->getContainer()->get('doctrine')
64
			              ->getRepository('VivaitAuthBundle:User')
65
			              ->findOneBy(array('email' => $email));
66
67
68
			if($user) {
69
				$output->writeln(sprintf("Error: There is already a user (%s) by this email address: %s", $user->getFullname(), $user->getEmail()));
70
				return;
71
			}
72
73
			#find group
74
			$group = $this->getContainer()->get('doctrine')
75
			              ->getRepository('VivaitAuthBundle:Group')
76
			              ->findOneBy(array('role' => $role));
77
78
79
			if(!$group) {
80
				$output->writeln(sprintf("Could not find a group with the role of %s", $role));
81
				return;
82
			}
83
84
			#find tenant
85
			$tenant = $this->getContainer()->get('doctrine')
86
			               ->getRepository('VivaitAuthBundle:Tenant')
87
			               ->findOneBy(array('code' => $tenantcode));
88
89
			if($tenantcode && !$tenant) {
90
				$output->writeln(sprintf("Error: Could not find tenant with code: %s", $tenantcode));
91
				return null;
92
			}
93
94
95
			$user = new User();
96
            $user->setFullname($fullname);
97
			$user->setUsername($username);
98
			$user->setActive(true);
99
			$user->setEmail($email);
100
			$user->setInitials($initials);
101
			$user->addGroup($group);
102
103
			if($tenant) {
104
				$user->addTenant($tenant);
105
			}
106
107
			$factory = $this->getContainer()->get('security.encoder_factory');
108
			$encoder = $factory->getEncoder($this);
109
110
			$user->newSalt();
111
			$user->setPassword($encoder->encodePassword($password, $user->getSalt()));
112
113
			#persist
114
			$em = $this->getContainer()->get('doctrine')->getManager();
115
			$em->persist($user);
116
			$em->flush();
117
118
			$output->writeln(sprintf("Success: User added successfully\n         Username: %s",$user->getUsername()));
119
120
			if($group) {
121
				$output->writeln(sprintf("            Group: %s", $group->getName()));
122
			}
123
124
			if($tenant) {
125
				$output->writeln(sprintf("           Tenant: %s", $tenant->getTenant()));
126
			}
127
		}
128
	}
129