Passed
Push — php-7.1 ( ed6d10...451fb2 )
by SignpostMarv
02:49
created

Schema::loadImportFreshCallbacks()   B

Complexity

Conditions 2
Paths 1

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 2.0014

Importance

Changes 0
Metric Value
cc 2
eloc 13
c 0
b 0
f 0
nc 1
nop 4
dl 0
loc 24
ccs 13
cts 14
cp 0.9286
crap 2.0014
rs 8.9713
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
        ) {
40
            /**
41
            * @var SchemaItem|null $item
42
            */
43 45
            $item = $this->$getter($name);
44
45 45
            if ($item instanceof SchemaItem) {
46 45
            return $this->typeCache[$cid] = $item;
47
            }
48
        }
49
50 45
        return $this->findSomethingNoThrowSchemas(
51 45
            $this->getSchemas(),
52 45
            $cid,
53 45
            $getter,
54 45
            $name,
55 45
            $namespace,
56 45
            $calling
57
        );
58
    }
59
60
61
    /**
62
    * @param Schema[] $schemas
63
    * {@inheritdoc}
64
    */
65 45
    protected function findSomethingNoThrowSchemas(
66
        array $schemas,
67
        string $cid,
68
        string $getter,
69
        string $name,
70
        string $namespace = null,
71
        array & $calling = array()
72
    ) : ? SchemaItem {
73 45
        foreach ($schemas as $childSchema) {
74 45
            if (!isset($calling[spl_object_hash($childSchema)])) {
75
                /**
76
                * @var SchemaItem|null $in
77
                */
78 45
                $in = $childSchema->findSomethingNoThrow($getter, $name, $namespace, $calling);
79
80 45
                if ($in instanceof SchemaItem) {
81 45
                    return $this->typeCache[$cid] = $in;
82
                }
83
            }
84
        }
85
86 7
        return null;
87
    }
88
89
    /**
90
     * @param string $getter
91
     * {@inheritdoc}
92
     */
93 45
    protected function findSomething(
94
        string $getter,
95
        string $name,
96
        string $namespace = null,
97
        array &$calling = array()
98
    ) : SchemaItem {
99 45
        $in = $this->findSomethingNoThrow(
100 45
            $getter,
101 45
            $name,
102 45
            $namespace,
103 45
            $calling
104
        );
105
106 45
        if ($in instanceof SchemaItem) {
107 45
            return $in;
108
        }
109
110 5
        throw new TypeNotFoundException(sprintf("Can't find the %s named {%s}#%s.", substr($getter, 3), $namespace, $name));
111
    }
112
113
    /**
114
    * @param string $namespace
115
    * @param string $file
116
    * {@inheritdoc}
117
    */
118 45
    protected static function loadImportFreshKeys(
119
        SchemaReaderLoadAbstraction $reader,
120
        $namespace,
121
        $file
122
    ) {
123 45
        $globalSchemaInfo = $reader->getGlobalSchemaInfo();
124
125 45
        $keys = [];
126
127 45
        if (isset($globalSchemaInfo[$namespace])) {
128 45
            $keys[] = $globalSchemaInfo[$namespace];
129
        }
130
131 45
        $keys[] = $reader->getNamespaceSpecificFileIndex(
132 45
            $file,
133 45
            $namespace
134
        );
135
136 45
        $keys[] = $file;
137
138 45
        return $keys;
139
    }
140
141
    /**
142
    * {@inheritdoc}
143
    */
144 1
    protected static function loadImportFreshCallbacksNewSchema(
145
        $namespace,
146
        SchemaReaderLoadAbstraction $reader,
147
        Schema $schema,
148
        $file
149
    ) {
150
        /**
151
        * @var string $file
152
        */
153 1
        $newSchema = Schema::setLoadedFile(
154 1
            $file,
155 1
            ($namespace ? new Schema() : $schema)
156
        );
157
158 1
        if ($namespace) {
159
            $newSchema->addSchema($reader->getGlobalSchema());
160
            $schema->addSchema($newSchema);
161
        }
162
163 1
        return $newSchema;
164
    }
165
166
    /**
167
    * {@inheritdoc}
168
    */
169 1
    protected static function loadImportFreshCallbacks(
170
        $namespace,
171
        SchemaReaderLoadAbstraction $reader,
172
        Schema $schema,
173
        $file
174
    ) {
175
        /**
176
        * @var string $file
177
        */
178 1
        $file = $file;
179
180 1
        return $reader->schemaNode(
181 1
            static::loadImportFreshCallbacksNewSchema(
182 1
                $namespace,
183 1
                $reader,
184 1
                $schema,
185 1
                $file
186
            ),
187 1
            $reader->getDOM(
188 1
                $reader->hasKnownSchemaLocation($file)
189
                    ? $reader->getKnownSchemaLocation($file)
190 1
                    : $file
191 1
            )->documentElement,
192 1
            $schema
193
        );
194
    }
195
196
    /**
197
    * {@inheritdoc}
198
    */
199
    protected static function loadImportFresh(
200
        $namespace,
201
        SchemaReaderLoadAbstraction $reader,
202
        Schema $schema,
203
        $file
204
    ) {
205 1
        return function () use ($namespace, $reader, $schema, $file) : void {
206
            foreach (
207 1
                static::loadImportFreshCallbacks(
208 1
                    $namespace,
209 1
                    $reader,
210 1
                    $schema,
211 1
                    $file
212
                ) as $callback
213
            ) {
214 1
                $callback();
215
            }
216 1
        };
217
    }
218
}
219