Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/0x-First-Prototype' into 5-Authe…
Browse files Browse the repository at this point in the history
…ntication-Authorization

# Conflicts:
#	config/services.yaml
  • Loading branch information
mattamon committed Mar 4, 2024
2 parents 7fee84f + 78c47ad commit 0ad438e
Show file tree
Hide file tree
Showing 16 changed files with 362 additions and 81 deletions.
2 changes: 2 additions & 0 deletions config/api_platform/resources/asset.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ resources:
filters:
- Pimcore\Bundle\StudioApiBundle\Filter\AssetParentIdFilter
- Pimcore\Bundle\StudioApiBundle\Filter\AssetIdSearchFilter
- Pimcore\Bundle\StudioApiBundle\Filter\AssetExcludeFolderFilter
- Pimcore\Bundle\StudioApiBundle\Filter\AssetPathFilter
paginationClientItemsPerPage: true
ApiPlatform\Metadata\Get:
normalizationContext:
Expand Down
9 changes: 9 additions & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ services:
Pimcore\Bundle\StudioApiBundle\Filter\AssetIdSearchFilter:
tags: [ 'api_platform.filter' ]

Pimcore\Bundle\StudioApiBundle\Filter\AssetExcludeFolderFilter:
tags: [ 'api_platform.filter' ]

Pimcore\Bundle\StudioApiBundle\Filter\AssetPathFilter:
tags: [ 'api_platform.filter' ]

# Hydrators
Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\Hydrator\Asset\ArchiveHydratorInterface:
class: Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\Hydrator\Asset\ArchiveHydrator
Expand Down Expand Up @@ -76,6 +82,9 @@ services:
Pimcore\Bundle\StudioApiBundle\Service\IconServiceInterface:
class: Pimcore\Bundle\StudioApiBundle\Service\IconService

Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\AssetQueryProviderInterface:
class: Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\AssetQueryProvider

Pimcore\Bundle\StudioApiBundle\Service\TokenServiceInterface:
class: Pimcore\Bundle\StudioApiBundle\Service\TokenService

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,32 @@
namespace Pimcore\Bundle\StudioApiBundle\Filter;

use ApiPlatform\Serializer\Filter\FilterInterface;

use Pimcore\Bundle\StudioApiBundle\Dto\Asset;
use Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\AssetQueryContextTrait;
use Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\AssetQueryProviderInterface;
use Symfony\Component\HttpFoundation\Request;

final class AssetFolderFilter implements FilterInterface
final class AssetExcludeFolderFilter implements FilterInterface
{
public const ASSET_FOLDER_FILTER_CONTEXT = 'asset_folder_filter';
use AssetQueryContextTrait;

private const FOLDER_FILTER_QUERY_PARAM = 'excludeFolders';

private const FOLDER_FILTER_QUERY_PARAM = 'filterFolders';
public function __construct(AssetQueryProviderInterface $assetQueryProvider)
{
$this->assetQueryProvider = $assetQueryProvider;
}

public function apply(Request $request, bool $normalization, array $attributes, array &$context): void
{
$parentId = $request->query->get(self::FOLDER_FILTER_QUERY_PARAM);
$excludeFolders = $request->query->get(self::FOLDER_FILTER_QUERY_PARAM);

if (!$parentId) {
if ($excludeFolders !== 'true') {
return;
}

$context[self::ASSET_FOLDER_FILTER_CONTEXT] = (bool)$parentId;
$assetQuery = $this->getAssetQuery($context)->excludeFolders();
$this->setAssetQuery($context, $assetQuery);
}

public function getDescription(string $resourceClass): array
Expand Down
17 changes: 12 additions & 5 deletions src/Filter/AssetIdSearchFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,22 @@
namespace Pimcore\Bundle\StudioApiBundle\Filter;

use ApiPlatform\Serializer\Filter\FilterInterface;

use Pimcore\Bundle\StudioApiBundle\Dto\Asset;
use Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\AssetQueryContextTrait;
use Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\AssetQueryProviderInterface;
use Symfony\Component\HttpFoundation\Request;

final class AssetIdSearchFilter implements FilterInterface
{
public const ASSET_ID_SEARCH_FILTER = 'asset_id_search_filter';
use AssetQueryContextTrait;

private const ID_SEARCH_FILTER_QUERY_PARAM = 'idSearchTerm';

public function __construct(AssetQueryProviderInterface $assetQueryProvider)
{
$this->assetQueryProvider = $assetQueryProvider;
}

public function apply(Request $request, bool $normalization, array $attributes, array &$context): void
{
$searchIdTerm = $request->query->get(self::ID_SEARCH_FILTER_QUERY_PARAM);
Expand All @@ -32,7 +38,8 @@ public function apply(Request $request, bool $normalization, array $attributes,
return;
}

$context[self::ASSET_ID_SEARCH_FILTER] = $searchIdTerm;
$assetQuery = $this->getAssetQuery($context)->setSearchTerm($searchIdTerm);
$this->setAssetQuery($context, $assetQuery);
}

public function getDescription(string $resourceClass): array
Expand All @@ -43,9 +50,9 @@ public function getDescription(string $resourceClass): array
'type' => 'string',
'required' => false,
'is_collection' => false,
'description' => 'Filters assets by matching ids. As a wildcard, you can use *.',
'description' => 'Filter assets by matching ids. As a wildcard * can be used',
'openapi' => [
'description' => 'Filters assets by matching ids. As a wildcard, you can use *.',
'description' => 'Filter assets by matching ids. As a wildcard * can be used',
],
],
];
Expand Down
17 changes: 12 additions & 5 deletions src/Filter/AssetParentIdFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,22 @@
namespace Pimcore\Bundle\StudioApiBundle\Filter;

use ApiPlatform\Serializer\Filter\FilterInterface;

use Pimcore\Bundle\StudioApiBundle\Dto\Asset;
use Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\AssetQueryContextTrait;
use Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\AssetQueryProviderInterface;
use Symfony\Component\HttpFoundation\Request;

final class AssetParentIdFilter implements FilterInterface
{
public const ASSET_PARENT_ID_FILTER_CONTEXT = 'asset_parent_id_filter';
use AssetQueryContextTrait;

private const PARENT_ID_QUERY_PARAM = 'parentId';

public function __construct(AssetQueryProviderInterface $assetQueryProvider)
{
$this->assetQueryProvider = $assetQueryProvider;
}

public function apply(Request $request, bool $normalization, array $attributes, array &$context): void
{
$parentId = $request->query->get(self::PARENT_ID_QUERY_PARAM);
Expand All @@ -32,7 +38,8 @@ public function apply(Request $request, bool $normalization, array $attributes,
return;
}

$context[self::ASSET_PARENT_ID_FILTER_CONTEXT] = (int)$parentId;
$assetQuery = $this->getAssetQuery($context)->filterParentId((int)$parentId);
$this->setAssetQuery($context, $assetQuery);
}

public function getDescription(string $resourceClass): array
Expand All @@ -43,9 +50,9 @@ public function getDescription(string $resourceClass): array
'type' => 'int',
'required' => false,
'is_collection' => false,
'description' => 'Filters assets by parent id.',
'description' => 'Filter assets by parent id.',
'openapi' => [
'description' => 'Filters assets by parent id.',
'description' => 'Filter assets by parent id.',
],
],
];
Expand Down
104 changes: 104 additions & 0 deletions src/Filter/AssetPathFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under following license:
* - Pimcore Commercial License (PCL)
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license PCL
*/

namespace Pimcore\Bundle\StudioApiBundle\Filter;

use ApiPlatform\Serializer\Filter\FilterInterface;
use Pimcore\Bundle\StudioApiBundle\Dto\Asset;
use Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\AssetQueryContextTrait;
use Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\AssetQueryProviderInterface;
use Symfony\Component\HttpFoundation\Request;

final class AssetPathFilter implements FilterInterface
{
use AssetQueryContextTrait;

private const ASSET_PATH_QUERY_PARAM = 'assetPath';

private const ASSET_PATH_INCLUDE_PARENT_PARAM = 'assetPathIncludeParent';

private const ASSET_PATH_INCLUDE_DESCENDANTS_PARAM = 'assetPathIncludeDescendants';

Check notice on line 30 in src/Filter/AssetPathFilter.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Constant name is not following coding convention

Constant name `ASSET_PATH_INCLUDE_DESCENDANTS_PARAM` is too long (36 \> 32)

public function __construct(AssetQueryProviderInterface $assetQueryProvider)
{
$this->assetQueryProvider = $assetQueryProvider;
}

public function apply(Request $request, bool $normalization, array $attributes, array &$context): void
{
$path = $request->query->get(self::ASSET_PATH_QUERY_PARAM);

if (!$path) {
return;
}

$includeDescendants = $this->getBooleanValueFromQuery(
$request,
self::ASSET_PATH_INCLUDE_DESCENDANTS_PARAM,
false
);

$includeParent = $this->getBooleanValueFromQuery(
$request,
self::ASSET_PATH_INCLUDE_PARENT_PARAM,
false
);

$assetQuery = $this->getAssetQuery($context)->filterPath($path, $includeDescendants, $includeParent);
$this->setAssetQuery($context, $assetQuery);
}

public function getDescription(string $resourceClass): array
{
return [
self::ASSET_PATH_QUERY_PARAM => [
'property' => Asset::class,
'type' => 'string',
'required' => false,
'is_collection' => false,
'description' => 'Filter assets by path.',
'openapi' => [
'description' => 'Filter assets by path.',
],
],
self::ASSET_PATH_INCLUDE_PARENT_PARAM => [
'property' => Asset::class,
'type' => 'bool',
'required' => false,
'is_collection' => false,
'description' => 'Include the parent item in the result.',
'openapi' => [
'description' => 'Include the parent item in the result.',
],
],
self::ASSET_PATH_INCLUDE_DESCENDANTS_PARAM => [
'property' => Asset::class,
'type' => 'bool',
'required' => false,
'is_collection' => false,
'description' => 'Include all descendants in the result.',
'openapi' => [
'description' => 'Include all descendants in the result.',
],
],
];
}

private function getBooleanValueFromQuery(Request $request, string $queryName, bool $defaultValue): bool
{
return filter_var(
$request->query->get($queryName, $defaultValue),
FILTER_VALIDATE_BOOLEAN
);
}
}
10 changes: 9 additions & 1 deletion src/Service/AssetSearchResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@
namespace Pimcore\Bundle\StudioApiBundle\Service;

use Pimcore\Bundle\StudioApiBundle\Dto\Asset;
use Pimcore\Bundle\StudioApiBundle\Dto\Asset\Archive;
use Pimcore\Bundle\StudioApiBundle\Dto\Asset\Audio;
use Pimcore\Bundle\StudioApiBundle\Dto\Asset\Document;
use Pimcore\Bundle\StudioApiBundle\Dto\Asset\Folder;
use Pimcore\Bundle\StudioApiBundle\Dto\Asset\Image;
use Pimcore\Bundle\StudioApiBundle\Dto\Asset\Text;
use Pimcore\Bundle\StudioApiBundle\Dto\Asset\Unknown;
use Pimcore\Bundle\StudioApiBundle\Dto\Asset\Video;

final readonly class AssetSearchResult
{
Expand All @@ -37,7 +45,7 @@ public function getTotalItems(): int
}

/**
* @return array<int, Asset>
* @return array<int, Asset|Archive|Audio|Document|Folder|Image|Text|Unknown|Video>
*/
public function getItems(): array
{
Expand Down
21 changes: 13 additions & 8 deletions src/Service/AssetSearchService.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,29 @@
namespace Pimcore\Bundle\StudioApiBundle\Service;

use Pimcore\Bundle\StudioApiBundle\Dto\Asset;
use Pimcore\Bundle\StudioApiBundle\Dto\Asset\Archive;
use Pimcore\Bundle\StudioApiBundle\Dto\Asset\Audio;
use Pimcore\Bundle\StudioApiBundle\Dto\Asset\Document;
use Pimcore\Bundle\StudioApiBundle\Dto\Asset\Folder;
use Pimcore\Bundle\StudioApiBundle\Dto\Asset\Image;
use Pimcore\Bundle\StudioApiBundle\Dto\Asset\Text;
use Pimcore\Bundle\StudioApiBundle\Dto\Asset\Unknown;
use Pimcore\Bundle\StudioApiBundle\Dto\Asset\Video;
use Pimcore\Bundle\StudioApiBundle\Service\GenericData\AssetSearchAdapterInterface;
use Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\AssetQuery;

final readonly class AssetSearchService implements AssetSearchServiceInterface
{
public function __construct(private AssetSearchAdapterInterface $assetSearchAdapter)
{
}

public function searchAssets(
int $page = 1,
int $pageSize = 50,
?string $query = null,
?int $parentId = null
): AssetSearchResult {
return $this->assetSearchAdapter->searchAsset($page, $pageSize, $query, $parentId);
public function searchAssets(AssetQuery $assetQuery): AssetSearchResult
{
return $this->assetSearchAdapter->searchAssets($assetQuery);
}

public function getAssetById(int $id): ?Asset
public function getAssetById(int $id): Asset|Archive|Audio|Document|Folder|Image|Text|Unknown|Video|null
{
return $this->assetSearchAdapter->getAssetById($id);
}
Expand Down
18 changes: 11 additions & 7 deletions src/Service/AssetSearchServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,19 @@
namespace Pimcore\Bundle\StudioApiBundle\Service;

use Pimcore\Bundle\StudioApiBundle\Dto\Asset;
use Pimcore\Bundle\StudioApiBundle\Dto\Asset\Archive;
use Pimcore\Bundle\StudioApiBundle\Dto\Asset\Audio;
use Pimcore\Bundle\StudioApiBundle\Dto\Asset\Document;
use Pimcore\Bundle\StudioApiBundle\Dto\Asset\Folder;
use Pimcore\Bundle\StudioApiBundle\Dto\Asset\Image;
use Pimcore\Bundle\StudioApiBundle\Dto\Asset\Text;
use Pimcore\Bundle\StudioApiBundle\Dto\Asset\Unknown;
use Pimcore\Bundle\StudioApiBundle\Dto\Asset\Video;
use Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\AssetQuery;

interface AssetSearchServiceInterface
{
public function searchAssets(
int $page = 1,
int $pageSize = 50,
?string $query = null,
?int $parentId = null
): AssetSearchResult;
public function searchAssets(AssetQuery $assetQuery): AssetSearchResult;

public function getAssetById(int $id): ?Asset;
public function getAssetById(int $id): Asset|Archive|Audio|Document|Folder|Image|Text|Unknown|Video|null;
}
16 changes: 2 additions & 14 deletions src/Service/GenericData/AssetSearchAdapterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,11 @@

use Pimcore\Bundle\StudioApiBundle\Dto\Asset;
use Pimcore\Bundle\StudioApiBundle\Service\AssetSearchResult;
use Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\AssetQuery;

interface AssetSearchAdapterInterface
{
/**
* @param int $page
* @param int $pageSize
* @param string|null $searchTerm
* @param int|null $parentId
*
* @return AssetSearchResult
*/
public function searchAsset(
int $page,
int $pageSize,
?string $searchTerm,
?int $parentId = null
): AssetSearchResult;
public function searchAssets(AssetQuery $assetQuery): AssetSearchResult;

public function getAssetById(int $id): ?Asset;
}
Loading

0 comments on commit 0ad438e

Please sign in to comment.