Metadata::fromXML()   B
last analyzed

Complexity

Conditions 8
Paths 11

Size

Total Lines 46

Duplication

Lines 10
Ratio 21.74 %

Code Coverage

Tests 15
CRAP Score 10.6925

Importance

Changes 0
Metric Value
dl 10
loc 46
c 0
b 0
f 0
ccs 15
cts 23
cp 0.6522
rs 7.9337
cc 8
nc 11
nop 2
crap 10.6925
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 2
    public static function fromFile($path)
28
    {
29 2
        $deserializatonContext = new DeserializationContext();
30 2
        $xml = file_get_contents($path);
31
32 2
        return self::fromXML($xml, $deserializatonContext);
33
    }
34
35
    /**
36
     * @param string                 $xml
37
     * @param DeserializationContext $context
38
     *
39
     * @return EntityDescriptor|EntitiesDescriptor
40
     *
41
     * @throws \Exception
42
     */
43 2
    public static function fromXML($xml, DeserializationContext $context)
44
    {
45 2
        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...
46
            throw new \InvalidArgumentException('Expecting string');
47
        }
48
49 2
        $context->getDocument()->loadXML($xml);
50
51 2
        $node = $context->getDocument()->firstChild;
52 2
        while ($node && $node instanceof \DOMComment) {
53
            $node = $node->nextSibling;
54
        }
55 2
        if (null === $node) {
56
            throw new LightSamlXmlException('Empty XML');
57
        }
58
59 2
        if (SamlConstants::NS_METADATA !== $node->namespaceURI) {
60
            throw new LightSamlXmlException(sprintf(
61
                "Invalid namespace '%s' of the root XML element, expected '%s'",
62
                $node->namespaceURI,
63
                SamlConstants::NS_METADATA
64
            ));
65
        }
66
67
        $map = array(
68 2
            'EntityDescriptor' => '\LightSaml\Model\Metadata\EntityDescriptor',
69
            'EntitiesDescriptor' => '\LightSaml\Model\Metadata\EntitiesDescriptor',
70
        );
71
72 2
        $rootElementName = $node->localName;
73
74 2 View Code Duplication
        if (array_key_exists($rootElementName, $map)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
75 2
            if ($class = $map[$rootElementName]) {
76
                /** @var SamlElementInterface $result */
77 2
                $result = new $class();
78
            } else {
79 2
                throw new \LogicException('Deserialization of %s root element is not implemented');
80
            }
81
        } else {
82
            throw new LightSamlXmlException(sprintf("Unknown SAML metadata '%s'", $rootElementName));
83
        }
84
85 2
        $result->deserialize($node, $context);
86
87 2
        return $result;
88
    }
89
}
90