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