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