Conditions | 9 |
Paths | 144 |
Total Lines | 117 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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 |
||
59 | protected function execute(InputInterface $input, OutputInterface $output) |
||
60 | { |
||
61 | $this->tokenStorage->setToken( |
||
62 | new AnonymousToken('cli.bootstrap-identity-with-sms-token', 'cli', ['ROLE_SS', 'ROLE_RA']) |
||
63 | ); |
||
64 | |||
65 | $nameId = new NameId($input->getArgument('name-id')); |
||
66 | $institution = new Institution($input->getArgument('institution')); |
||
67 | $mailVerificationRequired = $this->requiresMailVerification($institution); |
||
68 | $commonName = $input->getArgument('common-name'); |
||
69 | $email = $input->getArgument('email'); |
||
70 | $preferredLocale = $input->getArgument('preferred-locale'); |
||
71 | $registrationStatus = $input->getArgument('registration-status'); |
||
72 | $phoneNumber = $input->getArgument('phone-number'); |
||
73 | $identity = false; |
||
74 | |||
75 | $output->writeln( |
||
76 | sprintf( |
||
77 | '<notice>Adding a %s SMS token for %s</notice>', |
||
78 | $registrationStatus, |
||
79 | $commonName |
||
80 | ) |
||
81 | ); |
||
82 | |||
83 | if ($this->identityRepository->hasIdentityWithNameIdAndInstitution($nameId, $institution)) { |
||
84 | $output->writeln( |
||
85 | sprintf( |
||
86 | '<notice>An identity with name ID "%s" from institution "%s" already exists, using that identity</notice>', |
||
87 | $nameId->getNameId(), |
||
88 | $institution->getInstitution() |
||
89 | ) |
||
90 | ); |
||
91 | $identity = $this->identityRepository->findOneByNameIdAndInstitution($nameId, $institution); |
||
92 | } |
||
93 | |||
94 | $this->connection->beginTransaction(); |
||
95 | |||
96 | $secondFactorId = Uuid::uuid4()->toString(); |
||
97 | |||
98 | if (!$identity) { |
||
99 | $output->writeln('<notice>Creating a new identity</notice>'); |
||
100 | $identity = $this->createIdentity($institution, $nameId, $commonName, $email, $preferredLocale); |
||
101 | } |
||
102 | |||
103 | try { |
||
104 | switch ($registrationStatus) { |
||
105 | case "unverified": |
||
106 | $output->writeln('<notice>Creating an unverified SMS token</notice>'); |
||
107 | $this->provePossession($secondFactorId, $identity, $phoneNumber); |
||
108 | break; |
||
109 | case "verified": |
||
110 | $output->writeln('<notice>Creating an unverified SMS token</notice>'); |
||
111 | $this->provePossession($secondFactorId, $identity, $phoneNumber); |
||
112 | /** @var UnverifiedSecondFactor $unverifiedSecondFactor */ |
||
113 | $unverifiedSecondFactor = $this->unverifiedSecondFactorRepository->findOneBy( |
||
114 | ['identityId' => $identity->id, 'type' => 'sms'] |
||
115 | ); |
||
116 | |||
117 | if ($mailVerificationRequired) { |
||
118 | $output->writeln('<notice>Creating a verified SMS token</notice>'); |
||
119 | $this->verifyEmail($identity, $unverifiedSecondFactor); |
||
120 | } |
||
121 | |||
122 | break; |
||
123 | case "vetted": |
||
124 | $output->writeln('<notice>Creating an unverified SMS token</notice>'); |
||
125 | $this->provePossession($secondFactorId, $identity, $phoneNumber); |
||
126 | /** @var UnverifiedSecondFactor $unverifiedSecondFactor */ |
||
127 | $unverifiedSecondFactor = $this->unverifiedSecondFactorRepository->findOneBy( |
||
128 | ['identityId' => $identity->id, 'type' => 'sms'] |
||
129 | ); |
||
130 | |||
131 | if ($mailVerificationRequired) { |
||
132 | $output->writeln('<notice>Creating a verified SMS token</notice>'); |
||
133 | $this->verifyEmail($identity, $unverifiedSecondFactor); |
||
134 | } |
||
135 | /** @var VerifiedSecondFactor $verifiedSecondFactor */ |
||
136 | $verifiedSecondFactor = $this->verifiedSecondFactorRepository->findOneBy( |
||
137 | ['identityId' => $identity->id, 'type' => 'sms'] |
||
138 | ); |
||
139 | $output->writeln('<notice>Vetting the verified SMS token</notice>'); |
||
140 | $this->vetSecondFactor( |
||
141 | 'sms', |
||
142 | 'db9b8bdf-720c-44ba-a4c4-154953e45f14', |
||
143 | $identity, |
||
144 | $secondFactorId, |
||
145 | $verifiedSecondFactor, |
||
146 | $phoneNumber |
||
147 | ); |
||
148 | break; |
||
149 | } |
||
150 | |||
151 | $this->eventBus->flush(); |
||
152 | $this->connection->commit(); |
||
153 | |||
154 | } catch (Exception $e) { |
||
155 | $output->writeln( |
||
156 | sprintf( |
||
157 | '<error>An Error occurred when trying to bootstrap the identity: "%s"</error>', |
||
158 | $e->getMessage() |
||
159 | ) |
||
160 | ); |
||
161 | |||
162 | $this->connection->rollBack(); |
||
163 | |||
164 | throw $e; |
||
165 | } |
||
166 | |||
167 | $output->writeln( |
||
168 | sprintf( |
||
169 | '<info>Successfully created identity with UUID %s and %s second factor with UUID %s</info>', |
||
170 | $identity->id, |
||
171 | $registrationStatus, |
||
172 | $secondFactorId |
||
173 | ) |
||
174 | ); |
||
175 | } |
||
176 | |||
217 |
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.