Conditions | 6 |
Paths | 5 |
Total Lines | 51 |
Code Lines | 23 |
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 |
||
81 | public function samlValidate( |
||
82 | Request $request, |
||
83 | #[MapQueryParameter] string $TARGET, |
||
84 | ): XmlResponse { |
||
85 | // From SAML2\SOAP::receive() |
||
86 | $postBody = $request->getContent(); |
||
87 | if (empty($postBody)) { |
||
88 | throw new \RuntimeException('samlValidate expects a soap body.'); |
||
89 | } |
||
90 | |||
91 | // SAML request values |
||
92 | // |
||
93 | // samlp:Request |
||
94 | // - RequestID [REQUIRED] - unique identifier for the request |
||
95 | // - IssueInstant [REQUIRED] - timestamp of the request |
||
96 | // samlp:AssertionArtifact [REQUIRED] - the valid CAS Service |
||
97 | |||
98 | $documentBody = DOMDocumentFactory::fromString($postBody); |
||
99 | $envelope = Envelope::fromXML($documentBody->documentElement); |
||
100 | |||
101 | // The SOAP Envelope must have only one ticket |
||
102 | $elements = $envelope->getBody()->getElements(); |
||
103 | if (count($elements) > 1 || count($elements) < 1) { |
||
104 | throw new ProtocolViolationException('samlValidate expects a soap body with only one ticket.'); |
||
105 | } |
||
106 | |||
107 | // Request Element |
||
108 | $samlpRequestParsed = SamlRequest::fromXML($elements[0]->getXML()); |
||
109 | // Assertion Artifact Element |
||
110 | $assertionArtifactParsed = $samlpRequestParsed->getRequest()[0]; |
||
111 | |||
112 | $ticketId = $assertionArtifactParsed->getContent(); |
||
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 |