-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathUpdateService.php
More file actions
104 lines (91 loc) · 4.18 KB
/
UpdateService.php
File metadata and controls
104 lines (91 loc) · 4.18 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
<?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\Updater\Service;
use Exception;
use Pimcore\Bundle\StaticResolverBundle\Models\Element\ServiceResolverInterface;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Service\DataServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Util\Trait\ValidateObjectDataTrait;
use Pimcore\Bundle\StudioBackendBundle\Element\Service\ElementSaveServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\ElementExistsException;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\ElementSavingFailedException;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\FieldValidationFailedException;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\NotFoundException;
use Pimcore\Bundle\StudioBackendBundle\Security\Service\SecurityServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Util\Trait\ElementProviderTrait;
use Pimcore\Model\DataObject\Concrete;
use Pimcore\Model\Document;
use Pimcore\Model\Element\DuplicateFullPathException;
use Pimcore\Model\Element\ElementInterface;
use Pimcore\Model\Element\ValidationException;
use function sprintf;
/**
* @internal
*/
final readonly class UpdateService implements UpdateServiceInterface
{
use ElementProviderTrait;
use ValidateObjectDataTrait;
public function __construct(
private AdapterLoaderInterface $adapterLoader,
private DataServiceInterface $objectDataService,
private SecurityServiceInterface $securityService,
private ServiceResolverInterface $serviceResolver,
private ElementSaveServiceInterface $elementSaveService,
) {
}
/**
* @throws ElementSavingFailedException|FieldValidationFailedException|NotFoundException
*/
public function update(string $elementType, int $id, array $data): void
{
$user = $this->securityService->getCurrentUser();
$element = $this->getElement($this->serviceResolver, $elementType, $id);
$task = $data[ElementSaveServiceInterface::INDEX_TASK] ?? null;
if (isset($data[self::USE_DRAFT_DATA_KEY]) && $data[self::USE_DRAFT_DATA_KEY] === true) {
$draftElement = $this->getDraftElement($element);
$considerDraftData = $element !== $draftElement;
if ($considerDraftData && $draftElement instanceof Concrete && $element instanceof Concrete) {
$this->objectDataService->handleDraftData($draftElement, $element, $task);
$element = $draftElement;
}
}
if (isset($data[self::EDITABLE_DATA_KEY]) && $element instanceof Concrete) {
$this->objectDataService->updateEditableData($element, $data[self::EDITABLE_DATA_KEY], $user);
unset($data[self::EDITABLE_DATA_KEY]);
}
foreach ($this->adapterLoader->loadAdapters($elementType) as $adapter) {
$adapter->update($element, $data);
}
try {
$this->elementSaveService->save($element, $user, $task);
} catch (DuplicateFullPathException) {
throw new ElementExistsException(
message: sprintf('Element with full path [%s] already exists', $element->getRealFullPath())
);
} catch (ValidationException $e) {
throw new FieldValidationFailedException($e->getMessage(), previous: $e);
} catch (Exception $e) {
throw new ElementSavingFailedException($id, $e->getMessage());
}
}
private function getDraftElement(ElementInterface $element): ElementInterface
{
if (!$element instanceof Concrete && !$element instanceof Document) {
return $element;
}
$version = $this->getLatestVersionForUser($element, $this->securityService->getCurrentUser());
return $this->getVersionData($element, $version);
}
}