1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the eav package. |
4
|
|
|
* |
5
|
|
|
* @author Alex Kuperwood <[email protected]> |
6
|
|
|
* @copyright 2025 Alex Kuperwood |
7
|
|
|
* @license https://opensource.org/license/mit The MIT License |
8
|
|
|
*/ |
9
|
|
|
declare(strict_types=1); |
10
|
|
|
|
11
|
|
|
namespace Kuperwood\Eav; |
12
|
|
|
|
13
|
|
|
use Doctrine\DBAL\Exception; |
|
|
|
|
14
|
|
|
use Kuperwood\Eav\Enum\_ENTITY; |
15
|
|
|
use Kuperwood\Eav\Exception\EntityException; |
16
|
|
|
use Kuperwood\Eav\Result\Result; |
17
|
|
|
use Kuperwood\Eav\Traits\SingletonsTrait; |
18
|
|
|
|
19
|
|
|
class EntityGnome |
20
|
|
|
{ |
21
|
|
|
use SingletonsTrait; |
22
|
|
|
|
23
|
|
|
private Entity $entity; |
24
|
|
|
|
25
|
1 |
|
public function __construct(Entity $entity) |
26
|
|
|
{ |
27
|
1 |
|
$this->entity = $entity; |
28
|
|
|
} |
29
|
|
|
|
30
|
1 |
|
public function getEntity(): Entity |
31
|
|
|
{ |
32
|
1 |
|
return $this->entity; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @throws EntityException |
37
|
|
|
* @throws Exception |
38
|
|
|
*/ |
39
|
10 |
|
public function beforeSave(): int |
40
|
|
|
{ |
41
|
10 |
|
$entity = $this->getEntity(); |
42
|
10 |
|
$set = $entity->getAttributeSet(); |
43
|
10 |
|
if (!$entity->hasKey()) { |
44
|
6 |
|
if (!$entity->hasDomainKey()) { |
45
|
1 |
|
EntityException::undefinedDomainKey(); |
46
|
|
|
} |
47
|
5 |
|
if (!$set->hasKey()) { |
48
|
1 |
|
EntityException::undefinedAttributeSetKey(); |
49
|
|
|
} |
50
|
4 |
|
$domainKey = $entity->getDomainKey(); |
51
|
4 |
|
$this->checkDomainExists($domainKey); |
52
|
3 |
|
$setKey = $set->getKey(); |
53
|
3 |
|
$this->checkAttrSetExist($setKey); |
54
|
2 |
|
$model = $this->makeEntityModel(); |
55
|
2 |
|
$key = $model->create([ |
56
|
2 |
|
_ENTITY::DOMAIN_ID => $domainKey, |
57
|
2 |
|
_ENTITY::ATTR_SET_ID => $setKey |
58
|
2 |
|
]); |
59
|
2 |
|
$entity->setKey($key); |
60
|
|
|
|
61
|
2 |
|
return 1; |
62
|
|
|
} |
63
|
4 |
|
$key = $entity->getKey(); |
64
|
4 |
|
$record = $this->checkEntityExists($key); |
65
|
3 |
|
$domainKey = (int) $record[_ENTITY::DOMAIN_ID]; |
66
|
3 |
|
$setKey = (int) $record[_ENTITY::ATTR_SET_ID]; |
67
|
|
|
|
68
|
3 |
|
$this->checkDomainExists($domainKey); |
69
|
2 |
|
$this->checkAttrSetExist($setKey); |
70
|
1 |
|
$set->setKey($setKey); |
71
|
1 |
|
$entity->setDomainKey($domainKey); |
72
|
|
|
|
73
|
1 |
|
return 2; |
74
|
|
|
} |
75
|
|
|
|
76
|
3 |
|
public function find(): Result |
77
|
|
|
{ |
78
|
3 |
|
$result = new Result(); |
79
|
|
|
|
80
|
3 |
|
$entity = $this->getEntity(); |
81
|
|
|
|
82
|
3 |
|
if (!$entity->hasKey()) { |
83
|
1 |
|
return $result->empty(); |
84
|
|
|
} |
85
|
2 |
|
$key = $entity->getKey(); |
86
|
|
|
|
87
|
2 |
|
$model = $this->makeEntityModel(); |
88
|
2 |
|
$record = $model->findByKey($key); |
89
|
|
|
|
90
|
2 |
|
if ($record === false) { |
91
|
1 |
|
return $result->notFound(); |
92
|
|
|
} |
93
|
|
|
|
94
|
1 |
|
$entity->setDomainKey($record[_ENTITY::DOMAIN_ID]); |
95
|
1 |
|
$set = $entity->getAttributeSet(); |
96
|
1 |
|
$set->setKey($record[_ENTITY::ATTR_SET_ID]); |
97
|
1 |
|
$set->fetchContainers(); |
98
|
|
|
|
99
|
1 |
|
$result->found(); |
100
|
|
|
|
101
|
1 |
|
return $result; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @throws EntityException |
106
|
|
|
* @throws Exception |
107
|
|
|
*/ |
108
|
14 |
|
public function save(): Result |
109
|
|
|
{ |
110
|
14 |
|
$entity = $this->getEntity(); |
111
|
14 |
|
$result = new Result(); |
112
|
14 |
|
$operationType = $this->beforeSave(); |
113
|
7 |
|
$set = $entity->getAttributeSet(); |
114
|
7 |
|
$set->fetchContainers(); |
115
|
7 |
|
$valueResults = []; |
116
|
7 |
|
foreach ($set->getContainers() as $container) { |
117
|
2 |
|
$attribute = $container->getAttribute(); |
118
|
2 |
|
$valueResults[$attribute->getName()] = $container->getStrategy()->save(); |
119
|
|
|
} |
120
|
7 |
|
1 == $operationType |
121
|
3 |
|
? $result->created() |
122
|
4 |
|
: $result->updated(); |
123
|
7 |
|
$result->setData($valueResults); |
124
|
7 |
|
$bag = $entity->getBag(); |
125
|
7 |
|
$bag->clear(); |
126
|
|
|
|
127
|
7 |
|
return $result; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @throws EntityException |
132
|
|
|
*/ |
133
|
4 |
|
public function delete() |
134
|
|
|
{ |
135
|
4 |
|
$entity = $this->getEntity(); |
136
|
4 |
|
$set = $entity->getAttributeSet(); |
137
|
|
|
|
138
|
4 |
|
if (!$entity->hasKey()) { |
139
|
1 |
|
EntityException::undefinedEntityKey(); |
140
|
|
|
} |
141
|
3 |
|
if (!$set->hasKey()) { |
142
|
1 |
|
EntityException::undefinedAttributeSetKey(); |
143
|
|
|
} |
144
|
|
|
|
145
|
2 |
|
$result = new Result(); |
146
|
2 |
|
$set->fetchContainers(); |
147
|
2 |
|
$deleteResults = []; |
148
|
2 |
|
foreach ($set->getContainers() as $container) { |
149
|
1 |
|
$attribute = $container->getAttribute(); |
150
|
1 |
|
$deleteResults[$attribute->getName()] = $container->getStrategy()->delete(); |
151
|
|
|
} |
152
|
2 |
|
$entityModel = $this->makeEntityModel(); |
153
|
2 |
|
$recordResult = $entityModel->deleteByKey($entity->getKey()); |
154
|
2 |
|
if (!$recordResult) { |
155
|
1 |
|
return $result->notDeleted(); |
156
|
|
|
} |
157
|
|
|
|
158
|
1 |
|
$entity->setKey(0); |
159
|
1 |
|
$entity->setDomainKey(0); |
160
|
1 |
|
$set->setKey(0); |
161
|
1 |
|
$set->resetContainers(); |
162
|
|
|
|
163
|
1 |
|
$result->deleted(); |
164
|
1 |
|
$result->setData($deleteResults); |
165
|
|
|
|
166
|
1 |
|
return $result; |
167
|
|
|
} |
168
|
|
|
|
169
|
2 |
|
public function validate(): Result |
170
|
|
|
{ |
171
|
2 |
|
$result = new Result(); |
172
|
2 |
|
$result->validationPassed(); |
173
|
2 |
|
$entity = $this->getEntity(); |
174
|
2 |
|
$set = $entity->getAttributeSet(); |
175
|
2 |
|
$set->fetchContainers(); |
176
|
2 |
|
$errors = []; |
177
|
2 |
|
foreach ($set->getContainers() as $container) { |
178
|
2 |
|
$validationResult = $container->getValueValidator()->validateField(); |
179
|
2 |
|
if (!is_null($validationResult)) { |
180
|
1 |
|
$errors[$container->getAttribute()->getName()] = $validationResult; |
181
|
|
|
} |
182
|
|
|
} |
183
|
2 |
|
if (count($errors) > 0) { |
184
|
1 |
|
$result->validationFails(); |
185
|
1 |
|
$result->setData($errors); |
186
|
|
|
} |
187
|
|
|
|
188
|
2 |
|
return $result; |
189
|
|
|
} |
190
|
|
|
|
191
|
1 |
|
public function toArray(): array |
192
|
|
|
{ |
193
|
1 |
|
$result = []; |
194
|
1 |
|
$entity = $this->getEntity(); |
195
|
1 |
|
foreach ($entity->getAttributeSet()->getContainers() as $container) { |
196
|
1 |
|
$result[$container->getAttribute()->getName()] = $container->getValueManager()->getValue(); |
197
|
|
|
} |
198
|
|
|
|
199
|
1 |
|
return $result; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @throws EntityException |
204
|
|
|
*/ |
205
|
|
|
private function checkEntityExists(int $key): array |
206
|
|
|
{ |
207
|
|
|
$entity = $this->makeEntityModel(); |
208
|
|
|
|
209
|
|
|
$result = $entity->findByKey($key); |
210
|
|
|
if ($result === false) |
211
|
|
|
EntityException::entityNotFound(); |
212
|
|
|
|
213
|
|
|
return $result; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @throws EntityException |
218
|
|
|
*/ |
219
|
1 |
|
private function checkDomainExists(int $key): void |
220
|
|
|
{ |
221
|
1 |
|
$domain = $this->makeDomainModel(); |
222
|
|
|
|
223
|
1 |
|
if ($domain->findByKey($key) === false) |
224
|
1 |
|
EntityException::domainNotFound(); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* @throws EntityException |
229
|
|
|
*/ |
230
|
1 |
|
private function checkAttrSetExist(int $key): void |
231
|
|
|
{ |
232
|
1 |
|
$model = $this->makeAttributeSetModel(); |
233
|
|
|
|
234
|
1 |
|
if($model->findByKey($key) === false) { |
235
|
1 |
|
EntityException::attrSetNotFound(); |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
} |
239
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths