Conditions | 8 |
Paths | 7 |
Total Lines | 57 |
Code Lines | 29 |
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 |
||
77 | public function samlValidate( |
||
78 | Request $request, |
||
79 | #[MapQueryParameter] string $TARGET, |
||
80 | ): XmlResponse { |
||
81 | // From SAML2\SOAP::receive() |
||
82 | $postBody = $request->getContent(); |
||
83 | if (empty($postBody)) { |
||
84 | throw new \RuntimeException('samlValidate expects a soap body.'); |
||
85 | } |
||
86 | |||
87 | // SAML request values |
||
88 | // |
||
89 | // samlp:Request |
||
90 | // - RequestID [REQUIRED] - unique identifier for the request |
||
91 | // - IssueInstant [REQUIRED] - timestamp of the request |
||
92 | // samlp:AssertionArtifact [REQUIRED] - the valid CAS Service |
||
93 | |||
94 | $documentBody = DOMDocumentFactory::fromString($postBody); |
||
95 | $xPath = new DOMXpath($documentBody); |
||
96 | $xPath->registerNamespace('soap-env', 'http://schemas.xmlsoap.org/soap/envelope/'); |
||
97 | $samlRequestAttributes = $xPath->query('/soap-env:Envelope/soap-env:Body/*'); |
||
98 | |||
99 | // Check for the required saml attributes |
||
100 | if (!$samlRequestAttributes->item(0)->hasAttribute('RequestID')) { |
||
101 | throw new \RuntimeException('Missing RequestID samlp:Request attribute.'); |
||
102 | } elseif (!$samlRequestAttributes->item(0)->hasAttribute('IssueInstant')) { |
||
103 | throw new \RuntimeException('Missing IssueInstant samlp:Request attribute.'); |
||
104 | } |
||
105 | |||
106 | $assertionArtifactNode = $samlRequestAttributes->item(0)->getElementsByTagName('AssertionArtifact'); |
||
107 | if ( |
||
108 | $assertionArtifactNode->count() === 0 |
||
109 | || empty($assertionArtifactNode->item(0)->nodeValue) |
||
110 | ) { |
||
111 | throw new \RuntimeException('Missing ticketId in AssertionArtifact'); |
||
112 | } |
||
113 | |||
114 | $ticketId = $assertionArtifactNode->item(0)->nodeValue; |
||
115 | Logger::debug('samlvalidate: Checking ticket ' . $ticketId); |
||
116 | |||
117 | try { |
||
118 | // validateAndDeleteTicket might throw a CasException. In order to avoid third party modules |
||
119 | // dependencies, we will catch and rethrow the Exception. |
||
120 | $ticket = $this->ticketValidator->validateAndDeleteTicket($ticketId, $TARGET); |
||
121 | } catch (\Exception $e) { |
||
122 | throw new \RuntimeException($e->getMessage()); |
||
123 | } |
||
124 | if (!\is_array($ticket)) { |
||
125 | throw new \RuntimeException('Error loading ticket'); |
||
126 | } |
||
127 | |||
128 | $response = $this->validateResponder->convertToSaml($ticket); |
||
129 | $soap = $this->validateResponder->wrapInSoap($response); |
||
130 | |||
131 | return new XmlResponse( |
||
132 | (string)$soap, |
||
133 | Response::HTTP_OK, |
||
134 | ); |
||
137 |