Conditions | 9 |
Paths | 22 |
Total Lines | 83 |
Code Lines | 54 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 |
||
132 | private function downloadMetadata(): EntitiesDescriptor|EntityDescriptor|null |
||
133 | { |
||
134 | Logger::debug($this->logLoc . 'Downloading metadata from ' . var_export($this->url, true)); |
||
135 | $configUtils = new Utils\Config(); |
||
136 | |||
137 | $context = ['ssl' => []]; |
||
138 | if ($this->sslCAFile !== null) { |
||
139 | $context['ssl']['cafile'] = $configUtils->getCertPath($this->sslCAFile); |
||
140 | Logger::debug( |
||
141 | $this->logLoc . 'Validating https connection against CA certificate(s) found in ' . |
||
142 | var_export($context['ssl']['cafile'], true), |
||
143 | ); |
||
144 | $context['ssl']['verify_peer'] = true; |
||
145 | $context['ssl']['CN_match'] = parse_url($this->url, PHP_URL_HOST); |
||
146 | } |
||
147 | |||
148 | try { |
||
149 | $httpUtils = new Utils\HTTP(); |
||
150 | $data = $httpUtils->fetch($this->url, $context, false); |
||
151 | } catch (Error\Exception $e) { |
||
152 | Logger::error($this->logLoc . 'Unable to load metadata from ' . var_export($this->url, true)); |
||
153 | return null; |
||
154 | } |
||
155 | |||
156 | $doc = DOMDocumentFactory::create(); |
||
157 | /** @var string $data */ |
||
158 | $res = $doc->loadXML($data); |
||
159 | if (!$res) { |
||
160 | Logger::error($this->logLoc . 'Error parsing XML from ' . var_export($this->url, true)); |
||
161 | return null; |
||
162 | } |
||
163 | |||
164 | /** @psalm-var \DOMElement[] $root */ |
||
165 | $root = XPath::xpQuery( |
||
166 | $doc->documentElement, |
||
167 | '/saml_metadata:EntityDescriptor|/saml_metadata:EntitiesDescriptor', |
||
168 | XPath::getXPath($doc->documentElement), |
||
169 | ); |
||
170 | |||
171 | if (count($root) === 0) { |
||
172 | Logger::error( |
||
173 | $this->logLoc . 'No <EntityDescriptor> or <EntitiesDescriptor> in metadata from ' . |
||
174 | var_export($this->url, true), |
||
175 | ); |
||
176 | return null; |
||
177 | } |
||
178 | |||
179 | if (count($root) > 1) { |
||
180 | Logger::error( |
||
181 | $this->logLoc . 'More than one <EntityDescriptor> or <EntitiesDescriptor> in metadata from ' . |
||
182 | var_export($this->url, true), |
||
183 | ); |
||
184 | return null; |
||
185 | } |
||
186 | |||
187 | $root = $root[0]; |
||
188 | try { |
||
189 | if ($root->localName === 'EntityDescriptor') { |
||
190 | $md = EntityDescriptor::fromXML($root); |
||
191 | } else { |
||
192 | $md = EntitiesDescriptor::fromXML($root); |
||
193 | } |
||
194 | } catch (Exception $e) { |
||
195 | Logger::error( |
||
196 | $this->logLoc . 'Unable to parse metadata from ' . |
||
197 | var_export($this->url, true) . ': ' . $e->getMessage(), |
||
198 | ); |
||
199 | return null; |
||
200 | } |
||
201 | |||
202 | if ($this->certificate !== null) { |
||
203 | $file = $configUtils->getCertPath($this->certificate); |
||
204 | $verifier = (new SignatureAlgorithmFactory())->getAlgorithm( |
||
205 | $md->getSignature()->getSignedInfo()->getSignatureMethod()->getAlgorithm(), |
||
206 | PublicKey::fromFile($file), |
||
207 | ); |
||
208 | |||
209 | /** @var \SimpleSAML\SAML2\XML\md\EntitiesDescriptor|\SimpleSAML\SAML2\XML\md\EntityDescriptor $md */ |
||
210 | $md = $md->verify($verifier); |
||
211 | Logger::debug($this->logLoc . 'Validated signature on metadata from ' . var_export($this->url, true)); |
||
212 | } |
||
213 | |||
214 | return $md; |
||
215 | } |
||
289 |