Completed
Push — master ( 203e38...2cdd54 )
by Gerrit
13:04
created

ObjectValueResolver::assertValue()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 9
cts 9
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 13
nc 3
nop 4
crap 4
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\ValueResolver;
14
15
use ReflectionClass;
16
use Symfony\Component\DependencyInjection\ContainerInterface;
17
use Addiks\RDMBundle\ValueResolver\ValueResolverInterface;
18
use Addiks\RDMBundle\Mapping\ObjectMappingInterface;
19
use Addiks\RDMBundle\Mapping\CallDefinitionInterface;
20
use Addiks\RDMBundle\Mapping\MappingInterface;
21
use ReflectionProperty;
22
use Addiks\RDMBundle\Exception\FailedRDMAssertionException;
23
use Addiks\RDMBundle\ValueResolver\CallDefinitionExecuterInterface;
24
25
final class ObjectValueResolver implements ValueResolverInterface
26
{
27
28
    /**
29
     * @var ContainerInterface
30
     */
31
    private $container;
32
33
    /**
34
     * @var ValueResolverInterface
35
     */
36
    private $fieldValueResolver;
37
38
    /**
39
     * @var CallDefinitionExecuterInterface
40
     */
41
    private $callDefinitionExecuter;
42
43 7
    public function __construct(
44
        ContainerInterface $container,
45
        ValueResolverInterface $fieldValueResolver,
46
        CallDefinitionExecuterInterface $callDefinitionExecuter
47
    ) {
48 7
        $this->container = $container;
49 7
        $this->fieldValueResolver = $fieldValueResolver;
50 7
        $this->callDefinitionExecuter = $callDefinitionExecuter;
51 7
    }
52
53 2
    public function resolveValue(
54
        MappingInterface $objectMapping,
55
        $entity,
56
        array $dataFromAdditionalColumns
57
    ) {
58
        /** @var mixed $object */
59 2
        $object = null;
60
61 2
        if ($objectMapping instanceof ObjectMappingInterface) {
62
            /** @var string $className */
63 2
            $className = $objectMapping->getClassName();
64
65
            /** @var null|CallDefinitionInterface $factory */
66 2
            $factory = $objectMapping->getFactory();
67
68 2
            $reflectionClass = new ReflectionClass($className);
69
70 2
            if ($factory instanceof CallDefinitionInterface) {
71
                $object = $this->callDefinitionExecuter->executeCallDefinition(
72
                    $factory,
73
                    $entity,
74
                    $dataFromAdditionalColumns
75
                );
76
77
            } else {
78
                /** @var object $object */
79 2
                $object = $reflectionClass->newInstanceWithoutConstructor();
80
            }
81
82 2
            foreach ($objectMapping->getFieldMappings() as $fieldName => $fieldMapping) {
83
                /** @var MappingInterface $fieldMapping */
84
85
                /** @var mixed $fieldValue */
86 2
                $fieldValue = $this->fieldValueResolver->resolveValue(
87 2
                    $fieldMapping,
88 2
                    $entity,
89 2
                    $dataFromAdditionalColumns
90
                );
91
92
                /** @var ReflectionProperty $reflectionProperty */
93 2
                $reflectionProperty = $reflectionClass->getProperty($fieldName);
94
95 2
                $reflectionProperty->setAccessible(true);
96 2
                $reflectionProperty->setValue($object, $fieldValue);
97
            }
98
99
        }
100
101 2
        return $object;
102
    }
103
104 2
    public function revertValue(
105
        MappingInterface $objectMapping,
106
        $entity,
107
        $valueFromEntityField
108
    ): array {
109
        /** @var array<scalar> $data */
110 2
        $data = array();
111
112 2
        if ($objectMapping instanceof ObjectMappingInterface) {
113
            /** @var string $className */
114 2
            $className = $objectMapping->getClassName();
115
116 2
            $reflectionClass = new ReflectionClass($className);
117
118 2 View Code Duplication
            foreach ($objectMapping->getFieldMappings() as $fieldName => $fieldMapping) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
119
                /** @var MappingInterface $fieldMapping */
120
121
                /** @var ReflectionProperty $reflectionProperty */
122 2
                $reflectionProperty = $reflectionClass->getProperty($fieldName);
123
124 2
                $reflectionProperty->setAccessible(true);
125 2
                $valueFromField = null;
126
127 2
                if (is_object($valueFromEntityField)) {
128 1
                    $valueFromField = $reflectionProperty->getValue($valueFromEntityField);
129
                }
130
131 2
                $data = array_merge($data, $this->fieldValueResolver->revertValue(
132 2
                    $fieldMapping,
133 2
                    $entity,
134 2
                    $valueFromField
135
                ));
136
            }
137
        }
138
139 2
        return $data;
140
    }
141
142 2
    public function assertValue(
143
        MappingInterface $objectMapping,
144
        $entity,
145
        array $dataFromAdditionalColumns,
146
        $actualValue
147
    ): void {
148 2
        if ($objectMapping instanceof ObjectMappingInterface) {
149
            /** @var string $className */
150 2
            $className = $objectMapping->getClassName();
151
152 2
            if (!is_null($actualValue) && !$actualValue instanceof $className) {
153 1
                throw FailedRDMAssertionException::expectedInstanceOf(
154 1
                    $objectMapping->getClassName(),
155 1
                    get_class($entity),
156 1
                    $objectMapping->describeOrigin()
157
                );
158
            }
159
        }
160 1
    }
161
162
}
163