Completed
Push — master ( 88a09b...db2a6e )
by Gerrit
04:31
created

ListMapping   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 138
Duplicated Lines 8.7 %

Coupling/Cohesion

Components 2
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 18
lcom 2
cbo 3
dl 12
loc 138
ccs 51
cts 51
cp 1
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A getDBALColumn() 0 4 1
A getEntryMapping() 0 4 1
A describeOrigin() 0 4 1
A collectDBALColumns() 0 7 1
A resolveValue() 0 31 4
A revertValue() 0 35 5
A assertValue() 12 12 3
A wakeUpMapping() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 Doctrine\DBAL\Schema\Column;
16
use Webmozart\Assert\Assert;
17
use Addiks\RDMBundle\Hydration\HydrationContextInterface;
18
use Addiks\RDMBundle\Exception\FailedRDMAssertionException;
19
use Symfony\Component\DependencyInjection\ContainerInterface;
20
use Addiks\RDMBundle\Mapping\MappingInterface;
21
22
final class ListMapping implements MappingInterface
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 11
    public function __construct(
41
        Column $column,
42
        MappingInterface $entryMapping,
43
        string $origin = "unknown"
44
    ) {
45 11
        $this->column = $column;
46 11
        $this->entryMapping = $entryMapping;
47 11
        $this->origin = $origin;
48 11
    }
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 2
    public function revertValue(
106
        HydrationContextInterface $context,
107
        $valueFromEntityField
108
    ): array {
109
        /** @var array<string, string> $data */
110 2
        $data = array();
111
112
        /** @var string $columnName */
113 2
        $columnName = $this->column->getName();
114
115
        /** @var array<string> $serializedValues */
116 2
        $serializedValues = array();
117
118 2
        if (is_iterable($valueFromEntityField)) {
119 2
            foreach ($valueFromEntityField as $key => $valueFromEntry) {
120
                /** @var mixed $valueFromEntry */
121
122 2
                $entryData = $this->entryMapping->revertValue(
123 2
                    $context,
124 2
                    $valueFromEntry
125
                );
126
127 2
                if (count($entryData) === 1) {
128 1
                    $serializedValues[$key] = array_values($entryData)[0];
129
130 1
                } elseif (!empty($entryData)) {
131 2
                    $serializedValues[$key] = $entryData;
132
                }
133
            }
134
        }
135
136 2
        $data[$columnName] = json_encode($serializedValues);
137
138 2
        return $data;
139
    }
140
141 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...
142
        HydrationContextInterface $context,
143
        array $dataFromAdditionalColumns,
144
        $actualValue
145
    ): void {
146 2
        if (!is_array($actualValue) && !is_null($actualValue)) {
147 1
            throw FailedRDMAssertionException::expectedArray(
148 1
                $actualValue,
149 1
                $this->origin
150
            );
151
        }
152 1
    }
153
154 1
    public function wakeUpMapping(ContainerInterface $container): void
155
    {
156 1
        $this->entryMapping->wakeUpMapping($container);
157 1
    }
158
159
}
160