Test Failed
Push — master ( 86efb6...f66ff9 )
by Gerrit
15:32
created

ArrayMapping::assertValue()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12

Duplication

Lines 12
Ratio 100 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

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