Passed
Pull Request — master (#18)
by SignpostMarv
03:22
created

Schema::findSomething()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

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