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