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

ArrayMapping::revertValue()   B

Complexity

Conditions 4
Paths 2

Size

Total Lines 30
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 30
ccs 13
cts 13
cp 1
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 16
nc 2
nop 2
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\Mapping;
14
15
use Addiks\RDMBundle\Mapping\MappingInterface;
16
use Webmozart\Assert\Assert;
17
use Doctrine\DBAL\Schema\Column;
18
use Addiks\RDMBundle\Mapping\ArrayMappingInterface;
19
use Addiks\RDMBundle\Hydration\HydrationContextInterface;
20
use Addiks\RDMBundle\Exception\FailedRDMAssertionException;
21
use Symfony\Component\DependencyInjection\ContainerInterface;
22
23
final class ArrayMapping implements ArrayMappingInterface
24
{
25
26
    /**
27
     * @var array<MappingInterface>
28
     */
29
    private $entryMappings = array();
30
31
    /**
32
     * @var string
33
     */
34
    private $origin;
35
36 11
    public function __construct(array $entryMappings, string $origin = "unknown")
37
    {
38 11
        $this->origin = $origin;
39
40 11
        foreach ($entryMappings as $key => $entryMapping) {
41
            /** @var MappingInterface $entryMapping */
42
43 11
            Assert::isInstanceOf($entryMapping, MappingInterface::class);
44
45 11
            $this->entryMappings[$key] = $entryMapping;
46
        }
47 11
    }
48
49 1
    public function getEntryMappings(): array
50
    {
51 1
        return $this->entryMappings;
52
    }
53
54 1
    public function describeOrigin(): string
55
    {
56 1
        return $this->origin;
57
    }
58
59 2
    public function collectDBALColumns(): array
60
    {
61
        /** @var array<Column> $dbalColumns */
62 2
        $dbalColumns = array();
63
64 2
        foreach ($this->entryMappings as $entryMapping) {
65
            /** @var MappingInterface $entryMapping */
66
67 2
            $dbalColumns = array_merge(
68 2
                $dbalColumns,
69 2
                $entryMapping->collectDBALColumns()
70
            );
71
        }
72
73 2
        return $dbalColumns;
74
    }
75
76 2
    public function resolveValue(
77
        HydrationContextInterface $context,
78
        array $dataFromAdditionalColumns
79
    ) {
80
        /** @var array<mixed> $value */
81 2
        $value = array();
82
83 2
        foreach ($this->entryMappings as $key => $entryMapping) {
84
            /** @var MappingInterface $entryMapping */
85
86 2
            $value[$key] = $entryMapping->resolveValue(
87 2
                $context,
88 2
                $dataFromAdditionalColumns
89
            );
90
        }
91
92 2
        return $value;
93
    }
94
95 3
    public function revertValue(
96
        HydrationContextInterface $context,
97
        $valueFromEntityField
98
    ): array {
99
        /** @var array<string, string> $data */
100 3
        $data = array();
101
102 3
        if (is_array($valueFromEntityField)) {
103 1
            foreach ($this->entryMappings as $key => $entryMapping) {
104
                /** @var MappingInterface $entryMapping */
105
106
                /** @var mixed $valueFromEntry */
107 1
                $valueFromEntry = null;
108
109 1
                if (isset($valueFromEntityField[$key])) {
110 1
                    $valueFromEntry = $valueFromEntityField[$key];
111
                }
112
113 1
                $data = array_merge(
114 1
                    $data,
115 1
                    $entryMapping->revertValue(
116 1
                        $context,
117 1
                        $valueFromEntry
118
                    )
119
                );
120
            }
121
        }
122
123 3
        return $data;
124
    }
125
126 2 View Code Duplication
    public function assertValue(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
127
        HydrationContextInterface $context,
128
        array $dataFromAdditionalColumns,
129
        $actualValue
130
    ): void {
131 2
        if (!is_array($actualValue) && !is_null($actualValue)) {
132 1
            throw FailedRDMAssertionException::expectedArray(
133 1
                $actualValue,
134 1
                $this->origin
135
            );
136
        }
137 1
    }
138
139 1
    public function wakeUpMapping(ContainerInterface $container): void
140
    {
141 1
        foreach ($this->entryMappings as $key => $entryMapping) {
142
            /** @var MappingInterface $entryMapping */
143
144 1
            $entryMapping->wakeUpMapping($container);
145
        }
146 1
    }
147
}
148