From 0384279c03e1629df1e7625bebf2699467002140 Mon Sep 17 00:00:00 2001 From: Matthias Schuhmayer <38959016+mattamon@users.noreply.github.com> Date: Mon, 3 Jun 2024 16:19:56 +0200 Subject: [PATCH] Switch to path parameters for element (#83) * Change to path parameters * Consistency checks * Remove positive integer * Rename request to MappedParameter * Remove unused class --- .../{ => Element}/CollectionController.php | 30 +++++---- .../Request/DependencyParameters.php | 18 +----- src/Dependency/Service/DependencyService.php | 24 +++++--- .../Service/DependencyServiceInterface.php | 2 + .../Controller/CollectionController.php | 2 +- .../PropertiesParameters.php | 2 +- .../Repository/PropertyRepository.php | 2 +- .../PropertyRepositoryInterface.php | 2 +- .../Request/UpdateElementProperties.php | 45 -------------- src/Property/Service/PropertyService.php | 3 +- .../Service/PropertyServiceInterface.php | 2 +- src/Request/ElementParameters.php | 61 +++++++++++++++++++ .../{ => Element}/CleanupController.php | 26 +++++--- .../{ => Element}/CollectionController.php | 30 +++++---- src/Version/Repository/VersionRepository.php | 16 ++--- .../Repository/VersionRepositoryInterface.php | 9 ++- .../Request/VersionCleanupParameters.php | 12 ---- src/Version/Request/VersionParameters.php | 49 --------------- src/Version/Service/VersionService.php | 22 +++++-- .../Service/VersionServiceInterface.php | 12 +++- 20 files changed, 180 insertions(+), 189 deletions(-) rename src/Dependency/Controller/{ => Element}/CollectionController.php (81%) rename src/Property/{Request => MappedParameter}/PropertiesParameters.php (93%) delete mode 100644 src/Property/Request/UpdateElementProperties.php create mode 100644 src/Request/ElementParameters.php rename src/Version/Controller/{ => Element}/CleanupController.php (73%) rename src/Version/Controller/{ => Element}/CollectionController.php (79%) delete mode 100644 src/Version/Request/VersionParameters.php diff --git a/src/Dependency/Controller/CollectionController.php b/src/Dependency/Controller/Element/CollectionController.php similarity index 81% rename from src/Dependency/Controller/CollectionController.php rename to src/Dependency/Controller/Element/CollectionController.php index 69a0611c5..0ca47fb3e 100644 --- a/src/Dependency/Controller/CollectionController.php +++ b/src/Dependency/Controller/Element/CollectionController.php @@ -14,7 +14,7 @@ * @license http://www.pimcore.org/license GPLv3 and PCL */ -namespace Pimcore\Bundle\StudioBackendBundle\Dependency\Controller; +namespace Pimcore\Bundle\StudioBackendBundle\Dependency\Controller\Element; use OpenApi\Attributes\Get; use Pimcore\Bundle\StudioBackendBundle\Controller\AbstractApiController; @@ -22,14 +22,15 @@ use Pimcore\Bundle\StudioBackendBundle\Dependency\Attributes\Response\Property\DependencyCollection; use Pimcore\Bundle\StudioBackendBundle\Dependency\Request\DependencyParameters; use Pimcore\Bundle\StudioBackendBundle\Dependency\Service\DependencyServiceInterface; -use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Parameters\Query\ElementTypeParameter; -use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Parameters\Query\IdParameter; +use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Parameters\Path\ElementTypeParameter; +use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Parameters\Path\IdParameter; use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Parameters\Query\PageParameter; use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Parameters\Query\PageSizeParameter; use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\Content\CollectionJson; use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\DefaultResponses; use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\SuccessResponse; use Pimcore\Bundle\StudioBackendBundle\OpenApi\Config\Tags; +use Pimcore\Bundle\StudioBackendBundle\Request\ElementParameters; use Pimcore\Bundle\StudioBackendBundle\Security\Service\SecurityServiceInterface; use Pimcore\Bundle\StudioBackendBundle\Util\Constants\HttpResponseCodes; use Pimcore\Bundle\StudioBackendBundle\Util\Traits\PaginatedResponseTrait; @@ -48,28 +49,28 @@ final class CollectionController extends AbstractApiController public function __construct( SerializerInterface $serializer, private readonly SecurityServiceInterface $securityService, - private readonly DependencyServiceInterface $hydratorService, + private readonly DependencyServiceInterface $dependencyService, ) { parent::__construct($serializer); } - #[Route('/dependencies', name: 'pimcore_studio_api_dependencies', methods: ['GET'])] + #[Route('/dependencies/{elementType}/{id}', name: 'pimcore_studio_api_dependencies', methods: ['GET'])] //#[IsGranted('STUDIO_API')] #[Get( - path: self::API_PATH . '/dependencies', + path: self::API_PATH . '/dependencies/{elementType}/{id}', operationId: 'getDependencies', - description: 'Get paginated dependencies. - Pass dependency mode to get either all elements that depend on the provided element + description: 'Get paginated dependencies. + Pass dependency mode to get either all elements that depend on the provided element or all dependencies for the provided element.', summary: 'Get all dependencies for provided element.', security: self::SECURITY_SCHEME, tags: [Tags::Dependencies->name] )] + #[ElementTypeParameter] + #[IdParameter('element')] #[PageParameter] #[PageSizeParameter] - #[IdParameter('ID of the element', 'element')] #[DependencyModeParameter] - #[ElementTypeParameter] #[SuccessResponse( description: 'Paginated dependencies with total count as header param', content: new CollectionJson(new DependencyCollection()) @@ -78,9 +79,14 @@ public function __construct( HttpResponseCodes::UNAUTHORIZED, HttpResponseCodes::NOT_FOUND, ])] - public function getDependencies(#[MapQueryString] DependencyParameters $parameters): JsonResponse + public function getDependencies( + string $elementType, + int $id, + #[MapQueryString] DependencyParameters $parameters + ): JsonResponse { - $collection = $this->hydratorService->getDependencies( + $collection = $this->dependencyService->getDependencies( + new ElementParameters($elementType, $id), $parameters, $this->securityService->getCurrentUser() ); diff --git a/src/Dependency/Request/DependencyParameters.php b/src/Dependency/Request/DependencyParameters.php index a31f3d8ea..ec0519be3 100644 --- a/src/Dependency/Request/DependencyParameters.php +++ b/src/Dependency/Request/DependencyParameters.php @@ -30,9 +30,7 @@ public function __construct( int $page, int $pageSize, - string $dependencyMode, - private int $elementId, - private string $elementType + string $dependencyMode ) { $this->mode = $this->getDependencyMode($dependencyMode); parent::__construct($page, $pageSize); @@ -43,20 +41,6 @@ public function getMode(): DependencyMode return $this->mode; } - public function getElementId(): int - { - return $this->elementId; - } - - public function getElementType(): string - { - if ($this->elementType === ElementTypes::TYPE_DATA_OBJECT) { - return ElementTypes::TYPE_OBJECT; - } - - return $this->elementType; - } - private function getDependencyMode(string $mode): DependencyMode { $dependencyMode = DependencyMode::tryFrom($mode); diff --git a/src/Dependency/Service/DependencyService.php b/src/Dependency/Service/DependencyService.php index f7dc8f92b..6e033411e 100644 --- a/src/Dependency/Service/DependencyService.php +++ b/src/Dependency/Service/DependencyService.php @@ -21,6 +21,7 @@ use Pimcore\Bundle\StudioBackendBundle\Dependency\Repository\DependencyRepositoryInterface; use Pimcore\Bundle\StudioBackendBundle\Dependency\Request\DependencyParameters; use Pimcore\Bundle\StudioBackendBundle\Dependency\Response\Collection; +use Pimcore\Bundle\StudioBackendBundle\Request\ElementParameters; use Pimcore\Bundle\StudioBackendBundle\Util\Traits\ElementProviderTrait; use Pimcore\Model\UserInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface; @@ -36,13 +37,14 @@ public function __construct( ) { } public function getDependencies( + ElementParameters $elementParameters, DependencyParameters $parameters, UserInterface $user ): Collection { return match ($parameters->getMode()) { - DependencyMode::REQUIRES => $this->getRequiredDependencies($parameters), - DependencyMode::REQUIRED_BY => $this->getRequiredByDependencies($parameters), + DependencyMode::REQUIRES => $this->getRequiredDependencies($elementParameters, $parameters), + DependencyMode::REQUIRED_BY => $this->getRequiredByDependencies($elementParameters, $parameters), }; } @@ -65,12 +67,13 @@ private function getDependencyCollection(array $dependencies): array } private function getRequiredDependencies( + ElementParameters $elementParameters, DependencyParameters $parameters ): Collection { $dependencies = $this->dependencyRepository->listRequiresDependencies( - $parameters->getElementType(), - $parameters->getElementId() + $elementParameters->getType(), + $elementParameters->getId() ); $dependencies = $this->getDependencyCollection($dependencies); @@ -80,18 +83,19 @@ private function getRequiredDependencies( $parameters->getPage(), $parameters->getPageSize(), $this->dependencyRepository->listRequiresDependenciesTotalCount( - $parameters->getElementType(), - $parameters->getElementId() + $elementParameters->getType(), + $elementParameters->getId() ) ); } private function getRequiredByDependencies( + ElementParameters $elementParameters, DependencyParameters $parameters ): Collection { $dependencies = $this->dependencyRepository->listRequiredByDependencies( - $parameters->getElementType(), - $parameters->getElementId() + $elementParameters->getType(), + $elementParameters->getId() ); $dependencies = $this->getDependencyCollection($dependencies); @@ -101,8 +105,8 @@ private function getRequiredByDependencies( $parameters->getPage(), $parameters->getPageSize(), $this->dependencyRepository->listRequiredByDependenciesTotalCount( - $parameters->getElementType(), - $parameters->getElementId() + $elementParameters->getType(), + $elementParameters->getId() ) ); } diff --git a/src/Dependency/Service/DependencyServiceInterface.php b/src/Dependency/Service/DependencyServiceInterface.php index b0f9a1ea5..881c52edf 100644 --- a/src/Dependency/Service/DependencyServiceInterface.php +++ b/src/Dependency/Service/DependencyServiceInterface.php @@ -18,11 +18,13 @@ use Pimcore\Bundle\StudioBackendBundle\Dependency\Request\DependencyParameters; use Pimcore\Bundle\StudioBackendBundle\Dependency\Response\Collection; +use Pimcore\Bundle\StudioBackendBundle\Request\ElementParameters; use Pimcore\Model\UserInterface; interface DependencyServiceInterface { public function getDependencies( + ElementParameters $elementParameters, DependencyParameters $parameters, UserInterface $user ): Collection; diff --git a/src/Property/Controller/CollectionController.php b/src/Property/Controller/CollectionController.php index 99019d534..5b19398e3 100644 --- a/src/Property/Controller/CollectionController.php +++ b/src/Property/Controller/CollectionController.php @@ -24,7 +24,7 @@ use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\DefaultResponses; use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\SuccessResponse; use Pimcore\Bundle\StudioBackendBundle\OpenApi\Config\Tags; -use Pimcore\Bundle\StudioBackendBundle\Property\Request\PropertiesParameters; +use Pimcore\Bundle\StudioBackendBundle\Property\MappedParameter\PropertiesParameters; use Pimcore\Bundle\StudioBackendBundle\Property\Schema\PredefinedProperty; use Pimcore\Bundle\StudioBackendBundle\Property\Service\PropertyServiceInterface; use Pimcore\Bundle\StudioBackendBundle\Util\Constants\HttpResponseCodes; diff --git a/src/Property/Request/PropertiesParameters.php b/src/Property/MappedParameter/PropertiesParameters.php similarity index 93% rename from src/Property/Request/PropertiesParameters.php rename to src/Property/MappedParameter/PropertiesParameters.php index 4e42e8aa1..b4b942ec8 100644 --- a/src/Property/Request/PropertiesParameters.php +++ b/src/Property/MappedParameter/PropertiesParameters.php @@ -14,7 +14,7 @@ * @license http://www.pimcore.org/license GPLv3 and PCL */ -namespace Pimcore\Bundle\StudioBackendBundle\Property\Request; +namespace Pimcore\Bundle\StudioBackendBundle\Property\MappedParameter; use Pimcore\Bundle\StudioBackendBundle\Util\Constants\ElementTypes; diff --git a/src/Property/Repository/PropertyRepository.php b/src/Property/Repository/PropertyRepository.php index 227515ec0..a95097b83 100644 --- a/src/Property/Repository/PropertyRepository.php +++ b/src/Property/Repository/PropertyRepository.php @@ -19,7 +19,7 @@ use Pimcore\Bundle\StaticResolverBundle\Models\Predefined\PredefinedResolverInterface; use Pimcore\Bundle\StudioBackendBundle\Exception\NotWriteableException; use Pimcore\Bundle\StudioBackendBundle\Exception\PropertyNotFoundException; -use Pimcore\Bundle\StudioBackendBundle\Property\Request\PropertiesParameters; +use Pimcore\Bundle\StudioBackendBundle\Property\MappedParameter\PropertiesParameters; use Pimcore\Bundle\StudioBackendBundle\Property\Schema\UpdatePredefinedProperty; use Pimcore\Bundle\StudioBackendBundle\Util\Constants\ElementTypes; use Pimcore\Bundle\StudioBackendBundle\Util\Traits\ElementProviderTrait; diff --git a/src/Property/Repository/PropertyRepositoryInterface.php b/src/Property/Repository/PropertyRepositoryInterface.php index 1dc6cb325..c80768945 100644 --- a/src/Property/Repository/PropertyRepositoryInterface.php +++ b/src/Property/Repository/PropertyRepositoryInterface.php @@ -18,7 +18,7 @@ use Pimcore\Bundle\StudioBackendBundle\Exception\NotWriteableException; use Pimcore\Bundle\StudioBackendBundle\Exception\PropertyNotFoundException; -use Pimcore\Bundle\StudioBackendBundle\Property\Request\PropertiesParameters; +use Pimcore\Bundle\StudioBackendBundle\Property\MappedParameter\PropertiesParameters; use Pimcore\Bundle\StudioBackendBundle\Property\Schema\UpdatePredefinedProperty; use Pimcore\Model\Element\ElementInterface; use Pimcore\Model\Property\Predefined; diff --git a/src/Property/Request/UpdateElementProperties.php b/src/Property/Request/UpdateElementProperties.php deleted file mode 100644 index bc92ffe7d..000000000 --- a/src/Property/Request/UpdateElementProperties.php +++ /dev/null @@ -1,45 +0,0 @@ -properties = array_map(static function (array $propertyData) { - return new UpdateElementProperty( - $propertyData['key'], - $propertyData['data'], - $propertyData['type'], - $propertyData['inheritable'], - ); - }, $items); - } - - public function getProperties(): array - { - return $this->properties; - } -} diff --git a/src/Property/Service/PropertyService.php b/src/Property/Service/PropertyService.php index c6d2eae3f..5c1a26694 100644 --- a/src/Property/Service/PropertyService.php +++ b/src/Property/Service/PropertyService.php @@ -23,8 +23,7 @@ use Pimcore\Bundle\StudioBackendBundle\Property\Event\PredefinedPropertyEvent; use Pimcore\Bundle\StudioBackendBundle\Property\Hydrator\PropertyHydratorInterface; use Pimcore\Bundle\StudioBackendBundle\Property\Repository\PropertyRepositoryInterface; -use Pimcore\Bundle\StudioBackendBundle\Property\Request\PropertiesParameters; -use Pimcore\Bundle\StudioBackendBundle\Property\Request\UpdateElementProperties; +use Pimcore\Bundle\StudioBackendBundle\Property\MappedParameter\PropertiesParameters; use Pimcore\Bundle\StudioBackendBundle\Property\Schema\ElementProperty; use Pimcore\Bundle\StudioBackendBundle\Property\Schema\PredefinedProperty; use Pimcore\Bundle\StudioBackendBundle\Property\Schema\UpdatePredefinedProperty; diff --git a/src/Property/Service/PropertyServiceInterface.php b/src/Property/Service/PropertyServiceInterface.php index a3aafad29..a2345fc93 100644 --- a/src/Property/Service/PropertyServiceInterface.php +++ b/src/Property/Service/PropertyServiceInterface.php @@ -19,7 +19,7 @@ use Pimcore\Bundle\StudioBackendBundle\Exception\ElementNotFoundException; use Pimcore\Bundle\StudioBackendBundle\Exception\NotWriteableException; use Pimcore\Bundle\StudioBackendBundle\Exception\PropertyNotFoundException; -use Pimcore\Bundle\StudioBackendBundle\Property\Request\PropertiesParameters; +use Pimcore\Bundle\StudioBackendBundle\Property\MappedParameter\PropertiesParameters; use Pimcore\Bundle\StudioBackendBundle\Property\Schema\ElementProperty; use Pimcore\Bundle\StudioBackendBundle\Property\Schema\PredefinedProperty; use Pimcore\Bundle\StudioBackendBundle\Property\Schema\UpdatePredefinedProperty; diff --git a/src/Request/ElementParameters.php b/src/Request/ElementParameters.php new file mode 100644 index 000000000..7f00b961c --- /dev/null +++ b/src/Request/ElementParameters.php @@ -0,0 +1,61 @@ +validate(); + } + + public function getType(): string + { + if ($this->type === ElementTypes::TYPE_DATA_OBJECT) { + return ElementTypes::TYPE_OBJECT; + } + return $this->type; + } + + public function getId(): int + { + return $this->id; + } + + /** + * @throws InvalidElementTypeException + */ + private function validate(): void + { + if (!in_array($this->type, ElementTypes::ALLOWED_TYPES)) { + throw new InvalidElementTypeException($this->type); + } + } +} diff --git a/src/Version/Controller/CleanupController.php b/src/Version/Controller/Element/CleanupController.php similarity index 73% rename from src/Version/Controller/CleanupController.php rename to src/Version/Controller/Element/CleanupController.php index e13e382f8..30ebd7c38 100644 --- a/src/Version/Controller/CleanupController.php +++ b/src/Version/Controller/Element/CleanupController.php @@ -14,20 +14,22 @@ * @license http://www.pimcore.org/license GPLv3 and PCL */ -namespace Pimcore\Bundle\StudioBackendBundle\Version\Controller; +namespace Pimcore\Bundle\StudioBackendBundle\Version\Controller\Element; use OpenApi\Attributes\Delete; use Pimcore\Bundle\StudioBackendBundle\Controller\AbstractApiController; -use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Parameters\Query\ElementTypeParameter; -use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Parameters\Query\IdParameter; +use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Parameters\Path\ElementTypeParameter; +use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Parameters\Path\IdParameter; use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Parameters\Query\TimestampParameter; use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\Content\IdsJson; use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\DefaultResponses; use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\SuccessResponse; use Pimcore\Bundle\StudioBackendBundle\OpenApi\Config\Tags; +use Pimcore\Bundle\StudioBackendBundle\Request\ElementParameters; use Pimcore\Bundle\StudioBackendBundle\Util\Constants\HttpResponseCodes; use Pimcore\Bundle\StudioBackendBundle\Version\Repository\VersionRepositoryInterface; use Pimcore\Bundle\StudioBackendBundle\Version\Request\VersionCleanupParameters; +use Pimcore\Bundle\StudioBackendBundle\Version\Service\VersionServiceInterface; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpKernel\Attribute\MapQueryString; use Symfony\Component\Routing\Attribute\Route; @@ -39,24 +41,24 @@ final class CleanupController extends AbstractApiController { public function __construct( - private readonly VersionRepositoryInterface $repository, + private readonly VersionServiceInterface $versionService, SerializerInterface $serializer, ) { parent::__construct($serializer); } - #[Route('/versions', name: 'pimcore_studio_api_cleanup_versions', methods: ['DELETE'])] + #[Route('/versions/{elementType}/{id}', name: 'pimcore_studio_api_cleanup_versions', methods: ['DELETE'])] //#[IsGranted('STUDIO_API')] #[Delete( - path: self::API_PATH . '/versions', + path: self::API_PATH . '/versions/{elementType}/{id}', operationId: 'cleanupVersion', description: 'Cleanup versions based on the provided parameters', summary: 'Cleanup versions', security: self::SECURITY_SCHEME, tags: [Tags::Versions->name] )] - #[IdParameter('ID of the element', 'element')] #[ElementTypeParameter] + #[IdParameter('ID of the element')] #[TimestampParameter('elementModificationDate', 'Modification timestamp of the element', true)] #[SuccessResponse( description: 'IDs of deleted versions', @@ -66,8 +68,14 @@ public function __construct( HttpResponseCodes::UNAUTHORIZED, HttpResponseCodes::NOT_FOUND, ])] - public function cleanupVersions(#[MapQueryString] VersionCleanupParameters $parameters): JsonResponse + public function cleanupVersions( + string $elementType, + int $id, + #[MapQueryString] VersionCleanupParameters $parameters + ): JsonResponse { - return $this->jsonResponse(['ids' => $this->repository->cleanupVersions($parameters)]); + return $this->jsonResponse( + ['ids' => $this->versionService->cleanupVersions(new ElementParameters($elementType, $id), $parameters)] + ); } } diff --git a/src/Version/Controller/CollectionController.php b/src/Version/Controller/Element/CollectionController.php similarity index 79% rename from src/Version/Controller/CollectionController.php rename to src/Version/Controller/Element/CollectionController.php index 05ed878c8..513db17ba 100644 --- a/src/Version/Controller/CollectionController.php +++ b/src/Version/Controller/Element/CollectionController.php @@ -14,23 +14,24 @@ * @license http://www.pimcore.org/license GPLv3 and PCL */ -namespace Pimcore\Bundle\StudioBackendBundle\Version\Controller; +namespace Pimcore\Bundle\StudioBackendBundle\Version\Controller\Element; use OpenApi\Attributes\Get; use Pimcore\Bundle\StudioBackendBundle\Controller\AbstractApiController; -use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Parameters\Query\ElementTypeParameter; -use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Parameters\Query\IdParameter; +use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Parameters\Path\ElementTypeParameter; +use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Parameters\Path\IdParameter; use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Parameters\Query\PageParameter; use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Parameters\Query\PageSizeParameter; use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\Content\CollectionJson; use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\DefaultResponses; use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\SuccessResponse; use Pimcore\Bundle\StudioBackendBundle\OpenApi\Config\Tags; +use Pimcore\Bundle\StudioBackendBundle\Request\CollectionParameters; +use Pimcore\Bundle\StudioBackendBundle\Request\ElementParameters; use Pimcore\Bundle\StudioBackendBundle\Security\Service\SecurityServiceInterface; use Pimcore\Bundle\StudioBackendBundle\Util\Constants\HttpResponseCodes; use Pimcore\Bundle\StudioBackendBundle\Util\Traits\PaginatedResponseTrait; use Pimcore\Bundle\StudioBackendBundle\Version\Attributes\Response\Property\VersionCollection; -use Pimcore\Bundle\StudioBackendBundle\Version\Request\VersionParameters; use Pimcore\Bundle\StudioBackendBundle\Version\Service\VersionServiceInterface; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpKernel\Attribute\MapQueryString; @@ -46,26 +47,26 @@ final class CollectionController extends AbstractApiController public function __construct( SerializerInterface $serializer, - private readonly SecurityServiceInterface $securityService, - private readonly VersionServiceInterface $hydratorService, + private readonly VersionServiceInterface $versionService, + private readonly SecurityServiceInterface $securityService ) { parent::__construct($serializer); } - #[Route('/versions', name: 'pimcore_studio_api_versions', methods: ['GET'])] + #[Route('/versions/{elementType}/{id}', name: 'pimcore_studio_api_versions', methods: ['GET'])] //#[IsGranted('STUDIO_API')] #[Get( - path: self::API_PATH . '/versions', + path: self::API_PATH . '/versions/{elementType}/{id}', operationId: 'getVersions', description: 'Get paginated versions', summary: 'Get all versions of element', security: self::SECURITY_SCHEME, tags: [Tags::Versions->name] )] + #[ElementTypeParameter] + #[IdParameter('element')] #[PageParameter] #[PageSizeParameter] - #[IdParameter('ID of the element', 'element')] - #[ElementTypeParameter] #[SuccessResponse( description: 'Paginated versions with total count as header param', content: new CollectionJson(new VersionCollection()) @@ -74,9 +75,14 @@ public function __construct( HttpResponseCodes::UNAUTHORIZED, HttpResponseCodes::NOT_FOUND, ])] - public function getVersions(#[MapQueryString] VersionParameters $parameters): JsonResponse + public function getVersions( + string $elementType, + int $id, + #[MapQueryString] CollectionParameters $parameters + ): JsonResponse { - $collection = $this->hydratorService->getVersions( + $collection = $this->versionService->getVersions( + new ElementParameters($elementType, $id), $parameters, $this->securityService->getCurrentUser() ); diff --git a/src/Version/Repository/VersionRepository.php b/src/Version/Repository/VersionRepository.php index 10f5e752f..f5d282af1 100644 --- a/src/Version/Repository/VersionRepository.php +++ b/src/Version/Repository/VersionRepository.php @@ -18,6 +18,8 @@ use Pimcore\Bundle\StaticResolverBundle\Models\Version\VersionResolverInterface; use Pimcore\Bundle\StudioBackendBundle\Exception\ElementNotFoundException; +use Pimcore\Bundle\StudioBackendBundle\Request\CollectionParameters; +use Pimcore\Bundle\StudioBackendBundle\Request\ElementParameters; use Pimcore\Bundle\StudioBackendBundle\Security\Service\SecurityServiceInterface; use Pimcore\Bundle\StudioBackendBundle\Util\Constants\ElementPermissions; use Pimcore\Bundle\StudioBackendBundle\Util\Traits\ElementProviderTrait; @@ -33,8 +35,6 @@ */ final readonly class VersionRepository implements VersionRepositoryInterface { - use ElementProviderTrait; - public function __construct( private SecurityServiceInterface $securityService, private VersionResolverInterface $versionResolver @@ -44,7 +44,8 @@ public function __construct( public function listVersions( ElementInterface $element, - VersionParameters $parameters, + string $originalType, + CollectionParameters $parameters, UserInterface $user ): VersionListing { $this->securityService->hasElementPermission( @@ -56,8 +57,8 @@ public function listVersions( $limit = $parameters->getPageSize(); $page = $parameters->getPage(); $list = $this->getElementVersionsListing( - $parameters->getElementId(), - $parameters->getElementType(), + $element->getId(), + $originalType, $user->getId() ); $paginatedList = $list; @@ -113,14 +114,15 @@ public function getVersionById( } public function cleanupVersions( + ElementParameters $elementParameters, VersionCleanupParameters $parameters ): array { $deletedVersions = []; $list = $this->getVersionListing( 'cid = ? AND ctype = ? AND date <> ?', [ - $parameters->getElementId(), - $parameters->getElementType(), + $elementParameters->getId(), + $elementParameters->getType(), $parameters->getElementModificationDate(), ] ); diff --git a/src/Version/Repository/VersionRepositoryInterface.php b/src/Version/Repository/VersionRepositoryInterface.php index 9d018c6e7..ab616ed54 100644 --- a/src/Version/Repository/VersionRepositoryInterface.php +++ b/src/Version/Repository/VersionRepositoryInterface.php @@ -16,8 +16,9 @@ namespace Pimcore\Bundle\StudioBackendBundle\Version\Repository; +use Pimcore\Bundle\StudioBackendBundle\Request\CollectionParameters; +use Pimcore\Bundle\StudioBackendBundle\Request\ElementParameters; use Pimcore\Bundle\StudioBackendBundle\Version\Request\VersionCleanupParameters; -use Pimcore\Bundle\StudioBackendBundle\Version\Request\VersionParameters; use Pimcore\Model\Element\ElementInterface; use Pimcore\Model\UserInterface; use Pimcore\Model\Version; @@ -30,7 +31,8 @@ interface VersionRepositoryInterface { public function listVersions( ElementInterface $element, - VersionParameters $parameters, + string $originalType, + CollectionParameters $parameters, UserInterface $user ): VersionListing; @@ -50,6 +52,7 @@ public function getVersionById( ): Version; public function cleanupVersions( - VersionCleanupParameters $parameters, + ElementParameters $elementParameters, + VersionCleanupParameters $parameters ): array; } diff --git a/src/Version/Request/VersionCleanupParameters.php b/src/Version/Request/VersionCleanupParameters.php index b7480c101..ad602223c 100644 --- a/src/Version/Request/VersionCleanupParameters.php +++ b/src/Version/Request/VersionCleanupParameters.php @@ -22,22 +22,10 @@ final readonly class VersionCleanupParameters { public function __construct( - private int $elementId, - private string $elementType, private int $elementModificationDate ) { } - public function getElementId(): int - { - return $this->elementId; - } - - public function getElementType(): string - { - return $this->elementType; - } - public function getElementModificationDate(): int { return $this->elementModificationDate; diff --git a/src/Version/Request/VersionParameters.php b/src/Version/Request/VersionParameters.php deleted file mode 100644 index c136fda76..000000000 --- a/src/Version/Request/VersionParameters.php +++ /dev/null @@ -1,49 +0,0 @@ -elementId; - } - - public function getElementType(): string - { - if ($this->elementType === ElementTypes::TYPE_DATA_OBJECT) { - return ElementTypes::TYPE_OBJECT; - } - - return $this->elementType; - } -} diff --git a/src/Version/Service/VersionService.php b/src/Version/Service/VersionService.php index dcd272047..3eb034489 100644 --- a/src/Version/Service/VersionService.php +++ b/src/Version/Service/VersionService.php @@ -19,12 +19,15 @@ use Pimcore\Bundle\StaticResolverBundle\Models\Element\ServiceResolverInterface; use Pimcore\Bundle\StudioBackendBundle\Exception\ElementPublishingFailedException; use Pimcore\Bundle\StudioBackendBundle\Exception\InvalidElementTypeException; +use Pimcore\Bundle\StudioBackendBundle\Request\CollectionParameters; +use Pimcore\Bundle\StudioBackendBundle\Request\ElementParameters; use Pimcore\Bundle\StudioBackendBundle\Security\Service\SecurityServiceInterface; use Pimcore\Bundle\StudioBackendBundle\Util\Constants\ElementPermissions; use Pimcore\Bundle\StudioBackendBundle\Util\Traits\ElementProviderTrait; use Pimcore\Bundle\StudioBackendBundle\Version\Event\VersionEvent; use Pimcore\Bundle\StudioBackendBundle\Version\Hydrator\VersionHydratorInterface; use Pimcore\Bundle\StudioBackendBundle\Version\Repository\VersionRepositoryInterface; +use Pimcore\Bundle\StudioBackendBundle\Version\Request\VersionCleanupParameters; use Pimcore\Bundle\StudioBackendBundle\Version\Request\VersionParameters; use Pimcore\Bundle\StudioBackendBundle\Version\Response\Collection; use Pimcore\Model\Element\ElementInterface; @@ -50,16 +53,19 @@ public function __construct( } public function getVersions( - VersionParameters $parameters, + ElementParameters $elementParameters, + CollectionParameters $parameters, UserInterface $user ): Collection { $element = $this->getElement( $this->serviceResolver, - $parameters->getElementType(), - $parameters->getElementId(), + $elementParameters->getType(), + $elementParameters->getId(), ); $scheduledTasks = $this->getScheduledTasks($element); - $list = $this->repository->listVersions($element, $parameters, $user); + + $list = $this->repository->listVersions($element, $elementParameters->getType(), $parameters, $user); + $versions = []; $versionObjects = $list->load(); foreach ($versionObjects as $versionObject) { @@ -144,4 +150,12 @@ private function getScheduledTasks(ElementInterface $element): array return $schedules; } + + public function cleanupVersions( + ElementParameters $elementParameters, + VersionCleanupParameters $parameters + ): array + { + return $this->repository->cleanupVersions($elementParameters, $parameters); + } } diff --git a/src/Version/Service/VersionServiceInterface.php b/src/Version/Service/VersionServiceInterface.php index b73ba5aee..320c7f43d 100644 --- a/src/Version/Service/VersionServiceInterface.php +++ b/src/Version/Service/VersionServiceInterface.php @@ -16,7 +16,9 @@ namespace Pimcore\Bundle\StudioBackendBundle\Version\Service; -use Pimcore\Bundle\StudioBackendBundle\Version\Request\VersionParameters; +use Pimcore\Bundle\StudioBackendBundle\Request\CollectionParameters; +use Pimcore\Bundle\StudioBackendBundle\Request\ElementParameters; +use Pimcore\Bundle\StudioBackendBundle\Version\Request\VersionCleanupParameters; use Pimcore\Bundle\StudioBackendBundle\Version\Response\Collection; use Pimcore\Model\UserInterface; @@ -26,7 +28,8 @@ interface VersionServiceInterface { public function getVersions( - VersionParameters $parameters, + ElementParameters $elementParameters, + CollectionParameters $parameters, UserInterface $user ): Collection; @@ -34,4 +37,9 @@ public function publishVersion( int $versionId, UserInterface $user ): int; + + public function cleanupVersions( + ElementParameters $elementParameters, + VersionCleanupParameters $parameters + ): array; }