Passed
Push — static-analysis ( ae4331...8e814c )
by SignpostMarv
03:31
created

Schema::loadImportFreshCallbacks()   B

Complexity

Conditions 2
Paths 1

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 2

Importance

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