Passed
Pull Request — master (#3)
by
unknown
11:20
created

MapperAttributed   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 193
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 32
eloc 58
dl 0
loc 193
rs 9.84
c 1
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
C getAllVariables() 0 43 12
A processComplexes() 0 2 1
A getAnnotation() 0 11 2
A getTableName() 0 11 2
A processEmbedded() 0 2 1
A processConstraints() 0 23 6
A name() 0 3 1
A processColumns() 0 10 3
A __construct() 0 4 1
A getScheme() 0 11 2
A getEntityClass() 0 3 1
1
<?php
2
/********************************************************************************
3
 *   Apache License, Version 2.0                                                *
4
 *                                                                              *
5
 *   Copyright [2020] [Nurlan Mukhanov <[email protected]>]                      *
6
 *                                                                              *
7
 *   Licensed under the Apache License, Version 2.0 (the "License");            *
8
 *   you may not use this file except in compliance with the License.           *
9
 *   You may obtain a copy of the License at                                    *
10
 *                                                                              *
11
 *       http://www.apache.org/licenses/LICENSE-2.0                             *
12
 *                                                                              *
13
 *   Unless required by applicable law or agreed to in writing, software        *
14
 *   distributed under the License is distributed on an "AS IS" BASIS,          *
15
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   *
16
 *   See the License for the specific language governing permissions and        *
17
 *   limitations under the License.                                             *
18
 *                                                                              *
19
 ********************************************************************************/
20
21
declare(strict_types=1);
22
23
namespace DBD\Entity;
24
25
use DBD\Entity\Common\EntityException;
26
use DBD\Entity\Interfaces\EntityMapper;
27
use ReflectionClass;
28
use ReflectionException;
29
30
31
/**
32
 * Class MapperAttributed
33
 *
34
 * @package DBD\Entity
35
 */
36
class MapperAttributed implements EntityMapper
37
{
38
    use MapperTrait;
0 ignored issues
show
introduced by
The trait DBD\Entity\MapperTrait requires some properties which are not provided by DBD\Entity\MapperAttributed: $embedded, $tables, $complex
Loading history...
39
40
    /**
41
     * @throws ReflectionException
42
     */
43
    public function __construct(
44
        protected string $entityClass
45
    ) {
46
        $this->getAllVariables();
47
    }
48
49
    public function name(): string
50
    {
51
        return substr($this->entityClass, strrpos($this->entityClass, '\\') + 1). 'Map';
52
    }
53
54
    public function getEntityClass(): string
55
    {
56
        return $this->entityClass;
57
    }
58
59
    /**
60
     * @return string
61
     * @throws ReflectionException
62
     * @throws EntityException
63
     */
64
    public function getScheme(): string
65
    {
66
        $reflection = new ReflectionClass($this->entityClass);
67
68
        $attributes = $reflection->getAttributes(EntityTable::class);
69
70
        foreach ($attributes as $attribute) {
71
            return $attribute->newInstance()->scheme;
72
        }
73
74
        throw new EntityException('Missing attribute scheme for ' . $this->entityClass);
75
    }
76
77
    /**
78
     * @return string
79
     * @throws EntityException
80
     * @throws ReflectionException
81
     */
82
    public function getTableName(): string
83
    {
84
        $reflection = new ReflectionClass($this->entityClass);
85
86
        $attributes = $reflection->getAttributes(EntityTable::class);
87
88
        foreach ($attributes as $attribute) {
89
            return $attribute->newInstance()->name;
90
        }
91
92
        throw new EntityException('Missing attribute name for ' . $this->entityClass);
93
    }
94
95
    /**
96
     * @return string
97
     * @throws EntityException
98
     * @throws ReflectionException
99
     */
100
    public function getAnnotation(): string
101
    {
102
        $reflection = new ReflectionClass($this->entityClass);
103
104
        $attributes = $reflection->getAttributes(EntityTable::class);
105
106
        foreach ($attributes as $attribute) {
107
            return $attribute->newInstance()->annotation;
108
        }
109
110
        throw new EntityException('Missing attribute annotation for ' . $this->entityClass);
111
    }
112
113
    /**
114
     * @return MapperVariables
115
     * @throws ReflectionException
116
     */
117
    public function getAllVariables(): MapperVariables
118
    {
119
        $thisName = $this->name();
120
121
        if (!isset(MapperCache::me()->allVariables[$thisName])) {
122
            $reflectionClass = new ReflectionClass($this->entityClass);
123
124
            $properties = $reflectionClass->getProperties();
125
126
            /**
127
             * @var Constraint[] $constraints
128
             * @var Embedded[] $embedded
129
             * @var Complex[] $complexes
130
             * @var Column[] $columns
131
             */
132
            $constraints = $embedded = $complexes = $columns = [];
133
134
            foreach ($properties as $property) {
135
                $attributes = $property->getAttributes();
136
137
                foreach ($attributes as $attribute) {
138
                    if ($attribute->getName() === Column::class || is_subclass_of($attribute->getName(), Column::class)) {
139
                        $columns[$property->getName()] = $attribute->newInstance();
140
                    }
141
                    else if ($attribute->getName() === Constraint::class || is_subclass_of($attribute->getName(), Constraint::class)) {
142
                        $constraints[$property->getName()] = $attribute->newInstance();
143
                    }
144
                    else if ($attribute->getName() === Embedded::class || is_subclass_of($attribute->getName(), Embedded::class)) {
145
                        $embedded[$property->getName()] = $attribute->newInstance();
146
                    }
147
                    else if ($attribute->getName() === Complex::class || is_subclass_of($attribute->getName(), Complex::class)) {
148
                        $complexes[$property->getName()] = $attribute->newInstance();
149
                    }
150
                }
151
            }
152
153
            $this->processComplexes($complexes);
154
            $this->processEmbedded($embedded);
155
            $this->processColumns($columns);
156
            $this->processConstraints($constraints, $columns, $embedded, $complexes);
157
        }
158
159
        return MapperCache::me()->allVariables[$thisName];
160
    }
161
162
    /**
163
     * @param Complex[] $complexes
164
     * @return void
165
     */
166
    private function processComplexes(array $complexes): void
0 ignored issues
show
Unused Code introduced by
The parameter $complexes is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

166
    private function processComplexes(/** @scrutinizer ignore-unused */ array $complexes): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
167
    {
168
169
    }
170
171
    /**
172
     * @param Embedded[] $embedded
173
     * @return void
174
     */
175
    private function processEmbedded(array $embedded): void
0 ignored issues
show
Unused Code introduced by
The parameter $embedded is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

175
    private function processEmbedded(/** @scrutinizer ignore-unused */ array $embedded): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
176
    {
177
178
    }
179
180
    /**
181
     * @param Column[] $columns
182
     * @return void
183
     */
184
    private function processColumns(array $columns): void
185
    {
186
        $thisName = $this->name();
187
188
        if (!isset(MapperCache::me()->columns[$thisName])) {
189
            MapperCache::me()->columns[$thisName] = [];
190
        }
191
192
        foreach ($columns as $columnName => $column) {
193
            MapperCache::me()->columns[$thisName][$columnName] = $column;
194
        }
195
    }
196
197
    /**
198
     * @param Constraint[] $constraints
199
     * @param Column[] $columns
200
     * @param Embedded[] $embedded
201
     * @param Complex[] $complexes
202
     * @return void
203
     * @throws EntityException
204
     * @throws ReflectionException
205
     */
206
    private function processConstraints(array $constraints, array $columns, array $embedded, array $complexes): void
207
    {
208
        $thisName = $this->name();
209
210
        if (!isset(MapperCache::me()->constraints[$thisName])) {
211
            MapperCache::me()->constraints[$thisName] = [];
212
213
            $entityClass = $this->getEntityClass();
214
215
            foreach ($constraints as $constraintName => $constraint) {
216
                if ($entityClass !== View::class && is_string($constraint->localColumn)) {
217
                    $constraint->localColumn = $this->findColumnByOriginName($constraint->localColumn);
218
                }
219
220
                MapperCache::me()->constraints[$thisName][$constraintName] = $constraint;
221
            }
222
223
            MapperCache::me()->allVariables[$thisName] = new MapperVariables($columns, $constraints, $embedded, $complexes);
224
225
            foreach (MapperCache::me()->constraints[$thisName] as $constraintName => $constraint) {
226
                $constraint->localTable = $this->getTable();
227
228
                $this->$constraintName = $constraint;
229
            }
230
        }
231
    }
232
}
233