From a8d9919687180cbca85a013c38cabd9ad695846d Mon Sep 17 00:00:00 2001 From: mattamon Date: Tue, 7 May 2024 16:02:35 +0200 Subject: [PATCH] Move saving to repository --- .../ElementSavingFailedException.php | 32 ++++++++++++++++++ src/Property/Repository.php | 33 +++++++++++++++++++ src/Property/RepositoryInterface.php | 13 ++++++++ src/Property/Service/PropertyService.php | 18 ++-------- 4 files changed, 80 insertions(+), 16 deletions(-) create mode 100644 src/Exception/ElementSavingFailedException.php diff --git a/src/Exception/ElementSavingFailedException.php b/src/Exception/ElementSavingFailedException.php new file mode 100644 index 000000000..ff327e98a --- /dev/null +++ b/src/Exception/ElementSavingFailedException.php @@ -0,0 +1,32 @@ +delete(); } + + /** + * @throws ElementSavingFailedException + */ + public function updateElementProperties(string $elementType, int $id, UpdateElementProperties $items): void + { + $element = $this->getElement($this->serviceResolver, $elementType, $id); + $properties = []; + foreach($items->getProperties() as $updateProperty) { + $property = new Property(); + $property->setType($updateProperty->getType()); + $property->setName($updateProperty->getKey()); + $property->setData($updateProperty->getData()); + $property->setInheritable($updateProperty->getInheritable()); + $properties[$updateProperty->getKey()] = $property; + } + $element->setProperties($properties); + try { + $element->save(); + } catch (DuplicateFullPathException $e) { + throw new ElementSavingFailedException($id, $e->getMessage()); + } + } } diff --git a/src/Property/RepositoryInterface.php b/src/Property/RepositoryInterface.php index 10dac9bac..945b58e6b 100644 --- a/src/Property/RepositoryInterface.php +++ b/src/Property/RepositoryInterface.php @@ -16,8 +16,12 @@ namespace Pimcore\Bundle\StudioBackendBundle\Property; +use Pimcore\Bundle\StudioBackendBundle\Exception\ElementSavingFailedException; use Pimcore\Bundle\StudioBackendBundle\Property\Request\PropertiesParameters; +use Pimcore\Bundle\StudioBackendBundle\Property\Request\UpdateElementProperties; use Pimcore\Bundle\StudioBackendBundle\Property\Schema\UpdatePredefinedProperty; +use Pimcore\Model\Element\DuplicateFullPathException; +use Pimcore\Model\Element\ElementInterface; use Pimcore\Model\Property\Predefined; use Pimcore\Model\Property\Predefined\Listing as PropertiesListing; @@ -30,5 +34,14 @@ public function listProperties(PropertiesParameters $parameters): PropertiesList public function updatePredefinedProperty(string $id, UpdatePredefinedProperty $property): Predefined; + /** + * @throws ElementSavingFailedException + */ + public function updateElementProperties( + string $elementType, + int $id, + UpdateElementProperties $items + ): void; + public function deletePredefinedProperty(string $id): void; } diff --git a/src/Property/Service/PropertyService.php b/src/Property/Service/PropertyService.php index f21bb8b02..c4aa7dab4 100644 --- a/src/Property/Service/PropertyService.php +++ b/src/Property/Service/PropertyService.php @@ -16,13 +16,11 @@ namespace Pimcore\Bundle\StudioBackendBundle\Property\Service; -use Pimcore\Bundle\StaticResolverBundle\Models\Element\ServiceResolver; use Pimcore\Bundle\StudioBackendBundle\Exception\PropertyNotFoundException; use Pimcore\Bundle\StudioBackendBundle\Property\RepositoryInterface; use Pimcore\Bundle\StudioBackendBundle\Property\Request\UpdateElementProperties; use Pimcore\Bundle\StudioBackendBundle\Property\Schema\UpdatePredefinedProperty; use Pimcore\Bundle\StudioBackendBundle\Util\Traits\ElementProviderTrait; -use Pimcore\Model\Property; use Pimcore\Model\Property\Predefined; /** @@ -33,8 +31,7 @@ use ElementProviderTrait; public function __construct( - private RepositoryInterface $repository, - private ServiceResolver $serviceResolver, + private RepositoryInterface $repository ) { } @@ -48,18 +45,7 @@ public function updatePredefinedProperty(string $id, UpdatePredefinedProperty $p public function updateElementProperties(string $elementType, int $id, UpdateElementProperties $items): void { - $element = $this->getElement($this->serviceResolver, $elementType, $id); - $properties = []; - foreach($items->getProperties() as $updateProperty) { - $property = new Property(); - $property->setType($updateProperty->getType()); - $property->setName($updateProperty->getKey()); - $property->setData($updateProperty->getData()); - $property->setInheritable($updateProperty->getInheritable()); - $properties[$updateProperty->getKey()] = $property; - } - $element->setProperties($properties); - $element->save(); + $this->repository->updateElementProperties($elementType, $id, $items); } /**