Skip to content

Commit

Permalink
Change to path parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
mattamon committed Jun 3, 2024
1 parent a83bbef commit 055abf9
Show file tree
Hide file tree
Showing 13 changed files with 170 additions and 135 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,23 @@
* @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;
use Pimcore\Bundle\StudioBackendBundle\Dependency\Attributes\Parameters\Query\DependencyModeParameter;
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;
Expand All @@ -48,15 +49,15 @@ 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
Expand All @@ -65,11 +66,11 @@ public function __construct(
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())
Expand All @@ -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()
);
Expand Down
18 changes: 1 addition & 17 deletions src/Dependency/Request/DependencyParameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);

Expand Down
24 changes: 14 additions & 10 deletions src/Dependency/Service/DependencyService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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),
};
}

Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -101,8 +105,8 @@ private function getRequiredByDependencies(
$parameters->getPage(),
$parameters->getPageSize(),
$this->dependencyRepository->listRequiredByDependenciesTotalCount(
$parameters->getElementType(),
$parameters->getElementId()
$elementParameters->getType(),
$elementParameters->getId()
)
);
}
Expand Down
2 changes: 2 additions & 0 deletions src/Dependency/Service/DependencyServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
61 changes: 61 additions & 0 deletions src/Request/ElementParameters.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioBackendBundle\Request;

use Pimcore\Bundle\StudioBackendBundle\Exception\InvalidElementTypeException;
use Pimcore\Bundle\StudioBackendBundle\Util\Constants\ElementTypes;
use Pimcore\ValueObject\Integer\PositiveInteger;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Positive;

/**
* @internal
*/
final readonly class ElementParameters
{
public function __construct(
#[NotBlank]
private string $type,
#[NotBlank]
#[Positive]
private int $id,
) {
$this->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;
}


private function validate(): void
{
if (!in_array($this->type, ElementTypes::ALLOWED_TYPES)){
throw new InvalidElementTypeException($this->type);
}
new PositiveInteger($this->id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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',
Expand All @@ -66,8 +68,13 @@ 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)]
);
}
}
Loading

0 comments on commit 055abf9

Please sign in to comment.