| Conditions | 6 |
| Paths | 4 |
| Total Lines | 74 |
| Code Lines | 41 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 121 | public function proxy( |
||
| 122 | Request $request, |
||
| 123 | #[MapQueryParameter] ?string $targetService = null, |
||
| 124 | #[MapQueryParameter] ?string $pgt = null, |
||
| 125 | ): XmlResponse { |
||
| 126 | $legal_target_service_urls = $this->casConfig->getOptionalValue('legal_target_service_urls', []); |
||
| 127 | // Fail if |
||
| 128 | $message = match (true) { |
||
| 129 | // targetService parameter is not defined |
||
| 130 | $targetService === null => 'Missing target service parameter [targetService]', |
||
| 131 | // pgt parameter is not defined |
||
| 132 | $pgt === null => 'Missing proxy granting ticket parameter: [pgt]', |
||
| 133 | !$this->checkServiceURL($this->sanitize($targetService), $legal_target_service_urls) => |
||
| 134 | "Target service parameter not listed as a legal service: [targetService] = {$targetService}", |
||
| 135 | default => null, |
||
| 136 | }; |
||
| 137 | |||
| 138 | if (!empty($message)) { |
||
| 139 | return new XmlResponse( |
||
| 140 | (string)$this->cas20Protocol->getValidateFailureResponse(C::ERR_INVALID_REQUEST, $message), |
||
| 141 | Response::HTTP_BAD_REQUEST, |
||
| 142 | ); |
||
| 143 | } |
||
| 144 | |||
| 145 | // Get the ticket |
||
| 146 | $proxyGrantingTicket = $this->ticketStore->getTicket($pgt); |
||
| 147 | $message = match (true) { |
||
| 148 | // targetService parameter is not defined |
||
| 149 | $proxyGrantingTicket === null => "Ticket {$pgt} not recognized", |
||
| 150 | // pgt parameter is not defined |
||
| 151 | !$this->ticketFactory->isProxyGrantingTicket($proxyGrantingTicket) |
||
| 152 | => "Not a valid proxy granting ticket id: {$pgt}", |
||
| 153 | default => null, |
||
| 154 | }; |
||
| 155 | |||
| 156 | if (!empty($message)) { |
||
| 157 | return new XmlResponse( |
||
| 158 | (string)$this->cas20Protocol->getValidateFailureResponse('BAD_PGT', $message), |
||
| 159 | Response::HTTP_BAD_REQUEST, |
||
| 160 | ); |
||
| 161 | } |
||
| 162 | |||
| 163 | // Get the session id from the ticket |
||
| 164 | $sessionTicket = $this->ticketStore->getTicket($proxyGrantingTicket['sessionId']); |
||
| 165 | |||
| 166 | if ( |
||
| 167 | $sessionTicket === null |
||
| 168 | || $this->ticketFactory->isSessionTicket($sessionTicket) === false |
||
| 169 | || $this->ticketFactory->isExpired($sessionTicket) |
||
| 170 | ) { |
||
| 171 | $message = "Ticket {$pgt} has expired"; |
||
| 172 | Logger::debug('casserver:' . $message); |
||
| 173 | |||
| 174 | return new XmlResponse( |
||
| 175 | (string)$this->cas20Protocol->getValidateFailureResponse('BAD_PGT', $message), |
||
| 176 | Response::HTTP_BAD_REQUEST, |
||
| 177 | ); |
||
| 178 | } |
||
| 179 | |||
| 180 | $proxyTicket = $this->ticketFactory->createProxyTicket( |
||
| 181 | [ |
||
| 182 | 'service' => $targetService, |
||
| 183 | 'forceAuthn' => $proxyGrantingTicket['forceAuthn'], |
||
| 184 | 'attributes' => $proxyGrantingTicket['attributes'], |
||
| 185 | 'proxies' => $proxyGrantingTicket['proxies'], |
||
| 186 | 'sessionId' => $proxyGrantingTicket['sessionId'], |
||
| 187 | ], |
||
| 188 | ); |
||
| 189 | |||
| 190 | $this->ticketStore->addTicket($proxyTicket); |
||
| 191 | |||
| 192 | return new XmlResponse( |
||
| 193 | (string)$this->cas20Protocol->getProxySuccessResponse($proxyTicket['id']), |
||
| 194 | Response::HTTP_OK, |
||
| 195 | ); |
||
| 239 |