GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

XmlDriver::parseExclusion()   B
last analyzed

Complexity

Conditions 6
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
c 0
b 0
f 0
rs 8.8571
cc 6
eloc 7
nc 1
nop 1
1
<?php
2
3
namespace Hateoas\Configuration\Metadata\Driver;
4
5
use Hateoas\Configuration\Embedded;
6
use Hateoas\Configuration\Exclusion;
7
use Hateoas\Configuration\Metadata\ClassMetadata;
8
use Hateoas\Configuration\Relation;
9
use Hateoas\Configuration\RelationProvider;
10
use Hateoas\Configuration\Route;
11
use JMS\Serializer\Exception\XmlErrorException;
12
use Metadata\Driver\AbstractFileDriver;
13
14
/**
15
 * @author Miha Vrhovnik <[email protected]>
16
 */
17
class XmlDriver extends AbstractFileDriver
18
{
19
    const NAMESPACE_URI = 'https://github.com/willdurand/Hateoas';
20
21
    /**
22
     * {@inheritdoc}
23
     */
24
    protected function loadMetadataFromFile(\ReflectionClass $class, $file)
25
    {
26
        $previous = libxml_use_internal_errors(true);
27
        $root     = simplexml_load_file($file);
28
        libxml_use_internal_errors($previous);
29
30
        if (false === $root) {
31
            throw new XmlErrorException(libxml_get_last_error());
32
        }
33
34
        $name = $class->getName();
35
        if (!$exists = $root->xpath("./class[@name = '" . $name . "']")) {
36
            throw new \RuntimeException(sprintf('Expected metadata for class %s to be defined in %s.', $name, $file));
37
        }
38
39
        $classMetadata = new ClassMetadata($name);
40
        $classMetadata->fileResources[] = $file;
41
        $classMetadata->fileResources[] = $class->getFileName();
42
43
        if ($exists[0]->attributes(self::NAMESPACE_URI)->providers) {
44
            $providers = preg_split('/\s*,\s*/', (string) $exists[0]->attributes(self::NAMESPACE_URI)->providers);
45
46
            foreach ($providers as $relationProvider) {
47
                $classMetadata->addRelationProvider(new RelationProvider($relationProvider));
48
            }
49
        }
50
51
        $elements = $exists[0]->children(self::NAMESPACE_URI);
52
53
        foreach ($elements->relation as $relation) {
54
            $name = (string) $relation->attributes('')->rel;
55
56
            $href = null;
57
            if (isset($relation->href)) {
58
                $href = $this->createHref($relation->href, $name);
59
            }
60
61
            $embedded = null;
62
            if (isset($relation->embedded)) {
63
                $embedded = $this->createEmbedded($relation->embedded);
64
            }
65
66
            $attributes = array();
67
            foreach ($relation->attribute as $attribute) {
68
                $attributes[(string) $attribute->attributes('')->name] = (string) $attribute->attributes('')->value;
69
            }
70
71
            $exclusion = isset($relation->exclusion) ? $this->parseExclusion($relation->exclusion) : null;
72
73
            $classMetadata->addRelation(
74
                new Relation(
75
                    $name,
76
                    $href,
77
                    $embedded,
78
                    $attributes,
79
                    $exclusion
80
                )
81
            );
82
        }
83
84
        return $classMetadata;
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90
    protected function getExtension()
91
    {
92
        return 'xml';
93
    }
94
95
    private function parseExclusion(\SimpleXMLElement $exclusion)
96
    {
97
        return new Exclusion(
98
            isset($exclusion->attributes('')->groups) ? preg_split('/\s*,\s*/', (string) $exclusion->attributes('')->groups) : null,
99
            isset($exclusion->attributes('')->{'since-version'}) ? (string) $exclusion->attributes('')->{'since-version'} : null,
100
            isset($exclusion->attributes('')->{'until-version'}) ? (string) $exclusion->attributes('')->{'until-version'} : null,
101
            isset($exclusion->attributes('')->{'max-depth'}) ? (string) $exclusion->attributes('')->{'max-depth'} : null,
102
            isset($exclusion->attributes('')->{'exclude-if'}) ? (string) $exclusion->attributes('')->{'exclude-if'} : null
103
        );
104
    }
105
106
    private function createHref($href, $name)
107
    {
108
        if (isset($href->attributes('')->uri) && isset($href->attributes('')->route)) {
109
            throw new \RuntimeException(sprintf(
110
                'uri and route attributes are mutually exclusive, please set only one of them. The problematic relation rel is %s.',
111
                $name
112
            ));
113
        } elseif (isset($href->attributes('')->uri)) {
114
            $href = (string) $href->attributes('')->uri;
115
        } else {
116
            $parameters = array();
117
            foreach ($href->parameter as $parameter) {
118
                $parameters[(string) $parameter->attributes('')->name] = (string) $parameter->attributes('')->value;
119
            }
120
121
            $href = new Route(
122
                (string) $href->attributes('')->route,
123
                $parameters,
124
                null !== ($absolute = $href->attributes('')->absolute) ? 'true' === strtolower($absolute) : false,
125
                isset($href->attributes('')->generator) ? (string) $href->attributes('')->generator : null
126
            );
127
        }
128
129
        return $href;
130
    }
131
132
    private function createEmbedded($embedded)
133
    {
134
        $embeddedExclusion = isset($embedded->exclusion) ? $this->parseExclusion($embedded->exclusion) : null;
135
        $xmlElementName = isset($embedded->attributes('')->{'xml-element-name'}) ? (string) $embedded->attributes('')->{'xml-element-name'} : null;
136
137
        return new Embedded(
138
            (string) $embedded->content,
139
            $xmlElementName,
140
            $embeddedExclusion
141
        );
142
    }
143
}
144