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