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 |
||
48 | class RaSecondFactorProjector extends Projector |
||
49 | { |
||
50 | /** |
||
51 | * @var RaSecondFactorRepository |
||
52 | */ |
||
53 | private $raSecondFactorRepository; |
||
54 | |||
55 | /** |
||
56 | * @var IdentityRepository |
||
57 | */ |
||
58 | private $identityRepository; |
||
59 | |||
60 | public function __construct( |
||
61 | RaSecondFactorRepository $raSecondFactorRepository, |
||
62 | IdentityRepository $identityRepository |
||
63 | ) { |
||
64 | $this->raSecondFactorRepository = $raSecondFactorRepository; |
||
65 | $this->identityRepository = $identityRepository; |
||
66 | } |
||
67 | |||
68 | View Code Duplication | public function applyIdentityRenamedEvent(IdentityRenamedEvent $event) |
|
|
|||
69 | { |
||
70 | $secondFactors = $this->raSecondFactorRepository->findByIdentityId((string) $event->identityId); |
||
71 | |||
72 | if (count($secondFactors) === 0) { |
||
73 | return; |
||
74 | } |
||
75 | |||
76 | $commonName = $event->commonName; |
||
77 | |||
78 | foreach ($secondFactors as $secondFactor) { |
||
79 | $secondFactor->name = $commonName; |
||
80 | } |
||
81 | |||
82 | $this->raSecondFactorRepository->saveAll($secondFactors); |
||
83 | } |
||
84 | |||
85 | View Code Duplication | public function applyIdentityEmailChangedEvent(IdentityEmailChangedEvent $event) |
|
86 | { |
||
87 | $secondFactors = $this->raSecondFactorRepository->findByIdentityId((string) $event->identityId); |
||
88 | |||
89 | if (count($secondFactors) === 0) { |
||
90 | return; |
||
91 | } |
||
92 | |||
93 | $email = $event->email; |
||
94 | |||
95 | foreach ($secondFactors as $secondFactor) { |
||
96 | $secondFactor->email = $email; |
||
97 | } |
||
98 | |||
99 | $this->raSecondFactorRepository->saveAll($secondFactors); |
||
100 | } |
||
101 | |||
102 | public function applyYubikeySecondFactorBootstrappedEvent(YubikeySecondFactorBootstrappedEvent $event) |
||
103 | { |
||
104 | $identity = $this->identityRepository->find((string) $event->identityId); |
||
105 | |||
106 | $secondFactor = new RaSecondFactor( |
||
107 | (string) $event->secondFactorId, |
||
108 | 'yubikey', |
||
109 | (string) $event->yubikeyPublicId, |
||
110 | $identity->id, |
||
111 | $identity->institution, |
||
112 | $event->commonName, |
||
113 | $event->email |
||
114 | ); |
||
115 | $secondFactor->status = SecondFactorStatus::vetted(); |
||
116 | |||
117 | $this->raSecondFactorRepository->save($secondFactor); |
||
118 | } |
||
119 | |||
120 | View Code Duplication | public function applyYubikeyPossessionProvenEvent(YubikeyPossessionProvenEvent $event) |
|
121 | { |
||
122 | $identity = $this->identityRepository->find((string) $event->identityId); |
||
123 | |||
124 | $this->raSecondFactorRepository->save( |
||
125 | new RaSecondFactor( |
||
126 | (string) $event->secondFactorId, |
||
127 | 'yubikey', |
||
128 | (string) $event->yubikeyPublicId, |
||
129 | $identity->id, |
||
130 | $identity->institution, |
||
131 | $event->commonName, |
||
132 | $event->email |
||
133 | ) |
||
134 | ); |
||
135 | } |
||
136 | |||
137 | View Code Duplication | public function applyPhonePossessionProvenEvent(PhonePossessionProvenEvent $event) |
|
138 | { |
||
139 | $identity = $this->identityRepository->find((string) $event->identityId); |
||
140 | |||
141 | $this->raSecondFactorRepository->save( |
||
142 | new RaSecondFactor( |
||
143 | (string) $event->secondFactorId, |
||
144 | 'sms', |
||
145 | (string) $event->phoneNumber, |
||
146 | $identity->id, |
||
147 | $identity->institution, |
||
148 | $event->commonName, |
||
149 | $event->email |
||
150 | ) |
||
151 | ); |
||
152 | } |
||
153 | |||
154 | View Code Duplication | public function applyGssfPossessionProvenEvent(GssfPossessionProvenEvent $event) |
|
155 | { |
||
156 | $identity = $this->identityRepository->find((string) $event->identityId); |
||
157 | |||
158 | $this->raSecondFactorRepository->save( |
||
159 | new RaSecondFactor( |
||
160 | (string) $event->secondFactorId, |
||
161 | (string) $event->stepupProvider, |
||
162 | (string) $event->gssfId, |
||
163 | $identity->id, |
||
164 | $identity->institution, |
||
165 | $event->commonName, |
||
166 | $event->email |
||
167 | ) |
||
168 | ); |
||
169 | } |
||
170 | |||
171 | View Code Duplication | public function applyU2fDevicePossessionProvenEvent(U2fDevicePossessionProvenEvent $event) |
|
172 | { |
||
173 | $identity = $this->identityRepository->find((string) $event->identityId); |
||
174 | |||
175 | $this->raSecondFactorRepository->save( |
||
176 | new RaSecondFactor( |
||
177 | (string) $event->secondFactorId, |
||
178 | 'u2f', |
||
179 | $event->keyHandle->getValue(), |
||
180 | $identity->id, |
||
181 | $identity->institution, |
||
182 | $event->commonName, |
||
183 | $event->email |
||
184 | ) |
||
185 | ); |
||
186 | } |
||
187 | |||
188 | public function applyEmailVerifiedEvent(EmailVerifiedEvent $event) |
||
192 | |||
193 | public function applySecondFactorVettedEvent(SecondFactorVettedEvent $event) |
||
194 | { |
||
195 | $this->updateStatus($event->secondFactorId, SecondFactorStatus::vetted()); |
||
196 | } |
||
197 | |||
198 | protected function applyUnverifiedSecondFactorRevokedEvent(UnverifiedSecondFactorRevokedEvent $event) |
||
202 | |||
203 | protected function applyCompliedWithUnverifiedSecondFactorRevocationEvent( |
||
204 | CompliedWithUnverifiedSecondFactorRevocationEvent $event |
||
205 | ) { |
||
206 | $this->updateStatus($event->secondFactorId, SecondFactorStatus::revoked()); |
||
207 | } |
||
208 | |||
209 | protected function applyVerifiedSecondFactorRevokedEvent(VerifiedSecondFactorRevokedEvent $event) |
||
210 | { |
||
211 | $this->updateStatus($event->secondFactorId, SecondFactorStatus::revoked()); |
||
212 | } |
||
213 | |||
214 | protected function applyCompliedWithVerifiedSecondFactorRevocationEvent( |
||
215 | CompliedWithVerifiedSecondFactorRevocationEvent $event |
||
216 | ) { |
||
217 | $this->updateStatus($event->secondFactorId, SecondFactorStatus::revoked()); |
||
218 | } |
||
219 | |||
220 | protected function applyVettedSecondFactorRevokedEvent(VettedSecondFactorRevokedEvent $event) |
||
221 | { |
||
222 | $this->updateStatus($event->secondFactorId, SecondFactorStatus::revoked()); |
||
223 | } |
||
224 | |||
225 | protected function applyCompliedWithVettedSecondFactorRevocationEvent( |
||
226 | CompliedWithVettedSecondFactorRevocationEvent $event |
||
227 | ) { |
||
228 | $this->updateStatus($event->secondFactorId, SecondFactorStatus::revoked()); |
||
229 | } |
||
230 | |||
231 | protected function applyIdentityForgottenEvent(IdentityForgottenEvent $event) |
||
235 | |||
236 | /** |
||
237 | * @param SecondFactorId $secondFactorId |
||
238 | * @param SecondFactorStatus $status |
||
239 | */ |
||
240 | private function updateStatus(SecondFactorId $secondFactorId, SecondFactorStatus $status) |
||
241 | { |
||
242 | $secondFactor = $this->raSecondFactorRepository->find((string) $secondFactorId); |
||
243 | $secondFactor->status = $status; |
||
244 | |||
245 | $this->raSecondFactorRepository->save($secondFactor); |
||
246 | } |
||
247 | } |
||
248 |
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.