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