Skip to content

Commit

Permalink
Switch to path parameters for element (#83)
Browse files Browse the repository at this point in the history
* Change to path parameters

* Consistency checks

* Remove positive integer

* Rename request to MappedParameter

* Remove unused class
  • Loading branch information
mattamon authored Jun 3, 2024
1 parent b4f673e commit 0384279
Show file tree
Hide file tree
Showing 20 changed files with 180 additions and 189 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,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())
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
2 changes: 1 addition & 1 deletion src/Property/Controller/CollectionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion src/Property/Repository/PropertyRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Property/Repository/PropertyRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
45 changes: 0 additions & 45 deletions src/Property/Request/UpdateElementProperties.php

This file was deleted.

3 changes: 1 addition & 2 deletions src/Property/Service/PropertyService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Property/Service/PropertyServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
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 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;
}

/**
* @throws InvalidElementTypeException
*/
private function validate(): void
{
if (!in_array($this->type, ElementTypes::ALLOWED_TYPES)) {
throw new InvalidElementTypeException($this->type);
}
}
}
Loading

0 comments on commit 0384279

Please sign in to comment.