Skip to content

Commit

Permalink
[Improvement] Add filters based on type (#28)
Browse files Browse the repository at this point in the history
* Add filters based on type

* Apply php-cs-fixer changes

* Add default for invalid filter type

* Apply php-cs-fixer changes

* Add types to interface

* Apply php-cs-fixer changes

* Make classes internal

* Apply php-cs-fixer changes

* Move Refresh to request

* Remove unecessary test

* Add internal

---------

Co-authored-by: mattamon <[email protected]>
  • Loading branch information
mattamon and mattamon authored Apr 17, 2024
1 parent 6a3cfc8 commit 4c75aab
Show file tree
Hide file tree
Showing 58 changed files with 320 additions and 83 deletions.
2 changes: 1 addition & 1 deletion config/filters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ services:

# DataObject
Pimcore\Bundle\StudioApiBundle\Filter\DataObject\ClassIdFilter:
tags: [ 'pimcore.studio_api.collection.filter' ]
tags: [ 'pimcore.studio_api.collection.data_object.filter' ]
2 changes: 1 addition & 1 deletion src/Attributes/Request/TokenRequestBody.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
use Attribute;
use OpenApi\Attributes\JsonContent;
use OpenApi\Attributes\RequestBody;
use Pimcore\Bundle\StudioApiBundle\Dto\Token\Refresh;
use Pimcore\Bundle\StudioApiBundle\Request\Query\Refresh;

#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)]
final class TokenRequestBody extends RequestBody
Expand Down
2 changes: 1 addition & 1 deletion src/Attributes/Request/TranslationRequestBody.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
use Attribute;
use OpenApi\Attributes\JsonContent;
use OpenApi\Attributes\RequestBody;
use Pimcore\Bundle\StudioApiBundle\Dto\Translation;
use Pimcore\Bundle\StudioApiBundle\Request\Query\Translation;

#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)]
final class TranslationRequestBody extends RequestBody
Expand Down
2 changes: 1 addition & 1 deletion src/Attributes/Response/UnauthorizedResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
use Attribute;
use OpenApi\Attributes\JsonContent;
use OpenApi\Attributes\Response;
use Pimcore\Bundle\StudioApiBundle\Dto\Unauthorized;
use Pimcore\Bundle\StudioApiBundle\Response\Schema\Unauthorized;

#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)]
final class UnauthorizedResponse extends Response
Expand Down
4 changes: 4 additions & 0 deletions src/Config/OpenApi/Security.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@

use OpenApi\Attributes\SecurityScheme;

/**
* @internal
* This class exists to document the public api
*/
#[SecurityScheme(
securityScheme: 'auth_token',
type: 'http',
Expand Down
4 changes: 4 additions & 0 deletions src/Config/OpenApi/Spec.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
use OpenApi\Attributes\Info;
use OpenApi\Attributes\License;

/**
* @internal
* This class exists to document the public api
*/
#[Info(
version: '0.0.1',
description: 'API for Studio generated by OpenApi Generator via zircote/swagger-php',
Expand Down
3 changes: 3 additions & 0 deletions src/Config/Tags.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

use OpenApi\Attributes\Tag;

/**
* @internal
*/
#[Tag(name: Tags::Assets->name, description: 'Asset operations to get/update/create/delete assets')]
#[Tag(name: Tags::Authorization->name, description: 'Login via username and password to get a token or refresh the token')]
#[Tag(name: Tags::DataObjects->name, description: 'DataObject operations to get/update/create/delete data objects')]
Expand Down
3 changes: 3 additions & 0 deletions src/Controller/Api/AbstractApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Serializer\SerializerInterface;

/**
* @internal
*/
#[Route('/studio/api')]
abstract class AbstractApiController extends AbstractController
{
Expand Down
10 changes: 8 additions & 2 deletions src/Controller/Api/Assets/CollectionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
use Pimcore\Bundle\StudioApiBundle\Controller\Api\AbstractApiController;
use Pimcore\Bundle\StudioApiBundle\Controller\Trait\PaginatedResponseTrait;
use Pimcore\Bundle\StudioApiBundle\Dto\Asset;
use Pimcore\Bundle\StudioApiBundle\Dto\Filter\Parameters;
use Pimcore\Bundle\StudioApiBundle\Exception\InvalidQueryTypeException;
use Pimcore\Bundle\StudioApiBundle\Request\Query\Filter\Parameters;
use Pimcore\Bundle\StudioApiBundle\Service\AssetSearchServiceInterface;
use Pimcore\Bundle\StudioApiBundle\Service\Filter\FilterServiceInterface;
use Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\AssetQuery;
Expand All @@ -43,6 +43,9 @@
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Serializer\SerializerInterface;

/**
* @internal
*/
final class CollectionController extends AbstractApiController
{
use PaginatedResponseTrait;
Expand Down Expand Up @@ -84,7 +87,10 @@ public function __construct(
public function getAssets(#[MapQueryString] Parameters $parameters): JsonResponse
{
/** @var AssetQuery $assetQuery */
$assetQuery = $this->filterService->applyCollectionFilter($parameters, 'asset');
$assetQuery = $this->filterService->applyFilters(
$parameters,
FilterServiceInterface::TYPE_ASSET
);

$result = $this->assetSearchService->searchAssets($assetQuery);

Expand Down
3 changes: 3 additions & 0 deletions src/Controller/Api/Assets/GetController.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Serializer\SerializerInterface;

/**
* @internal
*/
final class GetController extends AbstractApiController
{
public function __construct(
Expand Down
7 changes: 5 additions & 2 deletions src/Controller/Api/Authorization/AuthorizationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
use Pimcore\Bundle\StudioApiBundle\Config\Tags;
use Pimcore\Bundle\StudioApiBundle\Controller\Api\AbstractApiController;
use Pimcore\Bundle\StudioApiBundle\Dto\Credentials;
use Pimcore\Bundle\StudioApiBundle\Dto\Token;
use Pimcore\Bundle\StudioApiBundle\Dto\Token\Refresh;
use Pimcore\Bundle\StudioApiBundle\Request\Query\Refresh;
use Pimcore\Bundle\StudioApiBundle\Response\Schema\Token;
use Pimcore\Bundle\StudioApiBundle\Service\SecurityServiceInterface;
use Pimcore\Bundle\StudioApiBundle\Service\TokenServiceInterface;
use Pimcore\Security\User\User;
Expand All @@ -35,6 +35,9 @@
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Serializer\SerializerInterface;

/**
* @internal
*/
final class AuthorizationController extends AbstractApiController
{
public function __construct(
Expand Down
4 changes: 2 additions & 2 deletions src/Controller/Api/DataObjects/CollectionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
use Pimcore\Bundle\StudioApiBundle\Controller\Api\AbstractApiController;
use Pimcore\Bundle\StudioApiBundle\Controller\Trait\PaginatedResponseTrait;
use Pimcore\Bundle\StudioApiBundle\Dto\DataObject;
use Pimcore\Bundle\StudioApiBundle\Dto\Filter\DataObjectParameters;
use Pimcore\Bundle\StudioApiBundle\Exception\InvalidQueryTypeException;
use Pimcore\Bundle\StudioApiBundle\Request\Query\Filter\DataObjectParameters;
use Pimcore\Bundle\StudioApiBundle\Service\DataObjectSearchServiceInterface;
use Pimcore\Bundle\StudioApiBundle\Service\Filter\FilterServiceInterface;
use Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\DataObjectQuery;
Expand Down Expand Up @@ -87,7 +87,7 @@ public function getDataObjects(#[MapQueryString] DataObjectParameters $parameter
{

/** @var DataObjectQuery $dataObjectQuery */
$dataObjectQuery = $this->filterService->applyCollectionFilter($parameters, 'dataObject');
$dataObjectQuery = $this->filterService->applyFilters($parameters, 'dataObject');

$result = $this->dataObjectSearchService->searchDataObjects($dataObjectQuery);

Expand Down
3 changes: 3 additions & 0 deletions src/Controller/Api/OpenApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;

/**
* @internal
*/
final class OpenApiController extends AbstractController
{
public function __construct(private readonly OpenApiServiceInterface $openApiService)
Expand Down
5 changes: 4 additions & 1 deletion src/Controller/Api/TranslationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,16 @@
use Pimcore\Bundle\StudioApiBundle\Attributes\Response\SuccessResponse;
use Pimcore\Bundle\StudioApiBundle\Attributes\Response\UnauthorizedResponse;
use Pimcore\Bundle\StudioApiBundle\Config\Tags;
use Pimcore\Bundle\StudioApiBundle\Dto\Translation;
use Pimcore\Bundle\StudioApiBundle\Request\Query\Translation;
use Pimcore\Bundle\StudioApiBundle\Service\TranslatorServiceInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\Attribute\MapRequestPayload;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Serializer\SerializerInterface;

/**
* @internal
*/
final class TranslationController extends AbstractApiController
{
private const PATH = '/translations';
Expand Down
3 changes: 3 additions & 0 deletions src/Controller/Trait/PaginatedResponseTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Serializer\SerializerInterface;

/**
* @internal
*/
trait PaginatedResponseTrait
{
private const HEADER_TOTAL_ITEMS = 'X-Pimcore-Total-Items';
Expand Down
10 changes: 7 additions & 3 deletions src/DependencyInjection/CompilerPass/FilterPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,13 @@ final class FilterPass implements CompilerPassInterface
public function process(ContainerBuilder $container): void
{
$taggedServices = array_keys(
$container->findTaggedServiceIds(
TaggedIteratorAdapter::FILTER_TAG
)
[
... $container->findTaggedServiceIds(TaggedIteratorAdapter::FILTER_TAG),
... $container->findTaggedServiceIds(TaggedIteratorAdapter::FILTER_ASSET_TAG),
... $container->findTaggedServiceIds(TaggedIteratorAdapter::FILTER_DATA_OBJECT_TAG),
... $container->findTaggedServiceIds(TaggedIteratorAdapter::FILTER_DOCUMENT_TAG),

]
);

foreach ($taggedServices as $environmentType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,13 @@
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioApiBundle\Tests\Unit\Dto\Token;
namespace Pimcore\Bundle\StudioApiBundle\Exception;

use Codeception\Test\Unit;
use Pimcore\Bundle\StudioApiBundle\Dto\Token;
use Symfony\Component\HttpKernel\Exception\HttpException;

final class OutputTest extends Unit
/**
* @internal
*/
final class AccessDeniedException extends HttpException
{
public function testTokenOutput(): void
{
$output = new Token('token', 3600, 'test');
$this->assertSame('token', $output->getToken());
$this->assertSame(3600, $output->getLifetime());
$this->assertSame('test', $output->getUsername());
}
}
26 changes: 26 additions & 0 deletions src/Exception/InvalidFilterTypeException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?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\StudioApiBundle\Exception;

use Exception;

/**
* @internal
*/
final class InvalidFilterTypeException extends Exception
{
}
3 changes: 3 additions & 0 deletions src/Exception/InvalidPathException.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

use Exception;

/**
* @internal
*/
final class InvalidPathException extends Exception
{
}
3 changes: 3 additions & 0 deletions src/Exception/InvalidQueryTypeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

use Exception;

/**
* @internal
*/
final class InvalidQueryTypeException extends Exception
{
}
3 changes: 3 additions & 0 deletions src/Exception/NoRequestException.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

use Symfony\Component\HttpKernel\Exception\HttpException;

/**
* @internal
*/
final class NoRequestException extends HttpException
{
}
3 changes: 3 additions & 0 deletions src/Exception/NonPublicTranslationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

use Symfony\Component\HttpKernel\Exception\HttpException;

/**
* @internal
*/
final class NonPublicTranslationException extends HttpException
{
}
3 changes: 3 additions & 0 deletions src/Exception/NotAuthorizedException.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

use Symfony\Component\HttpKernel\Exception\HttpException;

/**
* @internal
*/
final class NotAuthorizedException extends HttpException
{
}
3 changes: 3 additions & 0 deletions src/Factory/QueryFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
use Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\DataObjectQueryProviderInterface;
use Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\QueryInterface;

/**
* @internal
*/
final readonly class QueryFactory implements QueryFactoryInterface
{
public function __construct(
Expand Down
3 changes: 3 additions & 0 deletions src/Factory/QueryFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
use Pimcore\Bundle\StudioApiBundle\Exception\InvalidQueryTypeException;
use Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\QueryInterface;

/**
* @internal
*/
interface QueryFactoryInterface
{
/**
Expand Down
7 changes: 5 additions & 2 deletions src/Filter/DataObject/ClassIdFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@

namespace Pimcore\Bundle\StudioApiBundle\Filter\DataObject;

use Pimcore\Bundle\StudioApiBundle\Dto\Filter\DataObjectParametersInterface;
use Pimcore\Bundle\StudioApiBundle\Dto\Filter\ParametersInterface;
use Pimcore\Bundle\StudioApiBundle\Filter\FilterInterface;
use Pimcore\Bundle\StudioApiBundle\Request\Query\Filter\DataObjectParametersInterface;
use Pimcore\Bundle\StudioApiBundle\Request\Query\Filter\ParametersInterface;
use Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\DataObjectQuery;
use Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\QueryInterface;

/**
* @internal
*/
final class ClassIdFilter implements FilterInterface
{
public function apply(ParametersInterface $parameters, QueryInterface $query): QueryInterface
Expand Down
5 changes: 4 additions & 1 deletion src/Filter/ExcludeFolderFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@

namespace Pimcore\Bundle\StudioApiBundle\Filter;

use Pimcore\Bundle\StudioApiBundle\Dto\Filter\ParametersInterface;
use Pimcore\Bundle\StudioApiBundle\Request\Query\Filter\ParametersInterface;
use Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\QueryInterface;

/**
* @internal
*/
final class ExcludeFolderFilter implements FilterInterface
{
public function apply(ParametersInterface $parameters, QueryInterface $query): QueryInterface
Expand Down
5 changes: 4 additions & 1 deletion src/Filter/FilterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@

namespace Pimcore\Bundle\StudioApiBundle\Filter;

use Pimcore\Bundle\StudioApiBundle\Dto\Filter\ParametersInterface;
use Pimcore\Bundle\StudioApiBundle\Request\Query\Filter\ParametersInterface;
use Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\QueryInterface;

/**
* @internal
*/
interface FilterInterface
{
public function apply(ParametersInterface $parameters, QueryInterface $query): QueryInterface;
Expand Down
5 changes: 4 additions & 1 deletion src/Filter/IdSearchFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@

namespace Pimcore\Bundle\StudioApiBundle\Filter;

use Pimcore\Bundle\StudioApiBundle\Dto\Filter\ParametersInterface;
use Pimcore\Bundle\StudioApiBundle\Request\Query\Filter\ParametersInterface;
use Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\QueryInterface;

/**
* @internal
*/
final class IdSearchFilter implements FilterInterface
{
public function apply(ParametersInterface $parameters, QueryInterface $query): QueryInterface
Expand Down
Loading

0 comments on commit 4c75aab

Please sign in to comment.