-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathElementDeleteHandler.php
More file actions
139 lines (121 loc) · 4.65 KB
/
ElementDeleteHandler.php
File metadata and controls
139 lines (121 loc) · 4.65 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
<?php
declare(strict_types=1);
/**
* This source file is available under the terms of the
* Pimcore Open Core License (POCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (https://www.pimcore.com)
* @license Pimcore Open Core License (POCL)
*/
namespace Pimcore\Bundle\StudioBackendBundle\Element\ExecutionEngine\AutomationAction\Messenger\Handler;
use Exception;
use Pimcore\Bundle\StaticResolverBundle\Models\User\UserResolverInterface;
use Pimcore\Bundle\StudioBackendBundle\Element\ExecutionEngine\AutomationAction\Messenger\Messages\ElementDeleteMessage;
use Pimcore\Bundle\StudioBackendBundle\Element\Service\ElementDeleteServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Element\Service\ElementServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\ExecutionEngine\AutomationAction\AbstractHandler;
use Pimcore\Bundle\StudioBackendBundle\ExecutionEngine\Model\AbortActionData;
use Pimcore\Bundle\StudioBackendBundle\ExecutionEngine\Util\Config;
use Pimcore\Bundle\StudioBackendBundle\ExecutionEngine\Util\StepConfig;
use Pimcore\Bundle\StudioBackendBundle\ExecutionEngine\Util\Trait\HandlerProgressTrait;
use Pimcore\Bundle\StudioBackendBundle\Mercure\Service\PublishServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Mercure\Service\UserTopicServiceInterface;
use Pimcore\Model\Element\ElementInterface;
use Pimcore\Model\Element\ElementDescriptor;
use Pimcore\Model\UserInterface;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
use function count;
/**
* @internal
*/
#[AsMessageHandler]
final class ElementDeleteHandler extends AbstractHandler
{
use HandlerProgressTrait;
public function __construct(
private readonly ElementDeleteServiceInterface $elementDeleteService,
private readonly ElementServiceInterface $elementService,
private readonly PublishServiceInterface $publishService,
private readonly UserResolverInterface $userResolver,
private readonly UserTopicServiceInterface $userTopicService,
) {
parent::__construct();
}
/**
* @throws Exception
*/
public function __invoke(ElementDeleteMessage $message): void
{
if (!$this->shouldBeExecuted($this->getJobRun($message))) {
return;
}
$jobRun = $this->getJobRun($message);
$validatedParameters = $this->validateFullParameters(
$message,
$jobRun,
$this->userResolver
);
if ($validatedParameters instanceof AbortActionData) {
$this->abort($validatedParameters);
}
$user = $validatedParameters->getUser();
$parentElement = $validatedParameters->getSubject();
$items = $this->extractConfigFieldFromJobStepConfig(
$message,
StepConfig::ITEMS_TO_DELETE->value
);
$totalItems = count($items);
$stepName = $this->getJobStep($message)->getName();
foreach ($items as $elementId) {
$element = $this->getElementById(
new ElementDescriptor($parentElement->getType(), $elementId),
$user,
$this->elementService
);
$this->deleteElement($element, $parentElement->getId(), $user);
$this->updateProgress(
$this->publishService,
$this->userTopicService,
$jobRun,
$stepName,
$totalItems,
100,
);
}
}
protected function configureStep(): void
{
$this->stepConfiguration->setRequired(StepConfig::ITEMS_TO_DELETE->value);
$this->stepConfiguration->setAllowedTypes(
StepConfig::ITEMS_TO_DELETE->value,
StepConfig::CONFIG_TYPE_ARRAY->value
);
}
/**
* @throws Exception
*/
private function deleteElement(
ElementInterface $element,
int $parentId,
UserInterface $user
): void {
$isParent = $element->getId() === $parentId;
try {
match ($isParent) {
true => $this->elementDeleteService->deleteParentElement($element, $user),
false => $this->elementDeleteService->deleteElement($element, $user, true),
};
} catch (Exception $exception) {
$this->abort($this->getAbortData(
Config::ELEMENT_DELETE_FAILED_MESSAGE->value,
[
'type' => $element->getType(),
'id' => $element->getId(),
'message' => $exception->getMessage(),
],
));
}
}
}