From 39840b7ec3b359bee1bf089396afb6dc352df218 Mon Sep 17 00:00:00 2001 From: mattamon Date: Wed, 22 May 2024 10:24:20 +0200 Subject: [PATCH] cleanup --- src/Note/Controller/DeleteController.php | 4 ++++ src/Note/Controller/Element/CreateController.php | 4 ++++ src/Note/Repository/NoteRepository.php | 12 +++++++++++- src/Note/Repository/NoteRepositoryInterface.php | 4 ++++ src/Note/Request/NoteParameters.php | 1 - src/Note/Service/NoteService.php | 11 +++++++---- src/Note/Service/NoteServiceInterface.php | 4 ++++ 7 files changed, 34 insertions(+), 6 deletions(-) diff --git a/src/Note/Controller/DeleteController.php b/src/Note/Controller/DeleteController.php index 8fdbd437b..ea3adcafc 100644 --- a/src/Note/Controller/DeleteController.php +++ b/src/Note/Controller/DeleteController.php @@ -18,6 +18,7 @@ use OpenApi\Attributes\Delete; use Pimcore\Bundle\StudioBackendBundle\Controller\AbstractApiController; +use Pimcore\Bundle\StudioBackendBundle\Exception\ElementNotFoundException; use Pimcore\Bundle\StudioBackendBundle\Note\Service\NoteServiceInterface; use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Parameters\Path\IdParameter; use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\Content\IdJson; @@ -44,6 +45,9 @@ public function __construct( parent::__construct($serializer); } + /** + * @throws ElementNotFoundException + */ #[Route('/notes/{id}', name: 'pimcore_studio_api_delete_note', methods: ['DELETE'])] #[IsGranted(UserPermissions::NOTES_EVENTS->value)] #[Delete( diff --git a/src/Note/Controller/Element/CreateController.php b/src/Note/Controller/Element/CreateController.php index 6e588bfb9..2cf840648 100644 --- a/src/Note/Controller/Element/CreateController.php +++ b/src/Note/Controller/Element/CreateController.php @@ -18,6 +18,7 @@ use OpenApi\Attributes\Post; use Pimcore\Bundle\StudioBackendBundle\Controller\AbstractApiController; +use Pimcore\Bundle\StudioBackendBundle\Exception\ElementSavingFailedException; use Pimcore\Bundle\StudioBackendBundle\Note\Attributes\Request\CreateNoteRequestBody; use Pimcore\Bundle\StudioBackendBundle\Note\Request\NoteElement; use Pimcore\Bundle\StudioBackendBundle\Note\Schema\CreateNote; @@ -47,6 +48,9 @@ public function __construct( parent::__construct($serializer); } + /** + * @throws ElementSavingFailedException + */ #[Route('/notes/{elementType}/{id}', name: 'pimcore_studio_api_create_element_note', methods: ['POST'])] #[IsGranted(UserPermissions::NOTES_EVENTS->value)] #[Post( diff --git a/src/Note/Repository/NoteRepository.php b/src/Note/Repository/NoteRepository.php index 7509c6a25..1d8ce0df8 100644 --- a/src/Note/Repository/NoteRepository.php +++ b/src/Note/Repository/NoteRepository.php @@ -16,8 +16,10 @@ namespace Pimcore\Bundle\StudioBackendBundle\Note\Repository; +use Exception; use Pimcore\Bundle\StaticResolverBundle\Models\Element\NoteResolverInterface; use Pimcore\Bundle\StudioBackendBundle\Exception\ElementNotFoundException; +use Pimcore\Bundle\StudioBackendBundle\Exception\ElementSavingFailedException; use Pimcore\Bundle\StudioBackendBundle\Note\Request\NoteElement; use Pimcore\Bundle\StudioBackendBundle\Note\Request\NoteParameters; use Pimcore\Bundle\StudioBackendBundle\Note\Schema\CreateNote; @@ -33,6 +35,9 @@ public function __construct(private NoteResolverInterface $noteResolver) { } + /** + * @throws ElementSavingFailedException + */ public function createNote(NoteElement $noteElement, CreateNote $createNote): Note { $note = new Note(); @@ -43,7 +48,12 @@ public function createNote(NoteElement $noteElement, CreateNote $createNote): No $note->setDescription($createNote->getDescription()); $note->setType($createNote->getType()); $note->setLocked(false); - $note->save(); + + try { + $note->save(); + } catch (Exception $e) { + throw new ElementSavingFailedException(0, $e->getTraceAsString()); + } return $note; } diff --git a/src/Note/Repository/NoteRepositoryInterface.php b/src/Note/Repository/NoteRepositoryInterface.php index f2890dbd6..34c53b986 100644 --- a/src/Note/Repository/NoteRepositoryInterface.php +++ b/src/Note/Repository/NoteRepositoryInterface.php @@ -17,6 +17,7 @@ namespace Pimcore\Bundle\StudioBackendBundle\Note\Repository; use Pimcore\Bundle\StudioBackendBundle\Exception\ElementNotFoundException; +use Pimcore\Bundle\StudioBackendBundle\Exception\ElementSavingFailedException; use Pimcore\Bundle\StudioBackendBundle\Note\Request\NoteElement; use Pimcore\Bundle\StudioBackendBundle\Note\Request\NoteParameters; use Pimcore\Bundle\StudioBackendBundle\Note\Schema\CreateNote; @@ -28,6 +29,9 @@ */ interface NoteRepositoryInterface { + /** + * @throws ElementSavingFailedException + */ public function createNote(NoteElement $noteElement, CreateNote $createNote): Note; public function getNote(int $id): Note; diff --git a/src/Note/Request/NoteParameters.php b/src/Note/Request/NoteParameters.php index e0bf1880b..9e493d195 100644 --- a/src/Note/Request/NoteParameters.php +++ b/src/Note/Request/NoteParameters.php @@ -17,7 +17,6 @@ namespace Pimcore\Bundle\StudioBackendBundle\Note\Request; use Pimcore\Bundle\StudioBackendBundle\Request\CollectionParameters; -use Pimcore\Bundle\StudioBackendBundle\Util\Constants\ElementTypes; /** * @internal diff --git a/src/Note/Service/NoteService.php b/src/Note/Service/NoteService.php index 9f6f7ae21..3aa0efadb 100644 --- a/src/Note/Service/NoteService.php +++ b/src/Note/Service/NoteService.php @@ -17,10 +17,10 @@ namespace Pimcore\Bundle\StudioBackendBundle\Note\Service; use Pimcore\Bundle\StudioBackendBundle\Exception\ElementNotFoundException; +use Pimcore\Bundle\StudioBackendBundle\Exception\ElementSavingFailedException; use Pimcore\Bundle\StudioBackendBundle\Note\Hydrator\NoteHydratorInterface; use Pimcore\Bundle\StudioBackendBundle\Note\Repository\NoteRepositoryInterface; use Pimcore\Bundle\StudioBackendBundle\Note\Request\NoteElement; -use Pimcore\Bundle\StudioBackendBundle\Note\Request\NoteElementInterface; use Pimcore\Bundle\StudioBackendBundle\Note\Request\NoteParameters; use Pimcore\Bundle\StudioBackendBundle\Note\Response\Collection; use Pimcore\Bundle\StudioBackendBundle\Note\Schema\CreateNote; @@ -29,16 +29,19 @@ /** * @internal */ -final class NoteService implements NoteServiceInterface +final readonly class NoteService implements NoteServiceInterface { public function __construct( - private readonly NoteRepositoryInterface $noteRepository, - private readonly NoteHydratorInterface $noteHydrator + private NoteRepositoryInterface $noteRepository, + private NoteHydratorInterface $noteHydrator ) { } + /** + * @throws ElementSavingFailedException + */ public function createNote(NoteElement $noteElement, CreateNote $createNote): Note { $note = $this->noteRepository->createNote($noteElement, $createNote); diff --git a/src/Note/Service/NoteServiceInterface.php b/src/Note/Service/NoteServiceInterface.php index 65f555c76..8512bd6b0 100644 --- a/src/Note/Service/NoteServiceInterface.php +++ b/src/Note/Service/NoteServiceInterface.php @@ -17,6 +17,7 @@ namespace Pimcore\Bundle\StudioBackendBundle\Note\Service; use Pimcore\Bundle\StudioBackendBundle\Exception\ElementNotFoundException; +use Pimcore\Bundle\StudioBackendBundle\Exception\ElementSavingFailedException; use Pimcore\Bundle\StudioBackendBundle\Note\Request\NoteElement; use Pimcore\Bundle\StudioBackendBundle\Note\Request\NoteParameters; use Pimcore\Bundle\StudioBackendBundle\Note\Response\Collection; @@ -28,6 +29,9 @@ */ interface NoteServiceInterface { + /** + * @throws ElementSavingFailedException + */ public function createNote(NoteElement $noteElement, CreateNote $createNote): Note; public function listNotes(NoteElement $noteElement, NoteParameters $parameters): Collection;