| Conditions | 12 |
| Paths | 48 |
| Total Lines | 101 |
| Code Lines | 59 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 2 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 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 |