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

ServiceMapping   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 90%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 2
dl 0
loc 124
ccs 36
cts 40
cp 0.9
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A __sleep() 0 8 1
A getServiceId() 0 4 1
A isLax() 0 4 1
A describeOrigin() 0 4 1
A collectDBALColumns() 0 4 1
A resolveValue() 0 20 2
A revertValue() 0 6 1
A assertValue() 0 19 3
A wakeUpMapping() 0 4 1
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
 * @license GPL-3.0
8
 * @author Gerrit Addiks <[email protected]>
9
 */
10
11
namespace Addiks\RDMBundle\Mapping;
12
13
use Addiks\RDMBundle\Mapping\ServiceMappingInterface;
14
use Addiks\RDMBundle\Hydration\HydrationContextInterface;
15
use Addiks\RDMBundle\Exception\FailedRDMAssertionException;
16
use ErrorException;
17
use Symfony\Component\DependencyInjection\ContainerInterface;
18
use ReflectionClass;
19
20
final class ServiceMapping implements ServiceMappingInterface
21
{
22
23
    /**
24
     * The service-id of the service to load for given entitiy-field.
25
     *
26
     * @var string
27
     */
28
    private $serviceId;
29
30
    /**
31
     * Set this to true if this field should not be checked for the correct service on persist.
32
     * This check is a safety-net and you should know what you are doing when you are disabling it.
33
     * You have been warned.
34
     *
35
     * @var bool
36
     */
37
    private $lax = false;
38
39
    /**
40
     * @var string
41
     */
42
    private $origin;
43
44
    /**
45
     * @var ContainerInterface
46
     */
47
    private $container;
48
49 21
    public function __construct(
50
        ContainerInterface $container,
51
        string $serviceId,
52
        bool $lax = false,
53
        string $origin = "unknown"
54
    ) {
55 21
        $this->container = $container;
56 21
        $this->serviceId = $serviceId;
57 21
        $this->lax = $lax;
58 21
        $this->origin = $origin;
59 21
    }
60
61 4
    public function __sleep(): array
62
    {
63
        return [
64 4
            'serviceId',
65
            'lax',
66
            'origin',
67
        ];
68
    }
69
70 1
    public function getServiceId(): string
71
    {
72 1
        return $this->serviceId;
73
    }
74
75 2
    public function isLax(): bool
76
    {
77 2
        return $this->lax;
78
    }
79
80 2
    public function describeOrigin(): string
81
    {
82 2
        return $this->origin;
83
    }
84
85 4
    public function collectDBALColumns(): array
86
    {
87 4
        return [];
88
    }
89
90 7
    public function resolveValue(
91
        HydrationContextInterface $context,
92
        array $dataFromAdditionalColumns
93
    ) {
94
        /** @var object $service */
95 7
        $service = null;
0 ignored issues
show
Unused Code introduced by
$service is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
96
97 7
        if (!$this->container->has($this->serviceId)) {
98
            throw new ErrorException(sprintf(
99
                "Referenced non-existent service '%s' %s!",
100
                $this->serviceId,
101
                $this->origin
102
            ));
103
        }
104
105
        /** @var object $service */
106 7
        $service = $this->container->get($this->serviceId);
107
108 7
        return $service;
109
    }
110
111 1
    public function revertValue(
112
        HydrationContextInterface $context,
113
        $valueFromEntityField
114
    ): array {
115 1
        return []; # Nothing to revert to for static services
116
    }
117
118 4
    public function assertValue(
119
        HydrationContextInterface $context,
120
        array $dataFromAdditionalColumns,
121
        $actualService
122
    ): void {
123 4
        if (!$this->lax) {
124
            /** @var object $expectedService */
125 3
            $expectedService = $this->resolveValue($context, $dataFromAdditionalColumns);
126
127 3
            if ($expectedService !== $actualService) {
128 2
                throw FailedRDMAssertionException::expectedDifferentService(
129 2
                    $this->serviceId,
130 2
                    new ReflectionClass($context->getEntityClass()),
131 2
                    $expectedService,
132 2
                    $actualService
133
                );
134
            }
135
        }
136 3
    }
137
138 1
    public function wakeUpMapping(ContainerInterface $container): void
139
    {
140 1
        $this->container = $container;
141 1
    }
142
143
}
144