Skip to content

Commit

Permalink
fix: throw better DuplicateOuuidException
Browse files Browse the repository at this point in the history
- newDocument is called on api update
- createData is called from ui
  • Loading branch information
Davidmattei committed Jul 12, 2024
1 parent bf6511d commit 821f972
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 24 deletions.
8 changes: 8 additions & 0 deletions EMS/core-bundle/src/Exception/DuplicateOuuidException.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
<?php

declare(strict_types=1);

namespace EMS\CoreBundle\Exception;

class DuplicateOuuidException extends ElasticmsException
{
public function __construct(string $ouuid, string $contentTypeName)
{
parent::__construct(
message: \sprintf('Duplicate ouuid "%s" for content type "%s"', $ouuid, $contentTypeName)
);
}
}
33 changes: 9 additions & 24 deletions EMS/core-bundle/src/Service/DataService.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormRegistryInterface;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\ConflictHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
Expand Down Expand Up @@ -443,32 +442,18 @@ public function createData(?string $ouuid, array $rawdata, ContentType $contentT
$newRevision->setEndTime(null);
$newRevision->setDeleted(false);
$newRevision->setDraft(true);
if ($byARealUser) {
$token = $this->tokenStorage->getToken();
if (null === $token) {
throw new \RuntimeException('Unexpected null token');
}
$newRevision->setLockBy($token->getUserIdentifier());
} else {
$newRevision->setLockBy('DATA_SERVICE');
}

$lockBy = $byARealUser ? $this->userService->getCurrentUser()->getUserIdentifier() : 'DATA_SERVICE';
$newRevision->setLockBy($lockBy);

$newRevision->setLockUntil($until);
$newRevision->setRawData($rawdata);

$em = $this->doctrine->getManager();
if (!empty($ouuid)) {
$revisionRepository = $em->getRepository(Revision::class);
$anotherObject = $revisionRepository->findOneBy([
'contentType' => $contentType,
'ouuid' => $newRevision->getOuuid(),
'endTime' => null,
]);

if (!empty($anotherObject)) {
throw new ConflictHttpException('Duplicate OUUID '.$ouuid.' for this content type');
}
if (null !== $ouuid && null !== $this->revRepository->getCurrentRevision($contentType, $ouuid)) {
throw new DuplicateOuuidException($ouuid, $contentType->getName());
}

$em = $this->doctrine->getManager();
$em->persist($newRevision);
$em->flush();

Expand Down Expand Up @@ -939,8 +924,8 @@ public function newDocument(ContentType $contentType, ?string $ouuid = null, ?ar

$revision = new Revision();

if (null !== $ouuid && $revisionRepository->countRevisions($ouuid, $contentType)) {
throw new DuplicateOuuidException();
if (null !== $ouuid && $revisionRepository->getCurrentRevision($contentType, $ouuid)) {
throw new DuplicateOuuidException($ouuid, $contentType->getName());
}

if (!empty($contentType->getDefaultValue())) {
Expand Down

0 comments on commit 821f972

Please sign in to comment.