|
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; |
|
|
|
|
|
|
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
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.