Skip to content

Commit

Permalink
Move saving to repository
Browse files Browse the repository at this point in the history
  • Loading branch information
mattamon committed May 7, 2024
1 parent 7721164 commit a8d9919
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 16 deletions.
32 changes: 32 additions & 0 deletions src/Exception/ElementSavingFailedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?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\Exception;

/**
* @internal
*/
final class ElementSavingFailedException extends AbstractApiException
{
public function __construct(int $id, ?string $error = null)
{
parent::__construct(500, sprintf(
'Failed to save element with ID %s: %s',
$id,
$error ?? 'Unknown error'
));
}
}
33 changes: 33 additions & 0 deletions src/Property/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,16 @@

namespace Pimcore\Bundle\StudioBackendBundle\Property;

use Pimcore\Bundle\StaticResolverBundle\Models\Element\ServiceResolver;
use Pimcore\Bundle\StudioBackendBundle\Exception\ElementSavingFailedException;
use Pimcore\Bundle\StudioBackendBundle\Exception\PropertyNotFoundException;
use Pimcore\Bundle\StudioBackendBundle\Property\Request\PropertiesParameters;
use Pimcore\Bundle\StudioBackendBundle\Property\Request\UpdateElementProperties;
use Pimcore\Bundle\StudioBackendBundle\Property\Schema\UpdatePredefinedProperty;
use Pimcore\Bundle\StudioBackendBundle\Util\Traits\ElementProviderTrait;
use Pimcore\Model\Element\DuplicateFullPathException;
use Pimcore\Model\Element\ElementInterface;

Check notice on line 27 in src/Property/Repository.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Unused import

Import 'Pimcore\\Model\\Element\\ElementInterface' is never used

Check notice on line 27 in src/Property/Repository.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Unused import

Import 'Pimcore\\Model\\Element\\ElementInterface' is never used
use Pimcore\Model\Property;
use Pimcore\Model\Property\Predefined;
use Pimcore\Model\Property\Predefined\Listing as PropertiesListing;
use Symfony\Contracts\Translation\TranslatorInterface;
Expand All @@ -28,7 +35,10 @@
*/
final readonly class Repository implements RepositoryInterface
{
use ElementProviderTrait;

public function __construct(
private ServiceResolver $serviceResolver,
private TranslatorInterface $translator
) {
}
Expand Down Expand Up @@ -93,4 +103,27 @@ public function deletePredefinedProperty(string $id): void

$predefined->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());
}
}
}
13 changes: 13 additions & 0 deletions src/Property/RepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Check notice on line 23 in src/Property/RepositoryInterface.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Unused import

Import 'Pimcore\\Model\\Element\\DuplicateFullPathException' is never used

Check notice on line 23 in src/Property/RepositoryInterface.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Unused import

Import 'Pimcore\\Model\\Element\\DuplicateFullPathException' is never used
use Pimcore\Model\Element\ElementInterface;

Check notice on line 24 in src/Property/RepositoryInterface.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Unused import

Import 'Pimcore\\Model\\Element\\ElementInterface' is never used

Check notice on line 24 in src/Property/RepositoryInterface.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Unused import

Import 'Pimcore\\Model\\Element\\ElementInterface' is never used
use Pimcore\Model\Property\Predefined;
use Pimcore\Model\Property\Predefined\Listing as PropertiesListing;

Expand All @@ -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;
}
18 changes: 2 additions & 16 deletions src/Property/Service/PropertyService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -33,8 +31,7 @@
use ElementProviderTrait;

public function __construct(
private RepositoryInterface $repository,
private ServiceResolver $serviceResolver,
private RepositoryInterface $repository
) {
}

Expand All @@ -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);
}

/**
Expand Down

0 comments on commit a8d9919

Please sign in to comment.