-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathReplaceNameStepExecutor.php
More file actions
63 lines (49 loc) · 1.96 KB
/
ReplaceNameStepExecutor.php
File metadata and controls
63 lines (49 loc) · 1.96 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
<?php
declare(strict_types=1);
namespace App\Migrations\Step;
use Ibexa\Contracts\Core\Repository\ContentService;
use Ibexa\Contracts\Core\Repository\Values\Filter\Filter;
use Ibexa\Contracts\Migration\StepExecutor\AbstractStepExecutor;
use Ibexa\Core\FieldType\TextLine\Value;
use Ibexa\Migration\ValueObject\Step\StepInterface;
final class ReplaceNameStepExecutor extends AbstractStepExecutor
{
private ContentService $contentService;
public function __construct(
ContentService $contentService
) {
$this->contentService = $contentService;
}
protected function doHandle(StepInterface $step)
{
assert($step instanceof ReplaceNameStep);
$contentItems = $this->contentService->find(new Filter());
foreach ($contentItems as $contentItem) {
$struct = $this->contentService->newContentUpdateStruct();
foreach ($contentItem->getFields() as $field) {
if ($field->fieldTypeIdentifier !== 'ezstring') {
continue;
}
if ($field->fieldDefIdentifier === 'identifier') {
continue;
}
if (strpos($field->value, 'Company Name') !== false) {
$newValue = str_replace('Company Name', $step->getReplacement(), $field->value);
$struct->setField($field->fieldDefIdentifier, new Value($newValue));
}
}
try {
$content = $this->contentService->createContentDraft($contentItem->contentInfo);
$content = $this->contentService->updateContent($content->getVersionInfo(), $struct);
$this->contentService->publishVersion($content->getVersionInfo());
} catch (\Throwable $e) {
// Ignore
}
}
return null;
}
public function canHandle(StepInterface $step): bool
{
return $step instanceof ReplaceNameStep;
}
}