Completed
Push — master ( 77cc29...840912 )
by Gerrit
10:19
created

ObjectMapping::__construct()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 27
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 13
cts 13
cp 1
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 20
nc 2
nop 8
crap 2

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
/**
3
 * Copyright (C) 2018 Gerrit Addiks.
4
 * This package (including this file) was released under the terms of the GPL-3.0.
5
 * You should have received a copy of the GNU General Public License along with this program.
6
 * If not, see <http://www.gnu.org/licenses/> or send me a mail so i can send you a copy.
7
 *
8
 * @license GPL-3.0
9
 *
10
 * @author Gerrit Addiks <[email protected]>
11
 */
12
13
namespace Addiks\RDMBundle\Mapping;
14
15
use Addiks\RDMBundle\Mapping\ObjectMappingInterface;
16
use Doctrine\DBAL\Schema\Column;
17
use Addiks\RDMBundle\Mapping\MappingInterface;
18
use Webmozart\Assert\Assert;
19
use Addiks\RDMBundle\Hydration\HydrationContextInterface;
20
use Addiks\RDMBundle\Mapping\CallDefinitionInterface;
21
use Addiks\RDMBundle\Exception\FailedRDMAssertionException;
22
use ReflectionClass;
23
use ReflectionProperty;
24
use ReflectionException;
25
use Symfony\Component\DependencyInjection\ContainerInterface;
26
use Doctrine\DBAL\Types\Type;
27
use Doctrine\DBAL\Connection;
28
29
final class ObjectMapping implements ObjectMappingInterface
30
{
31
32
    /**
33
     * @var string
34
     */
35
    private $className;
36
37
    /**
38
     * @var array<MappingInterface>
39
     */
40
    private $fieldMappings = array();
41
42
    /**
43
     * @var Column|null
44
     */
45
    private $column;
46
47
    /**
48
     * @var CallDefinitionInterface|null
49
     */
50
    private $factory;
51
52
    /**
53
     * @var CallDefinitionInterface|null
54
     */
55
    private $serializer;
56
57
    /**
58
     * @var string
59
     */
60
    private $origin;
61
62
    /**
63
     * @var string|null
64
     */
65
    private $id;
66
67
    /**
68
     * @var string|null
69
     */
70
    private $referencedId;
71
72 17
    public function __construct(
73
        string $className,
74
        array $fieldMappings,
75
        Column $column = null,
76
        string $origin = "undefined",
77
        CallDefinitionInterface $factory = null,
78
        CallDefinitionInterface $serializer = null,
79
        string $id = null,
80
        string $referencedId = null
81
    ) {
82 17
        $this->className = $className;
83 17
        $this->factory = $factory;
84 17
        $this->column = $column;
85 17
        $this->serializer = $serializer;
86 17
        $this->origin = $origin;
87 17
        $this->id = $id;
88 17
        $this->referencedId = $referencedId;
89
90 17
        foreach ($fieldMappings as $fieldName => $fieldMapping) {
91
            /** @var MappingInterface $fieldMapping */
92
93 17
            Assert::isInstanceOf($fieldMapping, MappingInterface::class);
94 17
            Assert::false(is_numeric($fieldName));
95
96 17
            $this->fieldMappings[$fieldName] = $fieldMapping;
97
        }
98 17
    }
99
100 1
    public function getClassName(): string
101
    {
102 1
        return $this->className;
103
    }
104
105 1
    public function getDBALColumn(): ?Column
106
    {
107 1
        return $this->column;
108
    }
109
110 1
    public function getFieldMappings(): array
111
    {
112 1
        return $this->fieldMappings;
113
    }
114
115 1
    public function describeOrigin(): string
116
    {
117 1
        return $this->origin;
118
    }
119
120 2
    public function collectDBALColumns(): array
121
    {
122
        /** @var array<Column> $additionalColumns */
123 2
        $additionalColumns = array();
124
125 2
        if ($this->column instanceof Column) {
126 1
            $additionalColumns[] = $this->column;
127
        }
128
129 2
        foreach ($this->fieldMappings as $fieldMapping) {
130
            /** @var MappingInterface $fieldMapping */
131
132 2
            $additionalColumns = array_merge(
133 2
                $additionalColumns,
134 2
                $fieldMapping->collectDBALColumns()
135
            );
136
        }
137
138 2
        return $additionalColumns;
139
    }
140
141 1
    public function getFactory(): ?CallDefinitionInterface
142
    {
143 1
        return $this->factory;
144
    }
145
146 1
    public function getSerializer(): ?CallDefinitionInterface
147
    {
148 1
        return $this->serializer;
149
    }
150
151 1
    public function getId(): ?string
152
    {
153 1
        return $this->id;
154
    }
155
156 1
    public function getReferencedId(): ?string
157
    {
158 1
        return $this->referencedId;
159
    }
160
161 3
    public function resolveValue(
162
        HydrationContextInterface $context,
163
        array $dataFromAdditionalColumns
164
    ) {
165
        /** @var mixed $object */
166 3
        $object = null;
0 ignored issues
show
Unused Code introduced by
$object is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
167
168 3
        $reflectionClass = new ReflectionClass($this->className);
169
170
        /** @var object|string $object */
171 3
        $object = $this->className;
172
173 3
        if ($reflectionClass->isInstantiable()) {
174 3
            $object = $reflectionClass->newInstanceWithoutConstructor();
175
        }
176
177 3
        $context->pushOnObjectHydrationStack($object);
178
179 3
        if (!empty($this->referencedId)) {
180
            $object = $context->getRegisteredValue($this->referencedId);
181
182 3
        } elseif ($this->factory instanceof CallDefinitionInterface) {
183
            /** @var array<string, string> $factoryData */
184 2
            $factoryData = $dataFromAdditionalColumns;
185
186 2
            if ($this->column instanceof Column && !array_key_exists("", $factoryData)) {
187
                /** @var Type $type */
188 1
                $type = $this->column->getType();
189
190
                /** @var string $columnName */
191 1
                $columnName = $this->column->getName();
192
193 1
                if (isset($dataFromAdditionalColumns[$columnName])) {
194
                    /** @var Connection $connection */
195
                    $connection = $context->getEntityManager()->getConnection();
196
197
                    $dataFromAdditionalColumns[$columnName] = $type->convertToPHPValue(
198
                        $dataFromAdditionalColumns[$columnName],
199
                        $connection->getDatabasePlatform()
200
                    );
201
202
                    $factoryData[""] = $dataFromAdditionalColumns[$columnName];
203
                }
204
            }
205
206 2
            $object = $this->factory->execute(
207 2
                $context,
208 2
                $factoryData
209
            );
210
        }
211
212
        // $object may have been replaced during creation, re-assign on top of stack.
213 3
        $context->popFromObjectHydrationStack();
214 3
        $context->pushOnObjectHydrationStack($object);
215
216 3
        if (!empty($this->id) && !$context->hasRegisteredValue($this->id)) {
217 2
            $context->registerValue($this->id, $object);
218
        }
219
220 3
        foreach ($this->fieldMappings as $fieldName => $fieldMapping) {
221
            /** @var MappingInterface $fieldMapping */
222
223
            /** @var mixed $fieldValue */
224 3
            $fieldValue = $fieldMapping->resolveValue(
225 3
                $context,
226 3
                $dataFromAdditionalColumns
227
            );
228
229
            /** @var ReflectionClass $propertyReflectionClass */
230 3
            $propertyReflectionClass = $reflectionClass;
231
232 3
            while (is_object($propertyReflectionClass) && !$propertyReflectionClass->hasProperty($fieldName)) {
233
                $propertyReflectionClass = $propertyReflectionClass->getParentClass();
234
            }
235
236 3
            if (!is_object($propertyReflectionClass)) {
237
                throw new ReflectionException(sprintf("Property %s does not exist", $fieldName));
238
            }
239
240
            /** @var ReflectionProperty $reflectionProperty */
241 3
            $reflectionProperty = $propertyReflectionClass->getProperty($fieldName);
242
243 3
            $reflectionProperty->setAccessible(true);
244 3
            $reflectionProperty->setValue($object, $fieldValue);
245
        }
246
247 3
        $context->popFromObjectHydrationStack();
248
249 3
        return $object;
250
    }
251
252 2
    public function revertValue(
253
        HydrationContextInterface $context,
254
        $valueFromEntityField
255
    ): array {
256
        /** @var array<scalar> $data */
257 2
        $data = array();
258
259 2
        $reflectionClass = new ReflectionClass($this->className);
260
261 2
        $context->pushOnObjectHydrationStack($valueFromEntityField);
262
263 2
        foreach ($this->fieldMappings as $fieldName => $fieldMapping) {
264
            /** @var MappingInterface $fieldMapping */
265
266
            /** @var ReflectionClass $propertyReflectionClass */
267 2
            $propertyReflectionClass = $reflectionClass;
268
269 2
            while (is_object($propertyReflectionClass) && !$propertyReflectionClass->hasProperty($fieldName)) {
270
                $propertyReflectionClass = $propertyReflectionClass->getParentClass();
271
            }
272
273 2
            if (!is_object($propertyReflectionClass)) {
274
                throw new ReflectionException(sprintf("Property %s does not exist", $fieldName));
275
            }
276
277
            /** @var ReflectionProperty $reflectionProperty */
278 2
            $reflectionProperty = $propertyReflectionClass->getProperty($fieldName);
279
280 2
            $reflectionProperty->setAccessible(true);
281 2
            $valueFromField = null;
282
283 2
            if (is_object($valueFromEntityField)) {
284 1
                $valueFromField = $reflectionProperty->getValue($valueFromEntityField);
285
            }
286
287
            /** @var array<string, mixed> $fieldData */
288 2
            $fieldData = $fieldMapping->revertValue(
289 2
                $context,
290 2
                $valueFromField
291
            );
292
293 2
            if (array_key_exists("", $fieldData)) {
294
                $fieldData[$fieldName] = $fieldData[""];
295
                unset($fieldData[""]);
296
            }
297
298 2
            $data = array_merge($data, $fieldData);
299
        }
300
301 2
        if ($this->serializer instanceof CallDefinitionInterface) {
302
            /** @var string $columnName */
303
            $columnName = '';
304
305
            if ($this->column instanceof Column) {
306
                $columnName = $this->column->getName();
307
            }
308
309
            $data[$columnName] = $this->serializer->execute(
310
                $context,
311
                $data
312
            );
313
314
            if ($this->column instanceof Column) {
315
                /** @var Type $type */
316
                $type = $this->column->getType();
317
318
                /** @var Connection $connection */
319
                $connection = $context->getEntityManager()->getConnection();
320
321
                $data[$columnName] = $type->convertToDatabaseValue(
322
                    $data[$columnName],
323
                    $connection->getDatabasePlatform()
324
                );
325
            }
326
        }
327
328 2
        $context->popFromObjectHydrationStack();
329
330 2
        return $data;
331
    }
332
333 2
    public function assertValue(
334
        HydrationContextInterface $context,
335
        array $dataFromAdditionalColumns,
336
        $actualValue
337
    ): void {
338 2
        if (!is_null($actualValue) && !$actualValue instanceof $this->className) {
339 1
            throw FailedRDMAssertionException::expectedInstanceOf(
340 1
                $this->className,
341 1
                $context->getEntityClass(),
342 1
                $this->origin
343
            );
344
        }
345 1
    }
346
347 1
    public function wakeUpMapping(ContainerInterface $container): void
348
    {
349 1
        if ($this->factory instanceof CallDefinitionInterface) {
350 1
            $this->factory->wakeUpCall($container);
351
        }
352
353 1
        if ($this->serializer instanceof CallDefinitionInterface) {
354 1
            $this->serializer->wakeUpCall($container);
355
        }
356 1
    }
357
358
}
359