Passed
Push — master ( 356755...14835f )
by Gerrit
04:25
created

ListValueResolver   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 110
Duplicated Lines 13.64 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 97.67%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 4
dl 15
loc 110
ccs 42
cts 43
cp 0.9767
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B resolveValue() 0 38 5
B revertValue() 0 40 6
A assertValue() 15 15 4

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\ValueResolver;
14
15
use Addiks\RDMBundle\ValueResolver\ValueResolverInterface;
16
use Addiks\RDMBundle\Exception\FailedRDMAssertionException;
17
use Addiks\RDMBundle\Mapping\ListMappingInterface;
18
use Addiks\RDMBundle\Mapping\MappingInterface;
19
use Addiks\RDMBundle\Hydration\HydrationContextInterface;
20
21
class ListValueResolver implements ValueResolverInterface
22
{
23
24
    /**
25
     * @var ValueResolverInterface
26
     */
27
    private $entryValueResolver;
28
29 9
    public function __construct(ValueResolverInterface $entryValueResolver)
30
    {
31 9
        $this->entryValueResolver = $entryValueResolver;
32 9
    }
33
34 1
    public function resolveValue(
35
        MappingInterface $listMapping,
36
        HydrationContextInterface $context,
37
        array $dataFromAdditionalColumns
38
    ) {
39
        /** @var null|array<mixed> $value */
40 1
        $value = null;
41
42 1
        if ($listMapping instanceof ListMappingInterface) {
43
            /** @var MappingInterface $entryMapping */
44 1
            $entryMapping = $listMapping->getEntryMapping();
45
46
            /** @var string $columnName */
47 1
            $columnName = $listMapping->getDBALColumn()->getName();
48
49 1
            if (isset($dataFromAdditionalColumns[$columnName])) {
50
                /** @var array<string> $rawValues */
51 1
                $serializedValues = json_decode($dataFromAdditionalColumns[$columnName], true);
52
53 1
                if (is_array($serializedValues)) {
54 1
                    $value = array();
55
56 1
                    foreach ($serializedValues as $key => $entryData) {
57 1
                        $value[$key] = $this->entryValueResolver->resolveValue(
58 1
                            $entryMapping,
59 1
                            $context,
60
                            [
61 1
                                '' => $entryData,
62 1
                                $columnName => $entryData
63
                            ]
64
                        );
65
                    }
66
                }
67
            }
68
        }
69
70 1
        return $value;
71
    }
72
73 2
    public function revertValue(
74
        MappingInterface $listMapping,
75
        HydrationContextInterface $context,
76
        $valueFromEntityField
77
    ): array {
78
        /** @var array<string, string> $data */
79 2
        $data = array();
80
81 2
        if ($listMapping instanceof ListMappingInterface && is_array($valueFromEntityField)) {
82
            /** @var MappingInterface $entryMapping */
83 1
            $entryMapping = $listMapping->getEntryMapping();
84
85
            /** @var string $columnName */
86 1
            $columnName = $listMapping->getDBALColumn()->getName();
87
88
            /** @var array<string> $serializedValues */
89 1
            $serializedValues = array();
90
91 1
            foreach ($valueFromEntityField as $key => $valueFromEntry) {
92
                /** @var mixed $valueFromEntry */
93
94 1
                $entryData = $this->entryValueResolver->revertValue(
95 1
                    $entryMapping,
96 1
                    $context,
97 1
                    $valueFromEntry
98
                );
99
100 1
                if (count($entryData) === 1) {
101 1
                    $serializedValues[$key] = array_values($entryData)[0];
102
103
                } elseif (isset($entryData[$columnName])) {
104 1
                    $serializedValues[$key] = $entryData;
105
                }
106
            }
107
108 1
            $data[$columnName] = json_encode($serializedValues);
109
        }
110
111 2
        return $data;
112
    }
113
114 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...
115
        MappingInterface $listMapping,
116
        HydrationContextInterface $context,
117
        array $dataFromAdditionalColumns,
118
        $actualValue
119
    ): void {
120 2
        if ($listMapping instanceof ListMappingInterface) {
121 2
            if (!is_array($actualValue) && !is_null($actualValue)) {
122 1
                throw FailedRDMAssertionException::expectedArray(
123 1
                    $actualValue,
124 1
                    $listMapping->describeOrigin()
125
                );
126
            }
127
        }
128 1
    }
129
130
}
131