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 |
||
| 39 | class GatewayController extends Controller |
||
| 40 | { |
||
| 41 | const RESPONSE_CONTEXT_SERVICE_ID = 'gateway.proxy.response_context'; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Receive an AuthnRequest from a service provider. |
||
| 45 | * |
||
| 46 | * The service provider is either a Stepup component (SelfService, RA) or |
||
| 47 | * an external service provider. |
||
| 48 | * |
||
| 49 | * This single sign-on action will start a new SAML request to the remote |
||
| 50 | * IDP configured in Stepup (most likely to be an instance of OpenConext |
||
| 51 | * EngineBlock). |
||
| 52 | * |
||
| 53 | * @param Request $httpRequest |
||
| 54 | * @return \Symfony\Component\HttpFoundation\RedirectResponse|Response |
||
| 55 | */ |
||
| 56 | public function ssoAction(Request $httpRequest) |
||
| 57 | { |
||
| 58 | /** @var \Psr\Log\LoggerInterface $logger */ |
||
| 59 | $logger = $this->get('logger'); |
||
| 60 | |||
| 61 | $redirectBinding = $this->get('surfnet_saml.http.redirect_binding'); |
||
| 62 | $gatewayLoginService = $this->getGatewayLoginService(); |
||
| 63 | |||
| 64 | $logger->notice('Received AuthnRequest, started processing'); |
||
| 65 | |||
| 66 | try { |
||
| 67 | $proxyRequest = $gatewayLoginService->singleSignOn($httpRequest); |
||
| 68 | } catch (RequesterFailureException $e) { |
||
| 69 | $response = $this->getGatewayFailedResponseService()->createRequesterFailureResponse($this->getResponseContext()); |
||
|
|
|||
| 70 | |||
| 71 | return $this->renderSamlResponse('consumeAssertion', $response); |
||
| 72 | } |
||
| 73 | |||
| 74 | return $redirectBinding->createResponseFor($proxyRequest); |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * |
||
| 79 | */ |
||
| 80 | public function proxySsoAction() |
||
| 81 | { |
||
| 82 | throw new HttpException(418, 'Not Yet Implemented'); |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Receive an AuthnResponse from an identity provider. |
||
| 87 | * |
||
| 88 | * The AuthnRequest started in ssoAction() resulted in an AuthnResponse |
||
| 89 | * from the IDP. This method handles the assertion and forwards the user |
||
| 90 | * using an internal redirect to the SecondFactorController to start the |
||
| 91 | * actual second factor verification. |
||
| 92 | * |
||
| 93 | * @param Request $request |
||
| 94 | * @return \Symfony\Component\HttpFoundation\Response |
||
| 95 | */ |
||
| 96 | public function consumeAssertionAction(Request $request) |
||
| 97 | { |
||
| 98 | $responseContext = $this->getResponseContext(); |
||
| 99 | $gatewayLoginService = $this->getGatewayConsumeAssertionService(); |
||
| 100 | |||
| 101 | try { |
||
| 102 | $gatewayLoginService->consumeAssertion($request, $responseContext); |
||
| 103 | } catch (ResponseFailureException $e) { |
||
| 104 | $response = $this->getGatewayFailedResponseService()->createResponseFailureResponse($responseContext); |
||
| 105 | |||
| 106 | return $this->renderSamlResponse('unprocessableResponse', $response); |
||
| 107 | } |
||
| 108 | |||
| 109 | return $this->forward('SurfnetStepupGatewayGatewayBundle:SecondFactor:selectSecondFactorForVerification'); |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Send a SAML response back to the service provider. |
||
| 114 | * |
||
| 115 | * Second factor verification handled by SecondFactorController is |
||
| 116 | * finished. The user was forwarded back to this action with an internal |
||
| 117 | * redirect. This method sends a AuthnResponse back to the service |
||
| 118 | * provider in response to the AuthnRequest received in ssoAction(). |
||
| 119 | */ |
||
| 120 | public function respondAction() |
||
| 121 | { |
||
| 122 | $responseContext = $this->getResponseContext(); |
||
| 123 | $gatewayLoginService = $this->getGatewayRespondService(); |
||
| 124 | |||
| 125 | $response = $gatewayLoginService->respond($responseContext); |
||
| 126 | $gatewayLoginService->resetRespondState($responseContext); |
||
| 127 | |||
| 128 | return $this->renderSamlResponse('consumeAssertion', $response); |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @return Response |
||
| 133 | */ |
||
| 134 | View Code Duplication | public function sendLoaCannotBeGivenAction() |
|
| 143 | |||
| 144 | /** |
||
| 145 | * @return Response |
||
| 146 | */ |
||
| 147 | View Code Duplication | public function sendAuthenticationCancelledByUserAction() |
|
| 156 | |||
| 157 | /** |
||
| 158 | * @param string $view |
||
| 159 | * @param SAMLResponse $response |
||
| 160 | * @return Response |
||
| 161 | */ |
||
| 162 | View Code Duplication | public function renderSamlResponse($view, SAMLResponse $response) |
|
| 172 | |||
| 173 | /** |
||
| 174 | * @param string $view |
||
| 175 | * @param array $parameters |
||
| 176 | * @param Response $response |
||
| 177 | * @return Response |
||
| 178 | */ |
||
| 179 | public function render($view, array $parameters = array(), Response $response = null) |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @return \Surfnet\StepupGateway\GatewayBundle\Saml\ResponseContext |
||
| 190 | */ |
||
| 191 | public function getResponseContext() |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @param SAMLResponse $response |
||
| 206 | * @return string |
||
| 207 | */ |
||
| 208 | private function getResponseAsXML(SAMLResponse $response) |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @return LoginService |
||
| 215 | */ |
||
| 216 | private function getGatewayLoginService() |
||
| 220 | |||
| 221 | /** |
||
| 222 | * @return ConsumeAssertionService |
||
| 223 | */ |
||
| 224 | private function getGatewayConsumeAssertionService() |
||
| 228 | |||
| 229 | /** |
||
| 230 | * @return RespondService |
||
| 231 | */ |
||
| 232 | private function getGatewayRespondService() |
||
| 236 | |||
| 237 | /** |
||
| 238 | * @return FailedResponseService |
||
| 239 | */ |
||
| 240 | private function getGatewayFailedResponseService() |
||
| 244 | } |
||
| 245 |
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.