Metadata::fromXML()   B
last analyzed

Complexity

Conditions 8
Paths 11

Size

Total Lines 41
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 23
c 1
b 0
f 0
dl 0
loc 41
rs 8.4444
cc 8
nc 11
nop 2
1
<?php
2
3
/*
4
 * This file is part of the LightSAML-Core package.
5
 *
6
 * (c) Milos Tomic <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace LightSaml\Model\Metadata;
13
14
use LightSaml\Error\LightSamlXmlException;
15
use LightSaml\Model\AbstractSamlModel;
16
use LightSaml\Model\Context\DeserializationContext;
17
use LightSaml\Model\SamlElementInterface;
18
use LightSaml\SamlConstants;
19
20
abstract class Metadata extends AbstractSamlModel
21
{
22
    /**
23
     * @param string $path
24
     *
25
     * @return EntitiesDescriptor|EntityDescriptor
26
     */
27
    public static function fromFile($path)
28
    {
29
        $deserializatonContext = new DeserializationContext();
30
        $xml = file_get_contents($path);
31
32
        return self::fromXML($xml, $deserializatonContext);
33
    }
34
35
    /**
36
     * @param string $xml
37
     *
38
     * @return EntityDescriptor|EntitiesDescriptor
39
     *
40
     * @throws \Exception
41
     */
42
    public static function fromXML($xml, DeserializationContext $context)
43
    {
44
        if (false == is_string($xml)) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
introduced by
The condition false == is_string($xml) is always false.
Loading history...
45
            throw new \InvalidArgumentException('Expecting string');
46
        }
47
48
        $context->getDocument()->loadXML($xml);
49
50
        $node = $context->getDocument()->firstChild;
51
        while ($node && $node instanceof \DOMComment) {
52
            $node = $node->nextSibling;
53
        }
54
        if (null === $node) {
55
            throw new LightSamlXmlException('Empty XML');
56
        }
57
58
        if (SamlConstants::NS_METADATA !== $node->namespaceURI) {
59
            throw new LightSamlXmlException(sprintf("Invalid namespace '%s' of the root XML element, expected '%s'", $node->namespaceURI, SamlConstants::NS_METADATA));
60
        }
61
62
        $map = [
63
            'EntityDescriptor' => '\LightSaml\Model\Metadata\EntityDescriptor',
64
            'EntitiesDescriptor' => '\LightSaml\Model\Metadata\EntitiesDescriptor',
65
        ];
66
67
        $rootElementName = $node->localName;
68
69
        if (array_key_exists($rootElementName, $map)) {
70
            if ($class = $map[$rootElementName]) {
71
                /** @var SamlElementInterface $result */
72
                $result = new $class();
73
            } else {
74
                throw new \LogicException('Deserialization of %s root element is not implemented');
75
            }
76
        } else {
77
            throw new LightSamlXmlException(sprintf("Unknown SAML metadata '%s'", $rootElementName));
78
        }
79
80
        $result->deserialize($node, $context);
81
82
        return $result;
83
    }
84
}
85