Total Complexity | 46 |
Total Lines | 383 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like ProxyStateHandler often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ProxyStateHandler, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | class ProxyStateHandler |
||
25 | { |
||
26 | private $sessionPath; |
||
27 | |||
28 | /** |
||
29 | * @var \Symfony\Component\HttpFoundation\Session\SessionInterface |
||
30 | */ |
||
31 | private $session; |
||
32 | |||
33 | /** |
||
34 | * @param SessionInterface $session |
||
35 | */ |
||
36 | public function __construct(SessionInterface $session, $sessionPath) |
||
37 | { |
||
38 | $this->sessionPath = $sessionPath; |
||
39 | $this->session = $session; |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * Clear the complete state, leaving other states intact. |
||
44 | */ |
||
45 | public function clear() |
||
46 | { |
||
47 | $all = $this->session->all(); |
||
48 | |||
49 | foreach (array_keys($all) as $key) { |
||
50 | if (strpos($key, $this->sessionPath) === 0) { |
||
51 | $this->session->remove($key); |
||
52 | } |
||
53 | } |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * @param string $originalRequestId |
||
58 | * @return $this |
||
59 | */ |
||
60 | public function setRequestId($originalRequestId) |
||
61 | { |
||
62 | $this->set('request_id', $originalRequestId); |
||
63 | |||
64 | return $this; |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * @return string|null |
||
69 | */ |
||
70 | public function getRequestId() |
||
71 | { |
||
72 | return $this->get('request_id'); |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * @param string $serviceProvider |
||
77 | * @return $this |
||
78 | */ |
||
79 | public function setRequestServiceProvider($serviceProvider) |
||
80 | { |
||
81 | $this->set('service_provider', $serviceProvider); |
||
82 | |||
83 | return $this; |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * @return string|null |
||
88 | */ |
||
89 | public function getRequestServiceProvider() |
||
90 | { |
||
91 | return $this->get('service_provider'); |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * @param string $url |
||
96 | * @return $this |
||
97 | */ |
||
98 | public function setRequestAssertionConsumerServiceUrl($url) |
||
99 | { |
||
100 | $this->set('assertion_consumer_service_url', $url); |
||
101 | |||
102 | return $this; |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * @return string|null |
||
107 | */ |
||
108 | public function getRequestAssertionConsumerServiceUrl() |
||
109 | { |
||
110 | return $this->get('assertion_consumer_service_url'); |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * @param string $relayState |
||
115 | * @return $this |
||
116 | */ |
||
117 | public function setRelayState($relayState) |
||
118 | { |
||
119 | $this->set('relay_state', $relayState); |
||
120 | |||
121 | return $this; |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * @return string|null |
||
126 | */ |
||
127 | public function getRelayState() |
||
128 | { |
||
129 | return $this->get('relay_state'); |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * @param string $loaIdentifier |
||
134 | * @return $this |
||
135 | */ |
||
136 | public function setRequiredLoaIdentifier($loaIdentifier) |
||
137 | { |
||
138 | $this->set('loa_identifier', $loaIdentifier); |
||
139 | |||
140 | return $this; |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * @return string|null |
||
145 | */ |
||
146 | public function getRequiredLoaIdentifier() |
||
147 | { |
||
148 | return $this->get('loa_identifier'); |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * @param string $requestId |
||
153 | * @return $this |
||
154 | */ |
||
155 | public function setGatewayRequestId($requestId) |
||
156 | { |
||
157 | $this->set('gateway_request_id', $requestId); |
||
158 | |||
159 | return $this; |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * @return string|null |
||
164 | */ |
||
165 | public function getGatewayRequestId() |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * @param string $assertionAsXmlString |
||
172 | * @return $this |
||
173 | */ |
||
174 | public function saveAssertion($assertionAsXmlString) |
||
175 | { |
||
176 | $this->set('response_assertion', $assertionAsXmlString); |
||
177 | |||
178 | return $this; |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * @return null|string |
||
183 | */ |
||
184 | public function getAssertion() |
||
185 | { |
||
186 | return $this->get('response_assertion'); |
||
187 | } |
||
188 | |||
189 | public function saveIdentityNameId(string $nameId) |
||
190 | { |
||
191 | $this->set('name_id', $nameId); |
||
192 | |||
193 | return $this; |
||
194 | } |
||
195 | |||
196 | public function getIdentityNameId(): string |
||
197 | { |
||
198 | $nameId = $this->get('name_id'); |
||
199 | if (!$nameId) { |
||
200 | throw new RuntimeException('Unable to retrieve NameId, it was not set on the state handler'); |
||
201 | } |
||
202 | |||
203 | if (!is_string($nameId)) { |
||
204 | throw new RuntimeException( |
||
205 | sprintf('Unable to retrieve NameId, must be a string, but a %s was set', gettype($nameId)) |
||
206 | ); |
||
207 | } |
||
208 | return $nameId; |
||
209 | } |
||
210 | |||
211 | /** |
||
212 | * @param string $idpEntityId |
||
213 | * @return $this |
||
214 | */ |
||
215 | public function setAuthenticatingIdp($idpEntityId) |
||
216 | { |
||
217 | $this->set('authenticating_idp', $idpEntityId); |
||
218 | |||
219 | return $this; |
||
220 | } |
||
221 | |||
222 | /** |
||
223 | * @return null|string |
||
224 | */ |
||
225 | public function getAuthenticatingIdp() |
||
226 | { |
||
227 | return $this->get('authenticating_idp'); |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * @param string|null $secondFactorId |
||
232 | * @return $this |
||
233 | */ |
||
234 | public function setSelectedSecondFactorId($secondFactorId) |
||
235 | { |
||
236 | $this->set('selected_second_factor', $secondFactorId); |
||
237 | |||
238 | return $this; |
||
239 | } |
||
240 | |||
241 | /** |
||
242 | * @return null|string |
||
243 | */ |
||
244 | public function getSelectedSecondFactorId() |
||
245 | { |
||
246 | return $this->get('selected_second_factor'); |
||
247 | } |
||
248 | |||
249 | /** |
||
250 | * @param bool $verified |
||
251 | * @return $this |
||
252 | */ |
||
253 | public function setSecondFactorVerified($verified) |
||
254 | { |
||
255 | $this->set('selected_second_factor_verified', $verified); |
||
256 | |||
257 | return $this; |
||
258 | } |
||
259 | |||
260 | /** |
||
261 | * @return bool |
||
262 | */ |
||
263 | public function isSecondFactorVerified() |
||
264 | { |
||
265 | return $this->get('selected_second_factor_verified') === true; |
||
266 | } |
||
267 | |||
268 | public function setVerifiedBySsoOn2faCookie(bool $isVerifiedByCookie): self |
||
269 | { |
||
270 | $this->set('verified_by_sso_on_2fa_cookie', $isVerifiedByCookie); |
||
271 | |||
272 | return $this; |
||
273 | } |
||
274 | |||
275 | public function isVerifiedBySsoOn2faCookie(): bool |
||
276 | { |
||
277 | return $this->get('verified_by_sso_on_2fa_cookie') === true; |
||
278 | } |
||
279 | |||
280 | public function setSsoOn2faCookieFingerprint(string $fingerprint) |
||
281 | { |
||
282 | $this->set('sso_on_2fa_cookie_fingerprint', $fingerprint); |
||
283 | |||
284 | return $this; |
||
285 | } |
||
286 | |||
287 | public function getSsoOn2faCookieFingerprint() |
||
288 | { |
||
289 | return $this->get('sso_on_2fa_cookie_fingerprint'); |
||
290 | } |
||
291 | |||
292 | /** |
||
293 | * @param string $controllerName |
||
294 | * @return $this |
||
295 | */ |
||
296 | public function setResponseAction($controllerName) |
||
297 | { |
||
298 | $this->set('response_controller', $controllerName); |
||
299 | return $this; |
||
300 | } |
||
301 | /** |
||
302 | * @return string|null |
||
303 | */ |
||
304 | public function getResponseAction() |
||
305 | { |
||
306 | return $this->get('response_controller'); |
||
307 | } |
||
308 | /** |
||
309 | * @param string $serviceId |
||
310 | * @return $this |
||
311 | */ |
||
312 | public function setResponseContextServiceId($serviceId) |
||
313 | { |
||
314 | $this->set('response_context_service_id', $serviceId); |
||
315 | return $this; |
||
316 | } |
||
317 | |||
318 | /** |
||
319 | * @return string|null |
||
320 | */ |
||
321 | public function getResponseContextServiceId() |
||
322 | { |
||
323 | return $this->get('response_context_service_id'); |
||
324 | } |
||
325 | |||
326 | /** |
||
327 | * @param $organization |
||
328 | * @return $this |
||
329 | */ |
||
330 | public function setSchacHomeOrganization($organization) |
||
331 | { |
||
332 | $this->set('schac_home_organization', $organization); |
||
333 | return $this; |
||
334 | } |
||
335 | |||
336 | |||
337 | public function setIsForceAuthn(bool $forceAuthn): self |
||
338 | { |
||
339 | $this->set('force_authn', $forceAuthn); |
||
340 | return $this; |
||
341 | } |
||
342 | |||
343 | |||
344 | public function isForceAuthn(): bool |
||
345 | { |
||
346 | return $this->get('force_authn') === true; |
||
347 | } |
||
348 | /** |
||
349 | * @return string|null |
||
350 | */ |
||
351 | public function getSchacHomeOrganization() |
||
352 | { |
||
353 | return $this->get('schac_home_organization'); |
||
354 | } |
||
355 | |||
356 | /** |
||
357 | * @param string $locale |
||
358 | * @return $this |
||
359 | */ |
||
360 | public function setPreferredLocale($locale) |
||
361 | { |
||
362 | $this->set('locale', $locale); |
||
363 | return $this; |
||
364 | } |
||
365 | |||
366 | /** |
||
367 | * @return string|null |
||
368 | */ |
||
369 | public function getPreferredLocale() |
||
370 | { |
||
371 | return $this->get('locale'); |
||
372 | } |
||
373 | |||
374 | /** |
||
375 | * note that the authentication mode is stored outside the session path, to enable other state handlers |
||
376 | * to retrieve the Authentication state for a given authentication request id. |
||
377 | * |
||
378 | * @param $requestId |
||
379 | * @param $authenticationMode |
||
380 | */ |
||
381 | public function markAuthenticationModeForRequest($requestId, $authenticationMode) |
||
384 | } |
||
385 | |||
386 | public function getAuthenticationModeForRequestId($requestId) |
||
387 | { |
||
388 | return $this->session->get('surfnet/gateway/auth_mode/' . $requestId); |
||
389 | } |
||
390 | |||
391 | /** |
||
392 | * @param string $key |
||
393 | * @param mixed $value Any scalar |
||
394 | */ |
||
395 | protected function set($key, $value) |
||
396 | { |
||
397 | $this->session->set($this->sessionPath . $key, $value); |
||
398 | } |
||
399 | |||
400 | /** |
||
401 | * @param string $key |
||
402 | * @return mixed|null Any scalar |
||
403 | */ |
||
404 | protected function get($key) |
||
405 | { |
||
406 | return $this->session->get($this->sessionPath . $key); |
||
407 | } |
||
408 | } |
||
409 |