Completed
Push — master ( 8029f8...8d3f65 )
by Neomerx
02:45
created

ModelSchemes::hasAttributeType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php namespace Limoncello\Models;
2
3
/**
4
 * Copyright 2015-2016 [email protected] (www.neomerx.com)
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
use Limoncello\Models\Contracts\ModelSchemesInterface;
20
use LogicException;
21
22
/**
23
 * @package Limoncello\Models
24
 */
25
class ModelSchemes implements ModelSchemesInterface
26
{
27
    /**
28
     * @var array
29
     */
30
    private $relationshipTypes;
31
32
    /**
33
     * @var array
34
     */
35
    private $reversedRelationships;
36
37
    /**
38
     * @var array
39
     */
40
    private $reversedClasses;
41
42
    /**
43
     * @var array
44
     */
45
    private $foreignKeys;
46
47
    /**
48
     * @var array
49
     */
50
    private $belongsToMany;
51
52
    /**
53
     * @var array
54
     */
55
    private $tableNames;
56
57
    /**
58
     * @var array
59
     */
60
    private $primaryKeys;
61
62
    /**
63
     * @var array
64
     */
65
    private $attributeTypes;
66
67
    /**
68
     * @var array
69
     */
70
    private $attributeLengths;
71
72
    /**
73
     * @var array
74
     */
75
    private $attributes;
76
77
    /**
78
     * @inheritdoc
79
     */
80 5
    public function getData()
81
    {
82
        $result = [
83 5
            $this->foreignKeys,
84 5
            $this->belongsToMany,
85 5
            $this->relationshipTypes,
86 5
            $this->reversedRelationships,
87 5
            $this->tableNames,
88 5
            $this->primaryKeys,
89 5
            $this->attributeTypes,
90 5
            $this->attributeLengths,
91 5
            $this->attributes,
92 5
            $this->reversedClasses,
93 5
        ];
94
95 5
        return $result;
96
    }
97
98
    /**
99
     * @inheritdoc
100
     */
101 5
    public function setData(array $data)
102
    {
103 5
        list($this->foreignKeys, $this->belongsToMany, $this->relationshipTypes,
104 5
            $this->reversedRelationships,$this->tableNames, $this->primaryKeys,
105 5
            $this->attributeTypes, $this->attributeLengths, $this->attributes, $this->reversedClasses) = $data;
106 5
    }
107
108
    /**
109
     * @inheritdoc
110
     */
111 6
    public function registerClass($class, $tableName, $primaryKey, array $attributeTypes, array $attributeLengths)
112
    {
113 6
        $this->tableNames[$class]       = $tableName;
114 6
        $this->primaryKeys[$class]      = $primaryKey;
115 6
        $this->attributeTypes[$class]   = $attributeTypes;
116 6
        $this->attributeLengths[$class] = $attributeLengths;
117 6
        $this->attributes[$class]       = array_keys($attributeTypes);
118
119 6
        return $this;
120
    }
121
122
    /**
123
     * @inheritdoc
124
     */
125 1
    public function hasClass($class)
126
    {
127 1
        $result = array_key_exists($class, $this->tableNames);
128
129 1
        return $result;
130
    }
131
132
    /**
133
     * @inheritdoc
134
     */
135 2
    public function getTable($class)
136
    {
137 2
        $result = $this->tableNames[$class];
138
139 2
        return $result;
140
    }
141
142
    /**
143
     * @inheritdoc
144
     */
145 3
    public function getPrimaryKey($class)
146
    {
147 3
        $result = $this->primaryKeys[$class];
148
149 3
        return $result;
150
    }
151
152
    /**
153
     * @inheritdoc
154
     */
155 1
    public function getAttributeTypes($class)
156
    {
157 1
        $result = $this->attributeTypes[$class];
158
159 1
        return $result;
160
    }
161
162
    /**
163
     * @inheritdoc
164
     */
165 1
    public function getAttributeType($class, $name)
166
    {
167 1
        $result = $this->attributeTypes[$class][$name];
168
169 1
        return $result;
170
    }
171
172
    /**
173
     * @inheritdoc
174
     */
175 1
    public function hasAttributeType($class, $name)
176
    {
177 1
        $result = isset($this->attributeTypes[$class][$name]);
178
179 1
        return $result;
180
    }
181
182
    /**
183
     * @inheritdoc
184
     */
185 1
    public function getAttributeLengths($class)
186
    {
187 1
        $result = $this->attributeLengths[$class];
188
189 1
        return $result;
190
    }
191
192
    /**
193
     * @inheritdoc
194
     */
195 1
    public function hasAttributeLength($class, $name)
196
    {
197 1
        $result = isset($this->attributeLengths[$class][$name]);
198
199 1
        return $result;
200
    }
201
202
    /**
203
     * @inheritdoc
204
     */
205 1
    public function getAttributeLength($class, $name)
206
    {
207 1
        $result = $this->attributeLengths[$class][$name];
208
209 1
        return $result;
210
    }
211
212
    /**
213
     * @inheritdoc
214
     */
215 1
    public function getAttributes($class)
216
    {
217 1
        $result = $this->attributes[$class];
218
219 1
        return $result;
220
    }
221
222
    /**
223
     * @inheritdoc
224
     */
225 2
    public function hasRelationship($class, $name)
226
    {
227 2
        $result = isset($this->relationshipTypes[$class][$name]);
228
229 2
        return $result;
230
    }
231
232
    /**
233
     * @inheritdoc
234
     */
235 2
    public function getRelationshipType($class, $name)
236
    {
237 2
        $result = $this->relationshipTypes[$class][$name];
238
239 2
        return $result;
240
    }
241
242
    /**
243
     * @inheritdoc
244
     */
245 2
    public function getReverseRelationship($class, $name)
246
    {
247 2
        $result = $this->reversedRelationships[$class][$name];
248
249 2
        return $result;
250
    }
251
252
    /**
253
     * @inheritdoc
254
     */
255 1 View Code Duplication
    public function getReversePrimaryKey($class, $name)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
256
    {
257 1
        $reverseClass = $this->getReverseModelClass($class, $name);
258
259 1
        $table = $this->getTable($reverseClass);
260 1
        $key   = $this->getPrimaryKey($reverseClass);
261
262 1
        return [$key, $table];
263
    }
264
265
    /**
266
     * @inheritdoc
267
     */
268 1 View Code Duplication
    public function getReverseForeignKey($class, $name)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
269
    {
270 1
        list ($reverseClass, $reverseName) = $this->getReverseRelationship($class, $name);
271
272 1
        $table = $this->getTable($reverseClass);
273
        // would work only if $name is hasMany relationship
274 1
        $key   = $this->getForeignKey($reverseClass, $reverseName);
275
276 1
        return [$key, $table];
277
    }
278
279
    /**
280
     * @inheritdoc
281
     */
282 1
    public function getReverseModelClass($class, $name)
283
    {
284 1
        $reverseClass = $this->reversedClasses[$class][$name];
285
286 1
        return $reverseClass;
287
    }
288
289
    /**
290
     * @inheritdoc
291
     */
292 1
    public function getForeignKey($class, $name)
293
    {
294 1
        $result = $this->foreignKeys[$class][$name];
295
296 1
        return $result;
297
    }
298
299
    /**
300
     * @inheritdoc
301
     */
302 1
    public function getBelongsToManyRelationship($class, $name)
303
    {
304 1
        $result = $this->belongsToMany[$class][$name];
305
306 1
        return $result;
307
    }
308
309
    /**
310
     * @inheritdoc
311
     */
312 5
    public function registerBelongsToOneRelationship($class, $name, $foreignKey, $reverseClass, $reverseName)
313
    {
314 5
        $this->registerRelationshipType(RelationshipTypes::BELONGS_TO, $class, $name);
315 5
        $this->registerRelationshipType(RelationshipTypes::HAS_MANY, $reverseClass, $reverseName);
316
317 5
        $this->registerReversedRelationship($class, $name, $reverseClass, $reverseName);
318 5
        $this->registerReversedRelationship($reverseClass, $reverseName, $class, $name);
319
320 5
        $this->foreignKeys[$class][$name] = $foreignKey;
321
322 5
        return $this;
323
    }
324
325
    /**
326
     * @inheritdoc
327
     */
328 5
    public function registerBelongsToManyRelationship(
329
        $class,
330
        $name,
331
        $table,
332
        $foreignKey,
333
        $reverseForeignKey,
334
        $reverseClass,
335
        $reverseName
336
    ) {
337 5
        $this->registerRelationshipType(RelationshipTypes::BELONGS_TO_MANY, $class, $name);
338 5
        $this->registerRelationshipType(RelationshipTypes::BELONGS_TO_MANY, $reverseClass, $reverseName);
339
340
        // NOTE:
341
        // `registerReversedRelationship` relies on duplicate registration check in `registerRelationshipType`
342
        // so it must be called afterwards
343 5
        $this->registerReversedRelationship($class, $name, $reverseClass, $reverseName);
344 5
        $this->registerReversedRelationship($reverseClass, $reverseName, $class, $name);
345
346 5
        $this->belongsToMany[$class][$name]               = [$table, $foreignKey, $reverseForeignKey];
347 5
        $this->belongsToMany[$reverseClass][$reverseName] = [$table, $reverseForeignKey, $foreignKey];
348
349 5
        return $this;
350
    }
351
352
    /**
353
     * @param int    $type
354
     * @param string $class
355
     * @param string $name
356
     */
357 5
    private function registerRelationshipType($type, $class, $name)
358
    {
359 5
        if (isset($this->relationshipTypes[$class][$name]) === true) {
360
            // already registered
361 2
            throw new LogicException();
362
        }
363
364 5
        $this->relationshipTypes[$class][$name] = $type;
365 5
    }
366
367
    /**
368
     * @param string $class
369
     * @param string $name
370
     * @param string $reverseClass
371
     * @param string $reverseName
372
     */
373 5
    private function registerReversedRelationship($class, $name, $reverseClass, $reverseName)
374
    {
375
        // NOTE:
376
        // this function relies it would be called after
377
        // `registerRelationshipType` which prevents duplicate registrations
378
379 5
        $this->reversedRelationships[$class][$name] = [$reverseClass, $reverseName];
380 5
        $this->reversedClasses[$class][$name]       = $reverseClass;
381 5
    }
382
}
383