Passed
Branch dev_2x (3e8772)
by Adrian
01:42
created

Mapper::getErrors()   F

Complexity

Conditions 13
Paths 1024

Size

Total Lines 47
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 24
c 0
b 0
f 0
dl 0
loc 47
rs 2.45
cc 13
nc 1024
nop 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
declare(strict_types=1);
3
4
namespace Sirius\Orm\Blueprint;
5
6
use Sirius\Orm\Helpers\Inflector;
7
use Sirius\Orm\Helpers\Str;
8
9
class Mapper extends Base
10
{
11
    const ENTITY_STYLE_PROPERTIES = 'properties';
12
    const ENTITY_STYLE_METHODS = 'methods';
13
14
    /**
15
     * @var Orm
16
     */
17
    protected $orm;
18
19
    protected $name;
20
21
    protected $namespace;
22
23
    protected $className;
24
25
    protected $destination;
26
27
    protected $entityNamespace;
28
29
    protected $entityClass;
30
31
    protected $entityDestination;
32
33
    protected $entityStyle = self::ENTITY_STYLE_PROPERTIES;
34
35
    protected $primaryKey = 'id';
36
37
    protected $table;
38
39
    protected $tableAlias;
40
41
    protected $columns = [];
42
43
    protected $defaults = [];
44
45
    protected $behaviours = [];
46
47
    protected $relations = [];
48
49
    protected $guards = [];
50
51
    protected $traits = [];
52
53
    protected $computedProperties = [];
54
55
    protected $mapperMethods = [];
56
57
    protected $queryMethods = [];
58
59
    protected $entityMethods = [];
60
61
62
    public static function make(string $name)
63
    {
64
        return (new static)->setName($name);
65
    }
66
67
    public function getErrors(): array
68
    {
69
        $errors = [];
70
71
        if (! $this->table) {
72
            $errors[] = 'Missing table property';
73
        }
74
75
        if (! $this->className) {
76
            $errors[] = 'Missing class name property';
77
        }
78
79
        if ($this->destination) {
80
            if (! is_dir($this->destination)) {
81
                $errors[] = sprintf('%s is not a valid directory', $this->destination);
82
            } elseif (! is_writable($this->destination)) {
83
                $errors[] = sprintf('%s is not writable', $this->destination);
84
            }
85
        }
86
87
        if (! $this->entityClass) {
88
            $errors[] = 'Missing entity class name property';
89
        }
90
91
        if ($this->entityDestination) {
92
            if (! is_dir($this->entityDestination)) {
93
                $errors[] = sprintf('%s is not a valid directory', $this->entityDestination);
94
            } elseif (! is_writable($this->entityDestination)) {
95
                $errors[] = sprintf('%s is not writable', $this->entityDestination);
96
            }
97
        }
98
99
        if (empty($this->columns)) {
100
            $errors[] = 'Missing columns definitions';
101
        }
102
103
        /** @var Column $column */
104
        foreach ($this->columns as $column) {
105
            $errors = array_merge($errors, $column->getErrors());
106
        }
107
108
        /** @var Relation $relation */
109
        foreach ($this->relations as $relation) {
110
            $errors = array_merge($errors, $relation->getErrors());
111
        }
112
113
        return $errors;
114
    }
115
116
    public function getObservers(): array
117
    {
118
        $observers = [];
119
        /** @var Column $column */
120
        foreach ($this->columns as $column) {
121
            $observers = array_merge_recursive($observers, $column->getObservers());
122
        }
123
124
        /** @var Behaviour $behaviour */
125
        foreach ($this->behaviours as $behaviour) {
126
            $observers = array_merge_recursive($observers, $behaviour->getObservers());
127
        }
128
129
        /** @var ComputedProperty $property */
130
        foreach ($this->computedProperties as $property) {
131
            $observers = array_merge_recursive($observers, $property->getObservers());
132
        }
133
134
        /** @var Relation $relation */
135
        foreach ($this->relations as $relation) {
136
            $observers = array_merge_recursive($observers, $relation->getObservers());
137
        }
138
139
        return $observers;
140
    }
141
142
    public function getOrm(): Orm
143
    {
144
        return $this->orm;
145
    }
146
147
    public function setOrm(Orm $orm): Mapper
148
    {
149
        $this->orm = $orm;
150
151
        return $this;
152
    }
153
154
    public function getClassName(): string
155
    {
156
        return $this->className;
157
    }
158
159
    public function setClassName(string $className): Mapper
160
    {
161
        $this->className = $className;
162
163
        return $this;
164
    }
165
166
    public function getNamespace(): string
167
    {
168
        return $this->namespace ?: $this->orm->getMapperNamespace();
169
    }
170
171
    public function setNamespace(string $namespace): Mapper
172
    {
173
        $this->namespace = $namespace;
174
175
        return $this;
176
    }
177
178
    public function getDestination(): string
179
    {
180
        return $this->destination ?: $this->orm->getMapperDestination();
181
    }
182
183
    public function setDestination($destination): Mapper
184
    {
185
        $this->destination = $destination;
186
187
        return $this;
188
    }
189
190
    public function getEntityClass():string
191
    {
192
        return $this->entityClass;
193
    }
194
195
    public function setEntityClass(string $entityClass): Mapper
196
    {
197
        $this->entityClass = $entityClass;
198
199
        return $this;
200
    }
201
202
    public function getEntityNamespace(): string
203
    {
204
        return $this->entityNamespace ?: $this->orm->getEntityNamespace();
205
    }
206
207
    public function setEntityNamespace(string $entityNamespace): Mapper
208
    {
209
        $this->entityNamespace = $entityNamespace;
210
211
        return $this;
212
    }
213
214
    public function getEntityDestination(): string
215
    {
216
        return $this->entityDestination ?: $this->orm->getEntityDestination();
217
    }
218
219
    public function setEntityDestination(string $entityDestination): Mapper
220
    {
221
        $this->entityDestination = $entityDestination;
222
223
        return $this;
224
    }
225
226
    public function getEntityStyle(): string
227
    {
228
        return $this->entityStyle;
229
    }
230
231
    public function setEntityStyle(string $entityStyle): Mapper
232
    {
233
        $this->entityStyle = $entityStyle;
234
235
        return $this;
236
    }
237
238
    public function getPrimaryKey(): string
239
    {
240
        return $this->primaryKey;
241
    }
242
243
    public function setPrimaryKey(string $primaryKey): Mapper
244
    {
245
        $this->primaryKey = $primaryKey;
246
247
        return $this;
248
    }
249
250
    public function getTable(): string
251
    {
252
        return $this->table;
253
    }
254
255
    public function setTable($table): Mapper
256
    {
257
        $this->table = $table;
258
259
        return $this;
260
    }
261
262
    public function getTableAlias(): ?string
263
    {
264
        return $this->tableAlias;
265
    }
266
267
    public function setTableAlias($tableAlias): Mapper
268
    {
269
        $this->tableAlias = $tableAlias;
270
271
        return $this;
272
    }
273
274
    public function getColumns(): array
275
    {
276
        return $this->columns;
277
    }
278
279
    public function setColumns(array $columns): Mapper
280
    {
281
        $this->columns = $columns;
282
283
        return $this;
284
    }
285
286
    public function getDefaults(): array
287
    {
288
        return $this->defaults;
289
    }
290
291
    public function setDefaults(array $defaults): Mapper
292
    {
293
        $this->defaults = $defaults;
294
295
        return $this;
296
    }
297
298
    public function addAutoIncrementColumn($name = 'id'): Mapper
299
    {
300
        return $this->addColumn(
301
            Column::integer($name, true)
302
                  ->setAutoIncrement(true)
303
        );
304
    }
305
306
    public function getGuards(): array
307
    {
308
        return $this->guards;
309
    }
310
311
    public function setGuards(array $guards): Mapper
312
    {
313
        $this->guards = $guards;
314
315
        return $this;
316
    }
317
318
    public function addTrait(string $traitClassName): Mapper
319
    {
320
        $this->traits[] = $traitClassName;
321
322
        return $this;
323
    }
324
325
    public function getTraits(): array
326
    {
327
        return $this->traits;
328
    }
329
330
    public function addColumn(Column $column): Mapper
331
    {
332
        $column->setMapper($this);
333
        $this->columns[$column->getName()] = $column;
334
335
        return $this;
336
    }
337
338
    public function addBehaviour(Behaviour $behaviour)
339
    {
340
        $behaviour->setMapper($this);
341
        $this->behaviours[$behaviour->getName()] = $behaviour;
342
343
        return $this;
344
    }
345
346
    public function addMethod(ClassMethod $method): Mapper
347
    {
348
        $this->mapperMethods[$method->getName()] = $method;
349
350
        return $this;
351
    }
352
353
    public function addRelation($name, Relation $relation): Mapper
354
    {
355
        $relation->setMapper($this);
356
        if (! $relation->getForeignMapper()) {
357
            $relation->setForeignMapper(Inflector::pluralize($name));
358
        }
359
        $relation->setName($name);
360
        $this->relations[$name] = $relation;
361
362
        return $this;
363
    }
364
365
    public function getRelations(): array
366
    {
367
        return $this->relations;
368
    }
369
370
    public function addComputedProperty(ComputedProperty $property): Mapper
371
    {
372
        $property->setMapper($this);
373
        $this->computedProperties[$property->getName()] = $property;
374
375
        return $this;
376
    }
377
378
    public function getName(): string
379
    {
380
        return $this->name;
381
    }
382
383
    public function setName(string $name): Mapper
384
    {
385
        $this->name = $name;
386
387
        $singular = Inflector::singularize($name);
388
389
        if (! $this->table) {
390
            $this->setTable($name);
391
        }
392
393
        if ($this->table !== $name && ! $this->tableAlias) {
394
            $this->setTableAlias($name);
395
        }
396
397
        if (! $this->className) {
398
            $this->setClassName(Str::className($singular) . 'Mapper');
399
        }
400
401
        if (! $this->entityClass) {
402
            $this->setEntityClass(Str::className($singular));
403
        }
404
405
        return $this;
406
    }
407
408
    public function getQueryClass()
409
    {
410
        return str_replace('Mapper', 'Query', $this->getClassName());
411
    }
412
}
413