-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathPatchService.php
More file actions
257 lines (226 loc) · 9.33 KB
/
PatchService.php
File metadata and controls
257 lines (226 loc) · 9.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
<?php
declare(strict_types=1);
/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/
namespace Pimcore\Bundle\StudioBackendBundle\Patcher\Service;
use Exception;
use Pimcore\Bundle\GenericExecutionEngineBundle\Agent\JobExecutionAgentInterface;
use Pimcore\Bundle\GenericExecutionEngineBundle\Model\Job;
use Pimcore\Bundle\GenericExecutionEngineBundle\Model\JobStep;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Service\DataAdapterServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Util\Trait\ValidateObjectDataTrait;
use Pimcore\Bundle\StudioBackendBundle\Element\ExecutionEngine\AutomationAction\Messenger\Messages\PatchFolderMessage;
use Pimcore\Bundle\StudioBackendBundle\Element\ExecutionEngine\AutomationAction\Messenger\Messages\PatchMessage;
use Pimcore\Bundle\StudioBackendBundle\Element\ExecutionEngine\Util\JobSteps;
use Pimcore\Bundle\StudioBackendBundle\Element\Service\ElementSaveServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Element\Service\ElementServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\ElementExistsException;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\ElementSavingFailedException;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\ForbiddenException;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\InvalidArgumentException;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\NotFoundException;
use Pimcore\Bundle\StudioBackendBundle\ExecutionEngine\Util\Config;
use Pimcore\Bundle\StudioBackendBundle\ExecutionEngine\Util\Jobs;
use Pimcore\Bundle\StudioBackendBundle\MappedParameter\PatchFolderParameter;
use Pimcore\Bundle\StudioBackendBundle\Updater\Service\UpdateServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Util\Constant\DataObject\FieldKeys;
use Pimcore\Bundle\StudioBackendBundle\Util\Constant\PatchDataKeys;
use Pimcore\Bundle\StudioBackendBundle\Util\Constant\PatcherActions;
use Pimcore\Model\DataObject\Concrete;
use Pimcore\Model\Element\DuplicateFullPathException;
use Pimcore\Model\Element\ElementDescriptor;
use Pimcore\Model\Element\ElementInterface;
use Pimcore\Model\UserInterface;
use function array_key_exists;
use function count;
use function sprintf;
/**
* @internal
*/
final readonly class PatchService implements PatchServiceInterface
{
use ValidateObjectDataTrait;
public function __construct(
private AdapterLoaderInterface $adapterLoader,
private DataAdapterServiceInterface $dataAdapterService,
private ElementServiceInterface $elementService,
private JobExecutionAgentInterface $jobExecutionAgent,
private ElementSaveServiceInterface $elementSaveService
) {
}
/**
* @throws ForbiddenException|ElementSavingFailedException|NotFoundException|InvalidArgumentException
*/
public function patch(
string $elementType,
array $patchData,
UserInterface $user,
): ?int {
if (count($patchData) > 1) {
return $this->patchAsynchronously($elementType, $patchData, $user);
}
$element = $this->elementService->getAllowedElementById($elementType, $patchData[0]['id'], $user);
$this->patchElement($element, $elementType, $patchData[0], $user);
return null;
}
public function patchFolder(
string $elementType,
PatchFolderParameter $patchFolderParameter,
UserInterface $user,
): ?int {
$job = new Job(
name: Jobs::PATCH_ELEMENTS->value,
steps: [
new JobStep(
JobSteps::ELEMENT_FOLDER_PATCHING->value,
PatchFolderMessage::class,
'',
['filters' => $patchFolderParameter->getFilters()]
),
],
selectedElements: array_map(
static fn (array $data) => new ElementDescriptor(
$elementType,
$data['folderId']
),
$patchFolderParameter->getData()
),
environmentData: array_column($patchFolderParameter->getData(), null, 'folderId'),
);
$jobRun = $this->jobExecutionAgent->startJobExecution(
$job,
$user->getId(),
Config::CONTEXT_CONTINUE_ON_ERROR->value
);
return $jobRun->getId();
}
/**
* @throws ElementSavingFailedException
*/
public function patchElement(
ElementInterface $element,
string $elementType,
array $elementPatchData,
UserInterface $user,
): void {
try {
if (isset($elementPatchData[UpdateServiceInterface::EDITABLE_DATA_KEY]) && $element instanceof Concrete) {
$this->patchEditableData(
$element,
$elementPatchData[UpdateServiceInterface::EDITABLE_DATA_KEY],
$user
);
unset($elementPatchData[UpdateServiceInterface::EDITABLE_DATA_KEY]);
}
$adapters = $this->adapterLoader->loadAdapters($elementType);
foreach ($adapters as $adapter) {
$adapter->patch($element, $elementPatchData, $user);
}
$this->elementSaveService->save(
$element,
$user,
$elementPatchData[ElementSaveServiceInterface::INDEX_TASK] ?? null
);
} catch (DuplicateFullPathException) {
throw new ElementExistsException(
message: sprintf('Element with full path [%s] already exists', $element->getRealFullPath())
);
} catch (Exception $exception) {
throw new ElementSavingFailedException($element->getId(), $exception->getMessage());
}
}
public function handlePatchDataField(array $fieldData, array $existingValues, ?string $dataKey = null): array
{
$newData = $fieldData[PatchDataKeys::DATA->value];
$action = $fieldData[PatchDataKeys::ACTION->value];
$existingMap = [];
foreach ($existingValues as $existingItem) {
$existingMap[$this->getFieldMapKey($existingItem, $dataKey)] = $existingItem;
}
return match ($action) {
PatcherActions::ADD->value => $this->handleAddition($existingMap, $newData, $dataKey),
PatcherActions::REMOVE->value => $this->handleRemoval($existingMap, $newData, $dataKey),
default => $fieldData
};
}
/**
* @throws Exception
*/
private function patchEditableData(Concrete $element, array $data, UserInterface $user): void
{
$class = $element->getClass();
foreach ($data as $key => $value) {
$fieldDefinition = $class->getFieldDefinition($key);
if ($fieldDefinition === null || !array_key_exists($key, $data)) {
continue;
}
$adapter = $this->dataAdapterService->tryDataAdapter($fieldDefinition->getFieldtype());
if ($adapter === null) {
continue;
}
$value = $adapter->getDataForSetter($element, $fieldDefinition, $key, $data, $user, isPatch: true);
if (!$this->validateEncryptedField($fieldDefinition, $value)) {
continue;
}
$element->setValue($key, $value);
}
}
private function handleAddition(array $existingMap, array $newData, ?string $dataKey = null): array
{
foreach ($newData as $newEntry) {
$existingMap[$this->getFieldMapKey($newEntry, $dataKey)] = $newEntry;
}
return array_values($existingMap);
}
private function handleRemoval(array $existingMap, array $newData, ?string $dataKey = null): array
{
foreach ($newData as $newEntry) {
$entryKey = $this->getFieldMapKey($newEntry, $dataKey);
if (isset($existingMap[$entryKey])) {
unset($existingMap[$entryKey]);
}
}
return array_values($existingMap);
}
private function getFieldMapKey(array $item, ?string $dataKey = null): string
{
$elementData = $dataKey ? $item[$dataKey] : $item;
return $elementData[FieldKeys::ID_KEY->value] . '_' . $elementData[FieldKeys::TYPE_KEY->value];
}
private function patchAsynchronously(
string $elementType,
array $patchData,
UserInterface $user,
): int {
$job = new Job(
name: Jobs::PATCH_ELEMENTS->value,
steps: [
new JobStep(JobSteps::ELEMENT_PATCHING->value, PatchMessage::class, '', []),
],
selectedElements: array_map(
static fn (array $data) => new ElementDescriptor(
$elementType,
$data['id']
),
$patchData
),
environmentData: array_column($patchData, null, 'id'),
);
$jobRun = $this->jobExecutionAgent->startJobExecution(
$job,
$user->getId(),
Config::CONTEXT_CONTINUE_ON_ERROR->value
);
return $jobRun->getId();
}
}