| Conditions | 19 | 
| Paths | 53 | 
| Total Lines | 121 | 
| Code Lines | 72 | 
| 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 | ||
| 229 | public function validate( | ||
| 230 | Request $request, | ||
| 231 | string $method, | ||
| 232 | string $target, | ||
| 233 | bool $renew = false, | ||
| 234 | ?string $ticket = null, | ||
| 235 | ?string $service = null, | ||
| 236 | ?string $pgtUrl = null, | ||
| 237 |     ): XmlResponse { | ||
| 238 | $forceAuthn = $renew; | ||
| 239 | // todo: According to the protocol, there is no target??? Why are we using it? | ||
| 240 | $serviceUrl = $service ?? $target ?? null; | ||
| 241 | |||
| 242 | // Check if any of the required query parameters are missing | ||
| 243 |         if ($serviceUrl === null || $ticket === null) { | ||
| 244 | $messagePostfix = $serviceUrl === null ? 'service' : 'ticket'; | ||
| 245 |             $message        = "casserver: Missing service parameter: [{$messagePostfix}]"; | ||
| 246 | Logger::debug($message); | ||
| 247 | |||
| 248 | return new XmlResponse( | ||
| 249 | (string)$this->cas20Protocol->getValidateFailureResponse(C::ERR_INVALID_SERVICE, $message), | ||
| 250 | Response::HTTP_BAD_REQUEST, | ||
| 251 | ); | ||
| 252 | } | ||
| 253 | |||
| 254 |         try { | ||
| 255 | // Get the service ticket | ||
| 256 | // `getTicket` uses the unserializable method and Objects may throw Throwables in their | ||
| 257 | // unserialization handlers. | ||
| 258 | $serviceTicket = $this->ticketStore->getTicket($ticket); | ||
| 259 | // Delete the ticket | ||
| 260 | $this->ticketStore->deleteTicket($ticket); | ||
| 261 |         } catch (\Exception $e) { | ||
| 262 | $message = 'casserver:serviceValidate: internal server error. ' . var_export($e->getMessage(), true); | ||
| 263 | Logger::error($message); | ||
| 264 | |||
| 265 | return new XmlResponse( | ||
| 266 | (string)$this->cas20Protocol->getValidateFailureResponse(C::ERR_INVALID_SERVICE, $message), | ||
| 267 | Response::HTTP_INTERNAL_SERVER_ERROR, | ||
| 268 | ); | ||
| 269 | } | ||
| 270 | |||
| 271 | $failed = false; | ||
| 272 | $message = ''; | ||
| 273 |         if (empty($serviceTicket)) { | ||
| 274 | // No ticket | ||
| 275 | $message = 'ticket: ' . var_export($ticket, true) . ' not recognized'; | ||
| 276 | $failed = true; | ||
| 277 |         } elseif ($method === 'serviceValidate' && $this->ticketFactory->isProxyTicket($serviceTicket)) { | ||
| 278 | $message = 'Ticket ' . var_export($_GET['ticket'], true) . | ||
| 279 | ' is a proxy ticket. Use proxyValidate instead.'; | ||
| 280 | $failed = true; | ||
| 281 |         } elseif (!$this->ticketFactory->isServiceTicket($serviceTicket)) { | ||
| 282 | // This is not a service ticket | ||
| 283 | $message = 'ticket: ' . var_export($ticket, true) . ' is not a service ticket'; | ||
| 284 | $failed = true; | ||
| 285 |         } elseif ($this->ticketFactory->isExpired($serviceTicket)) { | ||
| 286 | // the ticket has expired | ||
| 287 | $message = 'Ticket has ' . var_export($ticket, true) . ' expired'; | ||
| 288 | $failed = true; | ||
| 289 |         } elseif ($this->sanitize($serviceTicket['service']) !== $this->sanitize($serviceUrl)) { | ||
| 290 | // The service url we passed to the query parameters does not match the one in the ticket. | ||
| 291 | $message = 'Mismatching service parameters: expected ' . | ||
| 292 | var_export($serviceTicket['service'], true) . | ||
| 293 | ' but was: ' . var_export($serviceUrl, true); | ||
| 294 | $failed = true; | ||
| 295 |         } elseif ($forceAuthn && !$serviceTicket['forceAuthn']) { | ||
| 296 | // If `forceAuthn` is required but not set in the ticket | ||
| 297 | $message = 'Ticket was issued from single sign on session'; | ||
| 298 | $failed = true; | ||
| 299 | } | ||
| 300 | |||
| 301 |         if ($failed) { | ||
| 302 | $finalMessage = 'casserver:validate: ' . $message; | ||
| 303 | Logger::error($finalMessage); | ||
| 304 | |||
| 305 | return new XmlResponse( | ||
| 306 | (string)$this->cas20Protocol->getValidateFailureResponse(C::ERR_INVALID_SERVICE, $message), | ||
| 307 | Response::HTTP_BAD_REQUEST, | ||
| 308 | ); | ||
| 309 | } | ||
| 310 | |||
| 311 | $attributes = $serviceTicket['attributes']; | ||
| 312 | $this->cas20Protocol->setAttributes($attributes); | ||
| 313 | |||
| 314 |         if (isset($pgtUrl)) { | ||
| 315 | $sessionTicket = $this->ticketStore->getTicket($serviceTicket['sessionId']); | ||
| 316 | if ( | ||
| 317 | $sessionTicket !== null | ||
| 318 | && $this->ticketFactory->isSessionTicket($sessionTicket) | ||
| 319 | && !$this->ticketFactory->isExpired($sessionTicket) | ||
| 320 |             ) { | ||
| 321 | $proxyGrantingTicket = $this->ticketFactory->createProxyGrantingTicket( | ||
| 322 | [ | ||
| 323 | 'userName' => $serviceTicket['userName'], | ||
| 324 | 'attributes' => $attributes, | ||
| 325 | 'forceAuthn' => false, | ||
| 326 | 'proxies' => array_merge( | ||
| 327 | [$serviceUrl], | ||
| 328 | $serviceTicket['proxies'], | ||
| 329 | ), | ||
| 330 | 'sessionId' => $serviceTicket['sessionId'], | ||
| 331 | ], | ||
| 332 | ); | ||
| 333 |                 try { | ||
| 334 | $this->httpUtils->fetch( | ||
| 335 | $pgtUrl . '?pgtIou=' . $proxyGrantingTicket['iou'] . '&pgtId=' . $proxyGrantingTicket['id'], | ||
| 336 | ); | ||
| 337 | |||
| 338 | $this->cas20Protocol->setProxyGrantingTicketIOU($proxyGrantingTicket['iou']); | ||
| 339 | |||
| 340 | $this->ticketStore->addTicket($proxyGrantingTicket); | ||
| 341 |                 } catch (\Exception $e) { | ||
| 342 | // Fall through | ||
| 343 | } | ||
| 344 | } | ||
| 345 | } | ||
| 346 | |||
| 347 | return new XmlResponse( | ||
| 348 | (string)$this->cas20Protocol->getValidateSuccessResponse($serviceTicket['userName']), | ||
| 349 | Response::HTTP_OK, | ||
| 350 | ); | ||
| 353 |