Conditions | 8 |
Paths | 7 |
Total Lines | 58 |
Code Lines | 30 |
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 |
||
74 | public function samlValidate( |
||
75 | Request $request, |
||
76 | #[MapQueryParameter] string $TARGET, |
||
77 | ): XmlResponse { |
||
78 | // From SAML2\SOAP::receive() |
||
79 | $postBody = $request->getContent(); |
||
80 | if (empty($postBody)) { |
||
81 | throw new \RuntimeException('samlValidate expects a soap body.'); |
||
82 | } |
||
83 | |||
84 | // SAML request values |
||
85 | // |
||
86 | // samlp:Request |
||
87 | // - RequestID [REQUIRED] - unique identifier for the request |
||
88 | // - IssueInstant [REQUIRED] - timestamp of the request |
||
89 | // samlp:AssertionArtifact [REQUIRED] - the valid CAS Service |
||
90 | |||
91 | $ticketParser = xml_parser_create(); |
||
92 | xml_parser_set_option($ticketParser, XML_OPTION_CASE_FOLDING, 0); |
||
93 | xml_parser_set_option($ticketParser, XML_OPTION_SKIP_WHITE, 1); |
||
94 | xml_parse_into_struct($ticketParser, $postBody, $values, $tags); |
||
95 | xml_parser_free($ticketParser); |
||
96 | |||
97 | // Check for the required saml attributes |
||
98 | $samlRequestAttributes = $values[ $tags['samlp:Request'][0] ]['attributes']; |
||
99 | if (!isset($samlRequestAttributes['RequestID'])) { |
||
100 | throw new \RuntimeException('Missing RequestID samlp:Request attribute.'); |
||
101 | } elseif (!isset($samlRequestAttributes['IssueInstant'])) { |
||
102 | throw new \RuntimeException('Missing IssueInstant samlp:Request attribute.'); |
||
103 | } |
||
104 | |||
105 | if ( |
||
106 | !isset($tags['samlp:AssertionArtifact']) |
||
107 | || empty($values[$tags['samlp:AssertionArtifact'][0]]['value']) |
||
108 | ) { |
||
109 | throw new \RuntimeException('Missing ticketId in AssertionArtifact'); |
||
110 | } |
||
111 | |||
112 | $ticketId = $values[$tags['samlp:AssertionArtifact'][0]]['value']; |
||
113 | Logger::debug('samlvalidate: Checking ticket ' . $ticketId); |
||
114 | |||
115 | try { |
||
116 | // validateAndDeleteTicket might throw a CasException. In order to avoid third party modules |
||
117 | // dependencies, we will catch and rethrow the Exception. |
||
118 | $ticket = $this->ticketValidator->validateAndDeleteTicket($ticketId, $TARGET); |
||
119 | } catch (\Exception $e) { |
||
120 | throw new \RuntimeException($e->getMessage()); |
||
121 | } |
||
122 | if (!\is_array($ticket)) { |
||
123 | throw new \RuntimeException('Error loading ticket'); |
||
124 | } |
||
125 | |||
126 | $response = $this->validateResponder->convertToSaml($ticket); |
||
127 | $soap = $this->validateResponder->wrapInSoap($response); |
||
128 | |||
129 | return new XmlResponse( |
||
130 | (string)$soap, |
||
131 | Response::HTTP_OK, |
||
132 | ); |
||
135 |