Skip to content

Commit 0b77f48

Browse files
authored
[Elements] Catch duplicate full path exception (#938)
* add catch for full path exception * adapt exception message * Apply php-cs-fixer changes
1 parent 2e0b9bf commit 0b77f48

8 files changed

Lines changed: 37 additions & 30 deletions

File tree

src/DataObject/Service/DataObjectService.php

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
use Pimcore\Bundle\StudioBackendBundle\Util\Constant\ElementPermissions;
4949
use Pimcore\Bundle\StudioBackendBundle\Util\Constant\ElementTypes;
5050
use Pimcore\Bundle\StudioBackendBundle\Util\Constant\HttpResponseCodes;
51-
use Pimcore\Bundle\StudioBackendBundle\Util\Constant\HttpResponseErrorKeys;
5251
use Pimcore\Bundle\StudioBackendBundle\Util\Trait\ElementProviderTrait;
5352
use Pimcore\Bundle\StudioBackendBundle\Util\Trait\UserPermissionTrait;
5453
use Pimcore\Model\DataObject\AbstractObject;
@@ -91,13 +90,6 @@ public function __construct(
9190
) {
9291
}
9392

94-
public function getDataObjectFullPath(
95-
string $parentFullPath,
96-
string $key
97-
): string {
98-
return str_ends_with($parentFullPath, '/') === true ? $parentFullPath . $key : $parentFullPath . '/' . $key;
99-
}
100-
10193
/**
10294
* @throws DatabaseException
10395
* @throws ElementSavingFailedException
@@ -112,15 +104,9 @@ public function addDataObject(
112104
): int {
113105
$user = $this->securityService->getCurrentUser();
114106
$parent = $this->getValidParent($user, $parentId);
115-
$fullPath = $this->getDataObjectFullPath(
116-
$parent->getFullPath(),
117-
$parameters->getKey()
118-
);
107+
$fullPath = $this->getElementFullPath($parent->getFullPath(), $parameters->getKey());
119108
if ($this->dataObjectServiceResolver->pathExists($fullPath)) {
120-
throw new ElementExistsException(
121-
$fullPath,
122-
HttpResponseErrorKeys::ELEMENT_EXISTS->value
123-
);
109+
throw new ElementExistsException(error: $fullPath);
124110
}
125111

126112
$class = $this->getValidClass($this->classDefinitionResolver, $parameters->getClassId());

src/DataObject/Service/DataObjectServiceInterface.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,4 @@ public function getDataObjectElementByPath(
9999
* @throws ForbiddenException|InvalidQueryTypeException|NotFoundException|UserNotFoundException
100100
*/
101101
public function setTreeSorting(DataObjectModel $parent, QueryInterface $dataObjectQuery): void;
102-
103-
public function getDataObjectFullPath(
104-
string $parentFullPath,
105-
string $key
106-
): string;
107102
}

src/Element/Service/ElementFolderService.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ public function createFolderByType(
6868

6969
if ($existingElement) {
7070
throw new ElementExistsException(
71-
'Folder already exists',
72-
HttpResponseErrorKeys::FOLDER_EXISTS->value
71+
error: 'Folder already exists',
72+
errorKey: HttpResponseErrorKeys::FOLDER_EXISTS->value
7373
);
7474
}
7575

src/Exception/Api/ElementExistsException.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,17 @@
2525
*/
2626
final class ElementExistsException extends AbstractApiException
2727
{
28-
public function __construct(?string $error = null, string $errorKey = HttpResponseErrorKeys::ELEMENT_EXISTS->value)
29-
{
30-
$message = sprintf(
31-
'Failed to create new element: %s',
32-
$error ?? 'Unknown error'
33-
);
28+
public function __construct(
29+
?string $message = null,
30+
?string $error = null,
31+
string $errorKey = HttpResponseErrorKeys::ELEMENT_EXISTS->value,
32+
) {
33+
if ($message === null) {
34+
$message = sprintf(
35+
'Failed to create new element: %s',
36+
$error ?? 'Unknown error'
37+
);
38+
}
3439

3540
parent::__construct(
3641
HttpResponseCodes::CONFLICT->value,

src/Patcher/Adapter/KeyAdapter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
#[AutoconfigureTag(TaggedIteratorAdapter::ADAPTER_TAG)]
3030
final readonly class KeyAdapter implements PatchAdapterInterface
3131
{
32-
private const INDEX_KEY = 'key';
32+
private const string INDEX_KEY = 'key';
3333

3434
public function patch(ElementInterface $element, array $data, UserInterface $user): void
3535
{

src/Patcher/Service/PatchService.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use Pimcore\Bundle\StudioBackendBundle\Element\ExecutionEngine\Util\JobSteps;
2828
use Pimcore\Bundle\StudioBackendBundle\Element\Service\ElementSaveServiceInterface;
2929
use Pimcore\Bundle\StudioBackendBundle\Element\Service\ElementServiceInterface;
30+
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\ElementExistsException;
3031
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\ElementSavingFailedException;
3132
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\ForbiddenException;
3233
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\InvalidArgumentException;
@@ -39,11 +40,13 @@
3940
use Pimcore\Bundle\StudioBackendBundle\Util\Constant\PatchDataKeys;
4041
use Pimcore\Bundle\StudioBackendBundle\Util\Constant\PatcherActions;
4142
use Pimcore\Model\DataObject\Concrete;
43+
use Pimcore\Model\Element\DuplicateFullPathException;
4244
use Pimcore\Model\Element\ElementDescriptor;
4345
use Pimcore\Model\Element\ElementInterface;
4446
use Pimcore\Model\UserInterface;
4547
use function array_key_exists;
4648
use function count;
49+
use function sprintf;
4750

4851
/**
4952
* @internal
@@ -143,6 +146,10 @@ public function patchElement(
143146
$user,
144147
$elementPatchData[ElementSaveServiceInterface::INDEX_TASK] ?? null
145148
);
149+
} catch (DuplicateFullPathException) {
150+
throw new ElementExistsException(
151+
message: sprintf('Element with full path [%s] already exists', $element->getRealFullPath())
152+
);
146153
} catch (Exception $exception) {
147154
throw new ElementSavingFailedException($element->getId(), $exception->getMessage());
148155
}

src/Updater/Service/UpdateService.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,18 @@
2121
use Pimcore\Bundle\StudioBackendBundle\DataObject\Service\DataServiceInterface;
2222
use Pimcore\Bundle\StudioBackendBundle\DataObject\Util\Trait\ValidateObjectDataTrait;
2323
use Pimcore\Bundle\StudioBackendBundle\Element\Service\ElementSaveServiceInterface;
24+
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\ElementExistsException;
2425
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\ElementSavingFailedException;
2526
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\FieldValidationFailedException;
2627
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\NotFoundException;
2728
use Pimcore\Bundle\StudioBackendBundle\Security\Service\SecurityServiceInterface;
2829
use Pimcore\Bundle\StudioBackendBundle\Util\Trait\ElementProviderTrait;
2930
use Pimcore\Model\DataObject\Concrete;
3031
use Pimcore\Model\Document;
32+
use Pimcore\Model\Element\DuplicateFullPathException;
3133
use Pimcore\Model\Element\ElementInterface;
3234
use Pimcore\Model\Element\ValidationException;
35+
use function sprintf;
3336

3437
/**
3538
* @internal
@@ -77,6 +80,10 @@ public function update(string $elementType, int $id, array $data): void
7780

7881
try {
7982
$this->elementSaveService->save($element, $user, $task);
83+
} catch (DuplicateFullPathException) {
84+
throw new ElementExistsException(
85+
message: sprintf('Element with full path [%s] already exists', $element->getRealFullPath())
86+
);
8087
} catch (ValidationException $e) {
8188
throw new FieldValidationFailedException($e->getMessage(), previous: $e);
8289
} catch (Exception $e) {

src/Util/Trait/ElementProviderTrait.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,11 @@ private function getCoreElementType(string $type): string
125125

126126
return $type;
127127
}
128+
129+
private function getElementFullPath(
130+
string $parentFullPath,
131+
string $key
132+
): string {
133+
return str_ends_with($parentFullPath, '/') === true ? $parentFullPath . $key : $parentFullPath . '/' . $key;
134+
}
128135
}

0 commit comments

Comments
 (0)