Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 2 additions & 16 deletions src/DataObject/Service/DataObjectService.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
use Pimcore\Bundle\StudioBackendBundle\Util\Constant\ElementPermissions;
use Pimcore\Bundle\StudioBackendBundle\Util\Constant\ElementTypes;
use Pimcore\Bundle\StudioBackendBundle\Util\Constant\HttpResponseCodes;
use Pimcore\Bundle\StudioBackendBundle\Util\Constant\HttpResponseErrorKeys;
use Pimcore\Bundle\StudioBackendBundle\Util\Trait\ElementProviderTrait;
use Pimcore\Bundle\StudioBackendBundle\Util\Trait\UserPermissionTrait;
use Pimcore\Model\DataObject\AbstractObject;
Expand Down Expand Up @@ -91,13 +90,6 @@ public function __construct(
) {
}

public function getDataObjectFullPath(
string $parentFullPath,
string $key
): string {
return str_ends_with($parentFullPath, '/') === true ? $parentFullPath . $key : $parentFullPath . '/' . $key;
}

/**
* @throws DatabaseException
* @throws ElementSavingFailedException
Expand All @@ -112,15 +104,9 @@ public function addDataObject(
): int {
$user = $this->securityService->getCurrentUser();
$parent = $this->getValidParent($user, $parentId);
$fullPath = $this->getDataObjectFullPath(
$parent->getFullPath(),
$parameters->getKey()
);
$fullPath = $this->getElementFullPath($parent->getFullPath(), $parameters->getKey());
if ($this->dataObjectServiceResolver->pathExists($fullPath)) {
throw new ElementExistsException(
$fullPath,
HttpResponseErrorKeys::ELEMENT_EXISTS->value
);
throw new ElementExistsException(error: $fullPath);
}

$class = $this->getValidClass($this->classDefinitionResolver, $parameters->getClassId());
Expand Down
5 changes: 0 additions & 5 deletions src/DataObject/Service/DataObjectServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,4 @@ public function getDataObjectElementByPath(
* @throws ForbiddenException|InvalidQueryTypeException|NotFoundException|UserNotFoundException
*/
public function setTreeSorting(DataObjectModel $parent, QueryInterface $dataObjectQuery): void;

public function getDataObjectFullPath(
string $parentFullPath,
string $key
): string;
}
4 changes: 2 additions & 2 deletions src/Element/Service/ElementFolderService.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ public function createFolderByType(

if ($existingElement) {
throw new ElementExistsException(
'Folder already exists',
HttpResponseErrorKeys::FOLDER_EXISTS->value
error: 'Folder already exists',
errorKey: HttpResponseErrorKeys::FOLDER_EXISTS->value
);
}

Expand Down
17 changes: 11 additions & 6 deletions src/Exception/Api/ElementExistsException.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,17 @@
*/
final class ElementExistsException extends AbstractApiException
{
public function __construct(?string $error = null, string $errorKey = HttpResponseErrorKeys::ELEMENT_EXISTS->value)
{
$message = sprintf(
'Failed to create new element: %s',
$error ?? 'Unknown error'
);
public function __construct(
?string $message = null,
?string $error = null,
string $errorKey = HttpResponseErrorKeys::ELEMENT_EXISTS->value,
) {
if ($message === null) {
$message = sprintf(
'Failed to create new element: %s',
$error ?? 'Unknown error'
);
}

parent::__construct(
HttpResponseCodes::CONFLICT->value,
Expand Down
2 changes: 1 addition & 1 deletion src/Patcher/Adapter/KeyAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#[AutoconfigureTag(TaggedIteratorAdapter::ADAPTER_TAG)]
final readonly class KeyAdapter implements PatchAdapterInterface
{
private const INDEX_KEY = 'key';
private const string INDEX_KEY = 'key';

public function patch(ElementInterface $element, array $data, UserInterface $user): void
{
Expand Down
7 changes: 7 additions & 0 deletions src/Patcher/Service/PatchService.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use Pimcore\Bundle\StudioBackendBundle\Element\ExecutionEngine\Util\JobSteps;
use Pimcore\Bundle\StudioBackendBundle\Element\Service\ElementSaveServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Element\Service\ElementServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\ElementExistsException;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\ElementSavingFailedException;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\ForbiddenException;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\InvalidArgumentException;
Expand All @@ -39,11 +40,13 @@
use Pimcore\Bundle\StudioBackendBundle\Util\Constant\PatchDataKeys;
use Pimcore\Bundle\StudioBackendBundle\Util\Constant\PatcherActions;
use Pimcore\Model\DataObject\Concrete;
use Pimcore\Model\Element\DuplicateFullPathException;
use Pimcore\Model\Element\ElementDescriptor;
use Pimcore\Model\Element\ElementInterface;
use Pimcore\Model\UserInterface;
use function array_key_exists;
use function count;
use function sprintf;

/**
* @internal
Expand Down Expand Up @@ -143,6 +146,10 @@ public function patchElement(
$user,
$elementPatchData[ElementSaveServiceInterface::INDEX_TASK] ?? null
);
} catch (DuplicateFullPathException) {
throw new ElementExistsException(
message: sprintf('Element with full path [%s] already exists', $element->getRealFullPath())
);
} catch (Exception $exception) {
throw new ElementSavingFailedException($element->getId(), $exception->getMessage());
}
Expand Down
7 changes: 7 additions & 0 deletions src/Updater/Service/UpdateService.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,18 @@
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
Expand Down Expand Up @@ -77,6 +80,10 @@ public function update(string $elementType, int $id, array $data): void

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) {
Expand Down
7 changes: 7 additions & 0 deletions src/Util/Trait/ElementProviderTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,11 @@ private function getCoreElementType(string $type): string

return $type;
}

private function getElementFullPath(
string $parentFullPath,
string $key
): string {
return str_ends_with($parentFullPath, '/') === true ? $parentFullPath . $key : $parentFullPath . '/' . $key;
}
}