Conditions | 9 |
Paths | 8 |
Total Lines | 76 |
Code Lines | 44 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
102 | public function process(GetResponseEvent $event) |
||
103 | { |
||
104 | if ($this->tokenStorage->getToken() === null |
||
105 | && $this->samlInteractionProvider->isSamlAuthenticationInitiated() |
||
106 | ) { |
||
107 | $expectedInResponseTo = $this->authenticationStateHandler->getRequestId(); |
||
108 | $logger = $this->authenticationLogger->forAuthentication($expectedInResponseTo); |
||
109 | |||
110 | $logger->notice('No authenticated user and AuthnRequest pending, attempting to process SamlResponse'); |
||
111 | |||
112 | try { |
||
113 | $assertion = $this->samlInteractionProvider->processSamlResponse($event->getRequest()); |
||
114 | } catch (AuthnFailedSamlResponseException $exception) { |
||
115 | $logger->notice(sprintf('SAML Authentication failed at IdP: "%s"', $exception->getMessage())); |
||
116 | $responseBody = $this->templating->render( |
||
117 | 'SurfnetStepupSelfServiceSelfServiceBundle:Saml/Exception:authnFailed.html.twig', |
||
118 | ['exception' => $exception] |
||
119 | ); |
||
120 | |||
121 | $event->setResponse(new Response($responseBody, Response::HTTP_UNAUTHORIZED)); |
||
122 | |||
123 | return; |
||
124 | } catch (PreconditionNotMetException $exception) { |
||
125 | $logger->notice(sprintf('SAMLResponse precondition not met: "%s"', $exception->getMessage())); |
||
126 | $responseBody = $this->templating->render( |
||
127 | 'SurfnetStepupSelfServiceSelfServiceBundle:Saml/Exception:preconditionNotMet.html.twig', |
||
128 | ['exception' => $exception] |
||
129 | ); |
||
130 | |||
131 | $event->setResponse(new Response($responseBody, Response::HTTP_UNAUTHORIZED)); |
||
132 | |||
133 | return; |
||
134 | } catch (Exception $exception) { |
||
135 | $logger->error(sprintf('Failed SAMLResponse Parsing: "%s"', $exception->getMessage())); |
||
136 | |||
137 | throw new AuthenticationException('Failed SAMLResponse parsing', 0, $exception); |
||
138 | } |
||
139 | |||
140 | if (!InResponseTo::assertEquals($assertion, $expectedInResponseTo)) { |
||
141 | $logger->error('Unknown or unexpected InResponseTo in SAMLResponse'); |
||
142 | |||
143 | throw new AuthenticationException('Unknown or unexpected InResponseTo in SAMLResponse'); |
||
144 | } |
||
145 | |||
146 | $logger->notice('Successfully processed SAMLResponse, attempting to authenticate'); |
||
147 | |||
148 | $token = new SamlToken(); |
||
149 | $token->assertion = $assertion; |
||
150 | |||
151 | try { |
||
152 | $authToken = $this->authenticationManager->authenticate($token); |
||
153 | } catch (AuthenticationException $failed) { |
||
154 | $logger->error(sprintf('Authentication Failed, reason: "%s"', $failed->getMessage())); |
||
155 | |||
156 | // By default deny authorization |
||
157 | $event->setResponse(new Response('', Response::HTTP_FORBIDDEN)); |
||
158 | |||
159 | return; |
||
160 | } |
||
161 | |||
162 | $this->tokenStorage->setToken($authToken); |
||
163 | |||
164 | // migrate the session to prevent session hijacking |
||
165 | $this->authenticatedSession->migrate(); |
||
166 | |||
167 | $event->setResponse(new RedirectResponse($this->authenticatedSession->getCurrentRequestUri())); |
||
168 | |||
169 | $logger->notice('Authentication succeeded, redirecting to original location'); |
||
170 | |||
171 | return; |
||
172 | } |
||
173 | |||
174 | if ($this->nextHandler) { |
||
175 | $this->nextHandler->process($event); |
||
176 | } |
||
177 | } |
||
178 | |||
184 |
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.