| Conditions | 11 |
| Paths | 28 |
| Total Lines | 73 |
| Code Lines | 49 |
| 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 |
||
| 117 | private function downloadMetadata() |
||
| 118 | { |
||
| 119 | Logger::debug($this->logLoc.'Downloading metadata from '.var_export($this->url, true)); |
||
| 120 | |||
| 121 | $context = ['ssl' => []]; |
||
| 122 | if ($this->sslCAFile !== null) { |
||
| 123 | $context['ssl']['cafile'] = Config::getCertPath($this->sslCAFile); |
||
| 124 | Logger::debug($this->logLoc.'Validating https connection against CA certificate(s) found in '. |
||
| 125 | var_export($context['ssl']['cafile'], true)); |
||
| 126 | $context['ssl']['verify_peer'] = true; |
||
| 127 | $context['ssl']['CN_match'] = parse_url($this->url, PHP_URL_HOST); |
||
| 128 | } |
||
| 129 | |||
| 130 | try { |
||
| 131 | $data = HTTP::fetch($this->url, $context, false); |
||
| 132 | } catch (\SimpleSAML\Error\Exception $e) { |
||
| 133 | Logger::error($this->logLoc.'Unable to load metadata from '.var_export($this->url, true)); |
||
| 134 | return null; |
||
| 135 | } |
||
| 136 | |||
| 137 | $doc = new \DOMDocument(); |
||
| 138 | /** @var string $data */ |
||
| 139 | $res = $doc->loadXML($data); |
||
| 140 | if (!$res) { |
||
| 141 | Logger::error($this->logLoc.'Error parsing XML from '.var_export($this->url, true)); |
||
| 142 | return null; |
||
| 143 | } |
||
| 144 | |||
| 145 | $root = Utils::xpQuery($doc->firstChild, '/saml_metadata:EntityDescriptor|/saml_metadata:EntitiesDescriptor'); |
||
| 146 | if (count($root) === 0) { |
||
| 147 | Logger::error($this->logLoc.'No <EntityDescriptor> or <EntitiesDescriptor> in metadata from '. |
||
| 148 | var_export($this->url, true)); |
||
| 149 | return null; |
||
| 150 | } |
||
| 151 | |||
| 152 | if (count($root) > 1) { |
||
| 153 | Logger::error($this->logLoc.'More than one <EntityDescriptor> or <EntitiesDescriptor> in metadata from '. |
||
| 154 | var_export($this->url, true)); |
||
| 155 | return null; |
||
| 156 | } |
||
| 157 | |||
| 158 | $root = $root[0]; |
||
| 159 | try { |
||
| 160 | if ($root->localName === 'EntityDescriptor') { |
||
| 161 | $md = new EntityDescriptor($root); |
||
| 162 | } else { |
||
| 163 | $md = new EntitiesDescriptor($root); |
||
| 164 | } |
||
| 165 | } catch (\Exception $e) { |
||
| 166 | Logger::error($this->logLoc.'Unable to parse metadata from '. |
||
| 167 | var_export($this->url, true).': '.$e->getMessage()); |
||
| 168 | return null; |
||
| 169 | } |
||
| 170 | |||
| 171 | if ($this->certificate !== null) { |
||
| 172 | $file = Config::getCertPath($this->certificate); |
||
| 173 | $certData = file_get_contents($file); |
||
| 174 | if ($certData === false) { |
||
| 175 | throw new Exception('Error loading certificate from '.var_export($file, true)); |
||
| 176 | } |
||
| 177 | |||
| 178 | // Extract the public key from the certificate for validation |
||
| 179 | $key = new XMLSecurityKey(XMLSecurityKey::RSA_SHA1, ['type'=>'public']); |
||
| 180 | $key->loadKey($file, true); |
||
| 181 | |||
| 182 | if (!$md->validate($key)) { |
||
| 183 | Logger::error($this->logLoc.'Error validating signature on metadata.'); |
||
| 184 | return null; |
||
| 185 | } |
||
| 186 | Logger::debug($this->logLoc.'Validated signature on metadata from '.var_export($this->url, true)); |
||
| 187 | } |
||
| 188 | |||
| 189 | return $md; |
||
| 190 | } |
||
| 262 |