-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathElementReferenceService.php
More file actions
167 lines (152 loc) · 5.53 KB
/
ElementReferenceService.php
File metadata and controls
167 lines (152 loc) · 5.53 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
<?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\Service\ExecutionEngine;
use Exception;
use Pimcore\Bundle\GenericExecutionEngineBundle\Agent\JobExecutionAgentInterface;
use Pimcore\Bundle\GenericExecutionEngineBundle\Model\Job;
use Pimcore\Bundle\GenericExecutionEngineBundle\Model\JobStep;
use Pimcore\Bundle\StaticResolverBundle\Models\Asset\AssetServiceResolverInterface;
use Pimcore\Bundle\StaticResolverBundle\Models\DataObject\DataObjectServiceResolverInterface;
use Pimcore\Bundle\StaticResolverBundle\Models\Document\DocumentServiceResolverInterface;
use Pimcore\Bundle\StudioBackendBundle\Element\ExecutionEngine\AutomationAction\Messenger\Messages\RewriteRefMessage;
use Pimcore\Bundle\StudioBackendBundle\Element\ExecutionEngine\Util\JobSteps;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\InvalidElementTypeException;
use Pimcore\Bundle\StudioBackendBundle\ExecutionEngine\Util\Config;
use Pimcore\Bundle\StudioBackendBundle\ExecutionEngine\Util\EnvironmentVariables;
use Pimcore\Bundle\StudioBackendBundle\ExecutionEngine\Util\Jobs;
use Pimcore\Bundle\StudioBackendBundle\ExecutionEngine\Util\StepConfig;
use Pimcore\Bundle\StudioBackendBundle\ExecutionEngine\Util\Trait\ChunkGeneratorTrait;
use Pimcore\Model\Asset;
use Pimcore\Model\DataObject\AbstractObject;
use Pimcore\Model\Document;
use Pimcore\Model\Element\ElementInterface;
use Pimcore\Model\UserInterface;
/**
* @internal
*/
final readonly class ElementReferenceService implements ElementReferenceServiceInterface
{
use ChunkGeneratorTrait;
private const int REWRITE_REFERENCES_BATCH_SIZE = 500;
public function __construct(
private AssetServiceResolverInterface $assetServiceResolver,
private DataObjectServiceResolverInterface $dataObjectServiceResolver,
private DocumentServiceResolverInterface $documentServiceResolver,
private JobExecutionAgentInterface $jobExecutionAgent,
) {
}
/**
* @throws Exception
*/
public function rewriteElementReferences(
UserInterface $user,
ElementInterface $element,
array $rewriteConfiguration,
array $parameters = []
): void {
match (true) {
$element instanceof AbstractObject => $this->rewriteDataObjectReferences(
$user,
$element,
$rewriteConfiguration,
$parameters
),
$element instanceof Asset => $this->rewriteAssetReferences(
$element,
$rewriteConfiguration
),
$element instanceof Document => $this->rewriteDocumentReferences(
$user,
$element,
$rewriteConfiguration,
$parameters
),
default => throw new InvalidElementTypeException($element->getType()),
};
}
public function rewriteReferencesWithExecutionEngine(
UserInterface $user,
array $rewriteConfiguration,
array $ids,
string $type
): int {
$jobSteps = [];
foreach ($this->chunkGenerator($ids, self::REWRITE_REFERENCES_BATCH_SIZE) as $batch) {
$jobSteps[] = new JobStep(
JobSteps::ELEMENT_REWRITE_REFERENCE->value,
RewriteRefMessage::class,
'',
[
StepConfig::ELEMENTS_TO_REWRITE_REFERENCES->value => $batch,
StepConfig::ELEMENT_TYPE_TO_REWRITE_REFERENCES->value => $type,
],
);
}
$job = new Job(
name: Jobs::REWRITE_REFERENCES->value,
steps: $jobSteps,
environmentData: [
EnvironmentVariables::REWRITE_CONFIGURATION->value => [$type => $rewriteConfiguration],
EnvironmentVariables::REWRITE_PARAMETERS->value => [],
]
);
$jobRun = $this->jobExecutionAgent->startJobExecution(
$job,
$user->getId(),
Config::CONTEXT_CONTINUE_ON_ERROR->value
);
return $jobRun->getId();
}
private function rewriteAssetReferences(
Asset $element,
array $rewriteConfiguration
): void {
$this->assetServiceResolver->rewriteIds(
$element,
$rewriteConfiguration
);
}
/**
* @throws Exception
*/
private function rewriteDocumentReferences(
UserInterface $user,
Document $element,
array $rewriteConfiguration,
array $parameters = [],
): void {
$object = $this->documentServiceResolver->rewriteIds(
$element,
$rewriteConfiguration,
$parameters
);
$object->setUserModification($user->getId());
$object->save();
}
/**
* @throws Exception
*/
private function rewriteDataObjectReferences(
UserInterface $user,
AbstractObject $element,
array $rewriteConfiguration,
array $parameters = [],
): void {
$object = $this->dataObjectServiceResolver->rewriteIds(
$element,
$rewriteConfiguration,
$parameters
);
$object->setUserModification($user->getId());
$object->save();
}
}