| Conditions | 13 | 
| Paths | 22 | 
| Total Lines | 88 | 
| Code Lines | 50 | 
| Lines | 0 | 
| Ratio | 0 % | 
| Changes | 3 | ||
| 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 | ||
| 78 | public function validate( | ||
| 79 | Request $request, | ||
| 80 | #[MapQueryParameter] ?string $ticket = null, | ||
| 81 | #[MapQueryParameter] bool $renew = false, | ||
| 82 | #[MapQueryParameter] ?string $service = null, | ||
| 83 |     ): Response { | ||
| 84 | $forceAuthn = $renew; | ||
| 85 | // Check if any of the required query parameters are missing | ||
| 86 | // Even though we can delegate the check to Symfony's `MapQueryParameter` we cannot return | ||
| 87 | // the failure response needed. As a result, we allow a default value, and we handle the missing | ||
| 88 | // values afterwards. | ||
| 89 |         if ($service === null || $ticket === null) { | ||
| 90 | $messagePostfix = $service === null ? 'service' : 'ticket'; | ||
| 91 |             Logger::debug("casserver: Missing service parameter: [{$messagePostfix}]"); | ||
| 92 | return new Response( | ||
| 93 | $this->cas10Protocol->getValidateFailureResponse(), | ||
| 94 | Response::HTTP_BAD_REQUEST, | ||
| 95 | ); | ||
| 96 | } | ||
| 97 | |||
| 98 |         try { | ||
| 99 | // Get the service ticket | ||
| 100 | // `getTicket` uses the unserializable method and Objects may throw Throwables in their | ||
| 101 | // unserialization handlers. | ||
| 102 | $serviceTicket = $this->ticketStore->getTicket($ticket); | ||
| 103 | // Delete the ticket | ||
| 104 | $this->ticketStore->deleteTicket($ticket); | ||
| 105 |         } catch (\Exception $e) { | ||
| 106 |             Logger::error('casserver:validate: internal server error. ' . var_export($e->getMessage(), true)); | ||
| 107 | return new Response( | ||
| 108 | $this->cas10Protocol->getValidateFailureResponse(), | ||
| 109 | Response::HTTP_INTERNAL_SERVER_ERROR, | ||
| 110 | ); | ||
| 111 | } | ||
| 112 | |||
| 113 | $failed = false; | ||
| 114 | $message = ''; | ||
| 115 |         if (empty($serviceTicket)) { | ||
| 116 | // No ticket | ||
| 117 | $message = 'ticket: ' . var_export($ticket, true) . ' not recognized'; | ||
| 118 | $failed = true; | ||
| 119 |         } elseif (!$this->ticketFactory->isServiceTicket($serviceTicket)) { | ||
| 120 | // This is not a service ticket | ||
| 121 | $message = 'ticket: ' . var_export($ticket, true) . ' is not a service ticket'; | ||
| 122 | $failed = true; | ||
| 123 |         } elseif ($this->ticketFactory->isExpired($serviceTicket)) { | ||
| 124 | // the ticket has expired | ||
| 125 | $message = 'Ticket has ' . var_export($ticket, true) . ' expired'; | ||
| 126 | $failed = true; | ||
| 127 |         } elseif ($this->sanitize($serviceTicket['service']) !== $this->sanitize($service)) { | ||
| 128 | // The service url we passed to the query parameters does not match the one in the ticket. | ||
| 129 | $message = 'Mismatching service parameters: expected ' . | ||
| 130 | var_export($serviceTicket['service'], true) . | ||
| 131 | ' but was: ' . var_export($service, true); | ||
| 132 | $failed = true; | ||
| 133 |         } elseif ($forceAuthn && !$serviceTicket['forceAuthn']) { | ||
| 134 | // If `forceAuthn` is required but not set in the ticket | ||
| 135 | $message = 'Ticket was issued from single sign on session'; | ||
| 136 | $failed = true; | ||
| 137 | } | ||
| 138 | |||
| 139 |         if ($failed) { | ||
| 140 |             Logger::error('casserver:validate: ' . $message); | ||
| 141 | return new Response( | ||
| 142 | $this->cas10Protocol->getValidateFailureResponse(), | ||
| 143 | Response::HTTP_BAD_REQUEST, | ||
| 144 | ); | ||
| 145 | } | ||
| 146 | |||
| 147 | // Get the username field | ||
| 148 |         $usernameField = $this->casConfig->getOptionalValue('attrname', 'eduPersonPrincipalName'); | ||
| 149 | |||
| 150 | // Fail if the username field is not present in the attribute list | ||
| 151 |         if (!\array_key_exists($usernameField, $serviceTicket['attributes'])) { | ||
| 152 | Logger::error( | ||
| 153 | 'casserver:validate: internal server error. Missing user name attribute: ' | ||
| 154 | . var_export($usernameField, true), | ||
| 155 | ); | ||
| 156 | return new Response( | ||
| 157 | $this->cas10Protocol->getValidateFailureResponse(), | ||
| 158 | Response::HTTP_BAD_REQUEST, | ||
| 159 | ); | ||
| 160 | } | ||
| 161 | |||
| 162 | // Successful validation | ||
| 163 | return new Response( | ||
| 164 | $this->cas10Protocol->getValidateSuccessResponse($serviceTicket['attributes'][$usernameField][0]), | ||
| 165 | Response::HTTP_OK, | ||
| 166 | ); | ||
| 179 |