Skip to content

Commit 9427acd

Browse files
authored
[Data Object Creation] Catch duplicate full path exception (#920)
* Replaced generic exception with more specific exception * Apply php-cs-fixer changes * Added method to interface
1 parent 6f4629b commit 9427acd

2 files changed

Lines changed: 23 additions & 2 deletions

File tree

src/DataObject/Service/DataObjectService.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
use Pimcore\Bundle\StudioBackendBundle\DataObject\Util\Trait\ValidateObjectDataTrait;
3333
use Pimcore\Bundle\StudioBackendBundle\Element\Service\ElementSaveServiceInterface;
3434
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\DatabaseException;
35+
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\ElementExistsException;
3536
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\ElementSavingFailedException;
3637
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\ForbiddenException;
3738
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\InvalidElementTypeException;
@@ -47,6 +48,7 @@
4748
use Pimcore\Bundle\StudioBackendBundle\Util\Constant\ElementPermissions;
4849
use Pimcore\Bundle\StudioBackendBundle\Util\Constant\ElementTypes;
4950
use Pimcore\Bundle\StudioBackendBundle\Util\Constant\HttpResponseCodes;
51+
use Pimcore\Bundle\StudioBackendBundle\Util\Constant\HttpResponseErrorKeys;
5052
use Pimcore\Bundle\StudioBackendBundle\Util\Trait\ElementProviderTrait;
5153
use Pimcore\Bundle\StudioBackendBundle\Util\Trait\UserPermissionTrait;
5254
use Pimcore\Model\DataObject\AbstractObject;
@@ -89,6 +91,13 @@ public function __construct(
8991
) {
9092
}
9193

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+
92101
/**
93102
* @throws DatabaseException
94103
* @throws ElementSavingFailedException
@@ -103,8 +112,15 @@ public function addDataObject(
103112
): int {
104113
$user = $this->securityService->getCurrentUser();
105114
$parent = $this->getValidParent($user, $parentId);
106-
if ($this->dataObjectServiceResolver->pathExists($parent->getFullPath() . '/' . $parameters->getKey())) {
107-
throw new ElementSavingFailedException(null, 'Element with the same key and path already exists');
115+
$fullPath = $this->getDataObjectFullPath(
116+
$parent->getFullPath(),
117+
$parameters->getKey()
118+
);
119+
if ($this->dataObjectServiceResolver->pathExists($fullPath)) {
120+
throw new ElementExistsException(
121+
$fullPath,
122+
HttpResponseErrorKeys::ELEMENT_EXISTS->value
123+
);
108124
}
109125

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

src/DataObject/Service/DataObjectServiceInterface.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,9 @@ 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;
102107
}

0 commit comments

Comments
 (0)