Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
54 | class VettingService |
||
55 | { |
||
56 | const REGISTRATION_CODE_EXPIRED_ERROR = |
||
57 | 'Surfnet\Stepup\Exception\DomainException: Cannot vet second factor, the registration window is closed.'; |
||
58 | |||
59 | /** |
||
60 | * @var \Surfnet\StepupBundle\Service\SmsSecondFactorService |
||
61 | */ |
||
62 | private $smsSecondFactorService; |
||
63 | |||
64 | /** |
||
65 | * @var \Surfnet\StepupRa\RaBundle\Service\YubikeySecondFactorService |
||
66 | */ |
||
67 | private $yubikeySecondFactorService; |
||
68 | |||
69 | /** |
||
70 | * @var \Surfnet\StepupRa\RaBundle\Service\GssfService |
||
71 | */ |
||
72 | private $gssfService; |
||
73 | |||
74 | /** |
||
75 | * @var \Surfnet\StepupRa\RaBundle\Service\U2fService |
||
76 | */ |
||
77 | private $u2fService; |
||
78 | |||
79 | /** |
||
80 | * @var \Surfnet\StepupRa\RaBundle\Service\CommandService |
||
81 | */ |
||
82 | private $commandService; |
||
83 | |||
84 | /** |
||
85 | * @var \Surfnet\StepupRa\RaBundle\Repository\VettingProcedureRepository |
||
86 | */ |
||
87 | private $vettingProcedureRepository; |
||
88 | |||
89 | /** |
||
90 | * @var \Symfony\Component\Translation\TranslatorInterface |
||
91 | */ |
||
92 | private $translator; |
||
93 | |||
94 | /** |
||
95 | * @var \Surfnet\StepupRa\RaBundle\Service\IdentityService |
||
96 | */ |
||
97 | private $identityService; |
||
98 | |||
99 | /** |
||
100 | * @var \Surfnet\StepupBundle\Service\SecondFactorTypeService |
||
101 | */ |
||
102 | private $secondFactorTypeService; |
||
103 | |||
104 | public function __construct( |
||
105 | SmsSecondFactorService $smsSecondFactorService, |
||
106 | YubikeySecondFactorService $yubikeySecondFactorService, |
||
107 | GssfService $gssfService, |
||
108 | U2fService $u2fService, |
||
109 | CommandService $commandService, |
||
110 | VettingProcedureRepository $vettingProcedureRepository, |
||
111 | TranslatorInterface $translator, |
||
112 | IdentityService $identityService, |
||
113 | SecondFactorTypeService $secondFactorTypeService |
||
114 | ) { |
||
115 | $this->smsSecondFactorService = $smsSecondFactorService; |
||
116 | $this->yubikeySecondFactorService = $yubikeySecondFactorService; |
||
117 | $this->gssfService = $gssfService; |
||
118 | $this->u2fService = $u2fService; |
||
119 | $this->commandService = $commandService; |
||
120 | $this->vettingProcedureRepository = $vettingProcedureRepository; |
||
121 | $this->translator = $translator; |
||
122 | $this->identityService = $identityService; |
||
123 | $this->secondFactorTypeService = $secondFactorTypeService; |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * @param StartVettingProcedureCommand $command |
||
128 | * @return bool |
||
129 | */ |
||
130 | public function isLoaSufficientToStartProcedure(StartVettingProcedureCommand $command) |
||
136 | |||
137 | /** |
||
138 | * @param StartVettingProcedureCommand $command |
||
139 | * @return bool |
||
140 | */ |
||
141 | public function isExpiredRegistrationCode(StartVettingProcedureCommand $command) |
||
142 | { |
||
143 | return DateTime::now()->comesAfter( |
||
144 | new DateTime( |
||
145 | $command->secondFactor->registrationRequestedAt |
||
146 | ->add(new \DateInterval('P14D')) |
||
147 | ->setTime(23, 59, 59) |
||
148 | ) |
||
149 | ); |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * @param StartVettingProcedureCommand $command |
||
154 | * @return string The procedure ID. |
||
155 | */ |
||
156 | public function startProcedure(StartVettingProcedureCommand $command) |
||
181 | |||
182 | /** |
||
183 | * @param string $procedureId |
||
184 | * @throws UnknownVettingProcedureException |
||
185 | */ |
||
186 | View Code Duplication | public function cancelProcedure($procedureId) |
|
202 | |||
203 | /** |
||
204 | * @return int |
||
205 | */ |
||
206 | public function getSmsOtpRequestsRemainingCount() |
||
210 | |||
211 | /** |
||
212 | * @return int |
||
213 | */ |
||
214 | public function getSmsMaximumOtpRequestsCount() |
||
218 | |||
219 | /** |
||
220 | * @param string $procedureId |
||
221 | * @param SendSmsChallengeCommand $command |
||
222 | * @return bool |
||
223 | * @throws UnknownVettingProcedureException |
||
224 | * @throws RuntimeException |
||
225 | */ |
||
226 | public function sendSmsChallenge($procedureId, SendSmsChallengeCommand $command) |
||
227 | { |
||
228 | $procedure = $this->getProcedure($procedureId); |
||
229 | |||
230 | $phoneNumber = InternationalPhoneNumber::fromStringFormat( |
||
231 | $procedure->getSecondFactor()->secondFactorIdentifier |
||
232 | ); |
||
233 | |||
234 | $identity = $this->identityService->findById($procedure->getSecondFactor()->identityId); |
||
235 | |||
236 | if (!$identity) { |
||
237 | throw new RuntimeException("Second factor is coupled to an identity that doesn't exist"); |
||
238 | } |
||
239 | |||
240 | $command->phoneNumber = $phoneNumber; |
||
241 | $command->body = $this->translator->trans('ra.vetting.sms.challenge_body', [], 'messages', $identity->preferredLocale); |
||
242 | $command->identity = $procedure->getSecondFactor()->identityId; |
||
243 | $command->institution = $procedure->getSecondFactor()->institution; |
||
244 | |||
245 | return $this->smsSecondFactorService->sendChallenge($command); |
||
246 | } |
||
247 | |||
248 | /** |
||
249 | * @param string $procedureId |
||
250 | * @param VerifyPossessionOfPhoneCommand $command |
||
251 | * @return OtpVerification |
||
252 | * @throws UnknownVettingProcedureException |
||
253 | * @throws DomainException |
||
254 | */ |
||
255 | public function verifyPhoneNumber($procedureId, VerifyPossessionOfPhoneCommand $command) |
||
270 | |||
271 | /** |
||
272 | * @param string $procedureId |
||
273 | * @param VerifyYubikeyPublicIdCommand $command |
||
274 | * @return YubikeySecondFactor\VerificationResult |
||
275 | */ |
||
276 | public function verifyYubikeyPublicId($procedureId, VerifyYubikeyPublicIdCommand $command) |
||
294 | |||
295 | /** |
||
296 | * @param string $procedureId |
||
297 | */ |
||
298 | public function startGssfVerification($procedureId) |
||
304 | |||
305 | /** |
||
306 | * @param string $gssfId |
||
307 | * @return GssfVerificationResult |
||
308 | */ |
||
309 | public function verifyGssfId($gssfId) |
||
324 | |||
325 | /** |
||
326 | * @param string $procedureId |
||
327 | * @return SignRequestCreationResult |
||
328 | */ |
||
329 | public function createU2fSignRequest($procedureId) |
||
340 | |||
341 | /** |
||
342 | * @param string $procedureId |
||
343 | * @param SignRequest $signRequest |
||
344 | * @param SignResponse $signResponse |
||
345 | * @return AuthenticationVerificationResult |
||
346 | */ |
||
347 | public function verifyU2fAuthentication($procedureId, SignRequest $signRequest, SignResponse $signResponse) |
||
366 | |||
367 | /** |
||
368 | * @param string $procedureId |
||
369 | * @param VerifyIdentityCommand $command |
||
370 | * @return void |
||
371 | * @throws UnknownVettingProcedureException |
||
372 | * @throws DomainException |
||
373 | */ |
||
374 | public function verifyIdentity($procedureId, VerifyIdentityCommand $command) |
||
381 | |||
382 | /** |
||
383 | * @param string $procedureId |
||
384 | * @return \Surfnet\StepupMiddlewareClient\Service\ExecutionResult |
||
385 | * @throws UnknownVettingProcedureException |
||
386 | * @throws DomainException |
||
387 | */ |
||
388 | public function vet($procedureId) |
||
411 | |||
412 | /** |
||
413 | * @param string $procedureId |
||
414 | * @return string |
||
415 | * @throws UnknownVettingProcedureException |
||
416 | */ |
||
417 | public function getIdentityCommonName($procedureId) |
||
421 | |||
422 | /** |
||
423 | * @param $procedureId |
||
424 | * @return string |
||
425 | * @throws UnknownVettingProcedureException |
||
426 | */ |
||
427 | public function getSecondFactorIdentifier($procedureId) |
||
431 | |||
432 | /** |
||
433 | * @param string $procedureId |
||
434 | * @return null|VettingProcedure |
||
435 | * @throws UnknownVettingProcedureException |
||
436 | */ |
||
437 | View Code Duplication | private function getProcedure($procedureId) |
|
453 | |||
454 | /** |
||
455 | * @param string $procedureId |
||
456 | * @return bool |
||
457 | */ |
||
458 | public function hasProcedure($procedureId) |
||
466 | } |
||
467 |
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.