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

Orm::getErrors()   C

Complexity

Conditions 11
Paths 192

Size

Total Lines 36
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
c 1
b 0
f 0
dl 0
loc 36
rs 6.55
cc 11
nc 192
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
class Orm extends Base
7
{
8
    /**
9
     * @var string
10
     */
11
    protected $mapperNamespace;
12
13
    /**
14
     * @var string
15
     */
16
    protected $mapperDestination;
17
18
    /**
19
     * @var string
20
     */
21
    protected $entityNamespace;
22
23
    /**
24
     * @var string
25
     */
26
    protected $entityDestination;
27
28
    /**
29
     * @var array
30
     */
31
    protected $mappers = [];
32
33
    public static function make(): Orm
34
    {
35
        return new static;
36
    }
37
38
    public function getErrors(): array
39
    {
40
        $errors = [];
41
42
        if (! $this->entityNamespace) {
43
            $errors[] = 'Missing entity namespace property';
44
        }
45
46
        if (! $this->entityDestination) {
47
            $errors[] = 'Missing entity destination property';
48
        } elseif (! is_dir($this->entityDestination)) {
49
            $errors[] = sprintf('%s is not a valid directory', $this->entityDestination);
50
        } elseif (! is_writable($this->entityDestination)) {
51
            $errors[] = sprintf('%s is not writable', $this->entityDestination);
52
        }
53
54
        if (! $this->mapperNamespace) {
55
            $errors[] = 'Missing mapper namespace property';
56
        }
57
58
        if (! $this->mapperDestination) {
59
            $errors[] = 'Missing entity destination property';
60
        } elseif (! is_dir($this->mapperDestination)) {
61
            $errors[] = sprintf('%s is not a valid directory', $this->mapperDestination);
62
        } elseif (! is_writable($this->mapperDestination)) {
63
            $errors[] = sprintf('%s is not writable', $this->mapperDestination);
64
        }
65
66
        /** @var Mapper $mapper */
67
        foreach ($this->mappers as $name => $mapper) {
68
            foreach ($mapper->getErrors() as $error) {
69
                $errors[] = sprintf('Mapper %s: %s', $name, $error);
70
            }
71
        }
72
73
        return $errors;
74
    }
75
76
    public function getObservers(): array
77
    {
78
        $observers = [];
79
        /** @var Mapper $mapper */
80
        foreach ($this->mappers as $mapper) {
81
            $observers = array_merge_recursive($observers, $mapper->getObservers());
82
        }
83
84
        return $observers;
85
    }
86
87
    public function applyObservers(string $key, $object)
88
    {
89
        $observers = $this->getObservers()[$key] ?? [];
90
        /** @var \Sirius\Orm\CodeGenerator\Observer\Base $observer */
91
        foreach ($observers as $observer) {
92
            $object = $observer->observe($key, $object);
93
        }
94
95
        return $object;
96
    }
97
98
    public function getMapperNamespace(): string
99
    {
100
        return $this->mapperNamespace;
101
    }
102
103
    /**
104
     * Set the default namespace for future mappers
105
     */
106
    public function setMapperNamespace(string $mapperNamespace): Orm
107
    {
108
        $this->mapperNamespace = $mapperNamespace;
109
110
        return $this;
111
    }
112
113
    public function getMapperDestination(): string
114
    {
115
        return $this->mapperDestination;
116
    }
117
118
    /**
119
     * Set default destination for future mappers
120
     */
121
    public function setMapperDestination(string $mapperDestination): Orm
122
    {
123
        $this->mapperDestination = $mapperDestination;
124
125
        return $this;
126
    }
127
128
    public function getEntityNamespace(): string
129
    {
130
        return $this->entityNamespace;
131
    }
132
133
    /**
134
     * Set default namespace for the entity classes to be be generated
135
     */
136
    public function setEntityNamespace(string $entityNamespace): Orm
137
    {
138
        $this->entityNamespace = $entityNamespace;
139
140
        return $this;
141
    }
142
143
    public function getEntityDestination(): string
144
    {
145
        return $this->entityDestination;
146
    }
147
148
    /**
149
     * Set default destination for the entity classes to be be generated
150
     */
151
    public function setEntityDestination(string $entityDestination): Orm
152
    {
153
        $this->entityDestination = $entityDestination;
154
155
        return $this;
156
    }
157
158
    /**
159
     * @return array|Mapper[]
160
     */
161
    public function getMappers(): array
162
    {
163
        return $this->mappers;
164
    }
165
166
    public function getMapper($name): ?Mapper
167
    {
168
        return $this->mappers[$name] ?? null;
169
    }
170
171
    public function addMapper(Mapper $mapper): Orm
172
    {
173
        $mapper->setOrm($this);
174
        $this->mappers[$mapper->getName()] = $mapper;
175
176
        return $this;
177
    }
178
}
179