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

ListMapping::resolveValue()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 31
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4

Importance

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