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

ListMapping   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 136
Duplicated Lines 8.82 %

Coupling/Cohesion

Components 2
Dependencies 3

Test Coverage

Coverage 98%

Importance

Changes 0
Metric Value
wmc 17
lcom 2
cbo 3
dl 12
loc 136
ccs 49
cts 50
cp 0.98
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
B resolveValue() 0 31 4
B revertValue() 0 33 4
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 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