Passed
Push — php-7.1 ( 280be0...7ba4a5 )
by SignpostMarv
02:07
created

Schema   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 173
Duplicated Lines 0 %

Test Coverage

Coverage 95.65%

Importance

Changes 0
Metric Value
dl 0
loc 173
ccs 66
cts 69
cp 0.9565
rs 10
c 0
b 0
f 0
wmc 19

7 Methods

Rating   Name   Duplication   Size   Complexity  
A findSomethingNoThrowSchemas() 0 19 4
A loadImportFresh() 0 16 2
B findSomethingNoThrow() 0 25 4
A loadImportFreshCallbacksNewSchema() 0 17 3
A loadImportFreshKeys() 0 21 2
A findSomething() 0 18 2
A loadImportFreshCallbacks() 0 19 2
1
<?php
2
declare(strict_types = 1);
3
namespace GoetasWebservices\XML\XSDReader\Schema;
4
5
use Closure;
6
use DOMElement;
7
use RuntimeException;
8
use GoetasWebservices\XML\XSDReader\AbstractSchemaReader;
9
use GoetasWebservices\XML\XSDReader\SchemaReaderLoadAbstraction;
10
use GoetasWebservices\XML\XSDReader\Schema\Type\Type;
11
use GoetasWebservices\XML\XSDReader\Schema\Attribute\Group as AttributeGroup;
12
use GoetasWebservices\XML\XSDReader\Schema\Element\Group;
13
use GoetasWebservices\XML\XSDReader\Schema\Element\ElementDef;
14
use GoetasWebservices\XML\XSDReader\Schema\Element\ElementItem;
15
use GoetasWebservices\XML\XSDReader\Schema\Exception\TypeNotFoundException;
16
use GoetasWebservices\XML\XSDReader\Schema\Exception\SchemaException;
17
use GoetasWebservices\XML\XSDReader\Schema\Attribute\AttributeItem;
18
use GoetasWebservices\XML\XSDReader\Schema\Attribute\AttributeDef;
19
use GoetasWebservices\XML\XSDReader\Utils\UrlUtils;
20
21
class Schema extends AbstractSchema
22
{
23
    /**
24
    * {@inheritdoc}
25
    */
26 45
    protected function findSomethingNoThrow(
27
        string $getter,
28
        string $name,
29
        string $namespace = null,
30
        array & $calling = array()
31
    ) : ? SchemaItem {
32 45
        $calling[spl_object_hash($this)] = true;
33 45
        $cid = "$getter, $name, $namespace";
34
35 45
        if (isset($this->typeCache[$cid])) {
36 45
            return $this->typeCache[$cid];
37
        } elseif (
38 45
            $this->getTargetNamespace() === $namespace &&
39 45
            (($item = $this->$getter($name)) instanceof SchemaItem)
40
        ) {
41 45
            return $this->typeCache[$cid] = $item;
42
        }
43
44 45
        return $this->findSomethingNoThrowSchemas(
45 45
            $this->getSchemas(),
46 45
            $cid,
47 45
            $getter,
48 45
            $name,
49 45
            $namespace,
50 45
            $calling
51
        );
52
    }
53
54
55
    /**
56
    * {@inheritdoc}
57
    */
58 45
    protected function findSomethingNoThrowSchemas(
59
        array $schemas,
60
        string $cid,
61
        string $getter,
62
        string $name,
63
        string $namespace = null,
64
        array & $calling = array()
65
    ) : ? SchemaItem {
66 45
        foreach ($schemas as $childSchema) {
67 45
            if (!isset($calling[spl_object_hash($childSchema)])) {
68 45
                $in = $childSchema->findSomethingNoThrow($getter, $name, $namespace, $calling);
69
70 45
                if ($in instanceof SchemaItem) {
71 45
                    return $this->typeCache[$cid] = $in;
72
                }
73
            }
74
        }
75
76 7
        return null;
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82 45
    protected function findSomething(
83
        string $getter,
84
        string $name,
85
        string $namespace = null,
86
        array &$calling = array()
87
    ) : SchemaItem {
88 45
        $in = $this->findSomethingNoThrow(
89 45
            $getter,
90 45
            $name,
91 45
            $namespace,
92 45
            $calling
93
        );
94
95 45
        if ($in instanceof SchemaItem) {
96 45
            return $in;
97
        }
98
99 5
        throw new TypeNotFoundException(sprintf("Can't find the %s named {%s}#%s.", substr($getter, 3), $namespace, $name));
100
    }
101
102
    /**
103
    * {@inheritdoc}
104
    */
105 45
    protected static function loadImportFreshKeys(
106
        SchemaReaderLoadAbstraction $reader,
107
        $namespace,
108
        $file
109
    ) {
110 45
        $globalSchemaInfo = $reader->getGlobalSchemaInfo();
111
112 45
        $keys = [];
113
114 45
        if (isset($globalSchemaInfo[$namespace])) {
115 45
            $keys[] = $globalSchemaInfo[$namespace];
116
        }
117
118 45
        $keys[] = $reader->getNamespaceSpecificFileIndex(
119 45
            $file,
120 45
            $namespace
121
        );
122
123 45
        $keys[] = $file;
124
125 45
        return $keys;
126
    }
127
128
    /**
129
    * {@inheritdoc}
130
    */
131 1
    protected static function loadImportFreshCallbacksNewSchema(
132
        $namespace,
133
        SchemaReaderLoadAbstraction $reader,
134
        Schema $schema,
135
        $file
136
    ) {
137 1
        $newSchema = Schema::setLoadedFile(
138 1
            $file,
139 1
            ($namespace ? new Schema() : $schema)
140
        );
141
142 1
        if ($namespace) {
143
            $newSchema->addSchema($reader->getGlobalSchema());
144
            $schema->addSchema($newSchema);
145
        }
146
147 1
        return $newSchema;
148
    }
149
150
    /**
151
    * {@inheritdoc}
152
    */
153 1
    protected static function loadImportFreshCallbacks(
154
        $namespace,
155
        SchemaReaderLoadAbstraction $reader,
156
        Schema $schema,
157
        $file
158
    ) {
159 1
        return $reader->schemaNode(
160 1
            static::loadImportFreshCallbacksNewSchema(
161 1
                $namespace,
162 1
                $reader,
163 1
                $schema,
164 1
                $file
165
            ),
166 1
            $reader->getDOM(
167 1
                $reader->hasKnownSchemaLocation($file)
168
                    ? $reader->getKnownSchemaLocation($file)
169 1
                    : $file
170 1
            )->documentElement,
171 1
            $schema
172
        );
173
    }
174
175
    /**
176
    * {@inheritdoc}
177
    */
178
    protected static function loadImportFresh(
179
        $namespace,
180
        SchemaReaderLoadAbstraction $reader,
181
        Schema $schema,
182
        $file
183
    ) {
184 1
        return function () use ($namespace, $reader, $schema, $file) : void {
185
            foreach (
186 1
                static::loadImportFreshCallbacks(
187 1
                    $namespace,
188 1
                    $reader,
189 1
                    $schema,
190 1
                    $file
191
                ) as $callback
192
            ) {
193 1
                $callback();
194
            }
195 1
        };
196
    }
197
}
198