Conditions | 8 |
Paths | 31 |
Total Lines | 85 |
Lines | 85 |
Ratio | 100 % |
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 |
||
53 | protected function execute(InputInterface $input, OutputInterface $output) |
||
54 | { |
||
55 | $this->tokenStorage->setToken( |
||
56 | new AnonymousToken('cli.bootstrap-yubikey-token', 'cli', ['ROLE_SS', 'ROLE_RA']) |
||
57 | ); |
||
58 | $nameId = new NameId($input->getArgument('name-id')); |
||
59 | $institutionText = $input->getArgument('institution'); |
||
60 | $institution = new Institution($institutionText); |
||
61 | $mailVerificationRequired = $this->requiresMailVerification($institutionText); |
||
62 | $registrationStatus = $input->getArgument('registration-status'); |
||
63 | $yubikey = $input->getArgument('yubikey'); |
||
64 | $actorId = $input->getArgument('actor-id'); |
||
65 | $this->enrichEventMetadata($actorId); |
||
66 | if (!$this->tokenBootstrapService->hasIdentityWithNameIdAndInstitution($nameId, $institution)) { |
||
67 | $output->writeln( |
||
68 | sprintf( |
||
69 | '<error>An identity with name ID "%s" from institution "%s" does not exist, create it first.</error>', |
||
70 | $nameId->getNameId(), |
||
71 | $institution->getInstitution() |
||
72 | ) |
||
73 | ); |
||
74 | |||
75 | return; |
||
76 | } |
||
77 | $identity = $this->tokenBootstrapService->findOneByNameIdAndInstitution($nameId, $institution); |
||
78 | $output->writeln(sprintf('<comment>Adding a %s Yubikey token for %s</comment>', $registrationStatus, $identity->commonName)); |
||
79 | $this->beginTransaction(); |
||
80 | $secondFactorId = Uuid::uuid4()->toString(); |
||
81 | |||
82 | try { |
||
83 | switch ($registrationStatus) { |
||
84 | case "unverified": |
||
85 | $output->writeln('<comment>Creating an unverified Yubikey token</comment>'); |
||
86 | $this->provePossession($secondFactorId, $identity, $yubikey); |
||
87 | break; |
||
88 | case "verified": |
||
89 | $output->writeln('<comment>Creating an unverified Yubikey token</comment>'); |
||
90 | $this->provePossession($secondFactorId, $identity, $yubikey); |
||
91 | $unverifiedSecondFactor = $this->tokenBootstrapService->findUnverifiedToken($identity->id, 'yubikey'); |
||
92 | if ($mailVerificationRequired) { |
||
93 | $output->writeln('<comment>Creating a verified Yubikey token</comment>'); |
||
94 | $this->verifyEmail($identity, $unverifiedSecondFactor); |
||
95 | } |
||
96 | break; |
||
97 | case "vetted": |
||
98 | $output->writeln('<comment>Creating an unverified Yubikey token</comment>'); |
||
99 | $this->provePossession($secondFactorId, $identity, $yubikey); |
||
100 | /** @var UnverifiedSecondFactor $unverifiedSecondFactor */ |
||
101 | $unverifiedSecondFactor = $this->tokenBootstrapService->findUnverifiedToken($identity->id, 'yubikey'); |
||
102 | if ($mailVerificationRequired) { |
||
103 | $output->writeln('<comment>Creating a verified Yubikey token</comment>'); |
||
104 | $this->verifyEmail($identity, $unverifiedSecondFactor); |
||
105 | } |
||
106 | $verifiedSecondFactor = $this->tokenBootstrapService->findVerifiedToken($identity->id, 'yubikey'); |
||
107 | $output->writeln('<comment>Vetting the verified Yubikey token</comment>'); |
||
108 | $this->vetSecondFactor( |
||
109 | 'yubikey', |
||
110 | $actorId, |
||
111 | $identity, |
||
112 | $secondFactorId, |
||
113 | $verifiedSecondFactor, |
||
114 | $yubikey |
||
115 | ); |
||
116 | break; |
||
117 | } |
||
118 | $this->finishTransaction(); |
||
119 | } catch (Exception $e) { |
||
120 | $output->writeln( |
||
121 | sprintf( |
||
122 | '<error>An Error occurred when trying to bootstrap the Yubikey token: "%s"</error>', |
||
123 | $e->getMessage() |
||
124 | ) |
||
125 | ); |
||
126 | $this->rollback(); |
||
127 | throw $e; |
||
128 | } |
||
129 | $output->writeln( |
||
130 | sprintf( |
||
131 | '<info>Successfully created identity with UUID %s and %s second factor with UUID %s</info>', |
||
132 | $identity->id, |
||
133 | $registrationStatus, |
||
134 | $secondFactorId |
||
135 | ) |
||
136 | ); |
||
137 | } |
||
138 | |||
149 |
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.