Skip to content

Commit

Permalink
Add DataObject prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
mattamon committed Apr 12, 2024
1 parent 79f9852 commit 82ab961
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 18 deletions.
63 changes: 49 additions & 14 deletions src/Controller/Api/DataObjects/CollectionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,30 @@

namespace Pimcore\Bundle\StudioApiBundle\Controller\Api\DataObjects;

use OpenApi\Attributes\Get;
use OpenApi\Attributes\JsonContent;
use Pimcore\Bundle\StudioApiBundle\Attributes\Parameters\Query\ExcludeFoldersParameter;
use Pimcore\Bundle\StudioApiBundle\Attributes\Parameters\Query\IdSearchTermParameter;
use Pimcore\Bundle\StudioApiBundle\Attributes\Parameters\Query\PageParameter;
use Pimcore\Bundle\StudioApiBundle\Attributes\Parameters\Query\PageSizeParameter;
use Pimcore\Bundle\StudioApiBundle\Attributes\Parameters\Query\ParentIdParameter;
use Pimcore\Bundle\StudioApiBundle\Attributes\Parameters\Query\PathIncludeDescendantsParameter;
use Pimcore\Bundle\StudioApiBundle\Attributes\Parameters\Query\PathIncludeParentParameter;
use Pimcore\Bundle\StudioApiBundle\Attributes\Parameters\Query\PathParameter;
use Pimcore\Bundle\StudioApiBundle\Attributes\Response\SuccessResponse;
use Pimcore\Bundle\StudioApiBundle\Attributes\Response\UnauthorizedResponse;
use Pimcore\Bundle\StudioApiBundle\Controller\Api\AbstractApiController;
use Pimcore\Bundle\StudioApiBundle\Controller\Trait\PaginatedResponseTrait;
use Pimcore\Bundle\StudioApiBundle\Dto\Collection;
use Pimcore\Bundle\StudioApiBundle\Dto\DataObject;
use Pimcore\Bundle\StudioApiBundle\Exception\InvalidQueryTypeException;
use Pimcore\Bundle\StudioApiBundle\Service\DataObjectSearchServiceInterface;
use Pimcore\Bundle\StudioApiBundle\Service\Filter\FilterServiceInterface;
use Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\DataObjectQuery;
use Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\DataObjectQueryProviderInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\Attribute\MapQueryString;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;

use Symfony\Component\Serializer\SerializerInterface;

final class CollectionController extends AbstractApiController
Expand All @@ -34,21 +48,47 @@ final class CollectionController extends AbstractApiController

public function __construct(
SerializerInterface $serializer,
private readonly DataObjectQueryProviderInterface $dataObjectQueryProvider,
private readonly DataObjectSearchServiceInterface $dataObjectSearchService,
private readonly FilterServiceInterface $filterService
) {
parent::__construct($serializer);
}

/**
* @throws InvalidQueryTypeException
*/
#[Route('/data-objects', name: 'pimcore_studio_api_data_objects', methods: ['GET'])]
#[IsGranted(self::VOTER_STUDIO_API)]
public function getAssets(#[MapQueryString] Collection $collection): JsonResponse
//#[IsGranted(self::VOTER_STUDIO_API)]
#[GET(
path: self::API_PATH . '/data-objects',
description: 'Get paginated data objects',
summary: 'Get all DataObjects',
security: self::SECURITY_SCHEME,
tags: ['DataObjects'],
)]
#[PageParameter]
#[PageSizeParameter]
#[ParentIdParameter]
#[IdSearchTermParameter]
#[ExcludeFoldersParameter]
#[PathParameter]
#[PathIncludeParentParameter]
#[PathIncludeDescendantsParameter]
#[SuccessResponse(
description: 'Paginated data objects with total count as header param',
content: new JsonContent(ref: DataObject::class)
)]
#[UnauthorizedResponse]

/**
* @throws InvalidQueryTypeException
*/
public function getDataObjects(#[MapQueryString] Collection $collection): JsonResponse
{

$dataObjectQuery = $this->getDataQuery()
->setPage($collection->getPage())
->setPageSize($collection->getPageSize())
->setClassDefinitionId('EV');
/** @var DataObjectQuery $dataObjectQuery */
$dataObjectQuery = $this->filterService->applyCollectionFilter($collection, 'dataObject');

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

return $this->getPaginatedCollection(
Expand All @@ -57,9 +97,4 @@ public function getAssets(#[MapQueryString] Collection $collection): JsonRespons
$result->getTotalItems()
);
}

private function getDataQuery(): DataObjectQuery
{
return $this->dataObjectQueryProvider->createDataObjectQuery();
}
}
1 change: 0 additions & 1 deletion src/Dto/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ public function __construct(
private readonly bool $workflowWithPermissions,
#[Property(description: 'Full path', type: 'string', example: '/path/to/asset.jpg')]
private readonly string $fullPath,
#[Property(description: 'ID', type: 'integer', example: 83)]
int $id,
#[Property(description: 'Parent ID', type: 'integer', example: 1)]
int $parentId,
Expand Down
26 changes: 26 additions & 0 deletions src/Dto/DataObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Pimcore\Bundle\StudioApiBundle\Dto;

use OpenApi\Attributes\Property;
use OpenApi\Attributes\Schema;

#[Schema(
title: 'DataObject',
type: 'object'
)]
readonly class DataObject
{
public function __construct(
#[Property(description: 'ID', type: 'integer', example: 83)]
private int $id
)
{

}

public function getId(): int
{
return $this->id;
}
}
2 changes: 2 additions & 0 deletions src/Dto/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@
namespace Pimcore\Bundle\StudioApiBundle\Dto;

use ApiPlatform\Metadata\ApiProperty;
use OpenApi\Attributes\Property;
use Pimcore\Bundle\StudioApiBundle\Dto\Asset\Permissions;

class Element
{
public function __construct(
#[Property(description: 'ID', type: 'integer', example: 83)]
private readonly int $id,
private readonly int $parentId,
private readonly string $path,
Expand Down
7 changes: 6 additions & 1 deletion src/Factory/QueryFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@

use Pimcore\Bundle\StudioApiBundle\Exception\InvalidQueryTypeException;
use Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\AssetQueryProviderInterface;
use Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\DataObjectQueryProviderInterface;
use Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\QueryInterface;

final readonly class QueryFactory implements QueryFactoryInterface
{
public function __construct(private AssetQueryProviderInterface $assetQueryProvider)
public function __construct(
private AssetQueryProviderInterface $assetQueryProvider,
private DataObjectQueryProviderInterface $dataObjectQueryProvider
)
{

}
Expand All @@ -34,6 +38,7 @@ public function create(string $type): QueryInterface
{
return match($type) {
'asset' => $this->assetQueryProvider->createAssetQuery(),
'dataObject' => $this->dataObjectQueryProvider->createDataObjectQuery(),
default => throw new InvalidQueryTypeException("Unknown query type: $type")
};
}
Expand Down
2 changes: 1 addition & 1 deletion src/Service/GenericData/V1/DataObjectQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\FullTextSearch\ElementKeySearch;
use Pimcore\Bundle\StaticResolverBundle\Models\DataObject\ClassDefinitionResolverInterface;

final class DataObjectQuery
final class DataObjectQuery implements QueryInterface
{
public const DATA_OBJECT_QUERY_ID = 'data_object_query';

Expand Down
3 changes: 2 additions & 1 deletion src/Service/GenericData/V1/DataObjectSearchAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

use Pimcore\Bundle\GenericDataIndexBundle\Service\Search\SearchService\DataObject\DataObjectSearchServiceInterface;
use Pimcore\Bundle\StaticResolverBundle\Models\Element\ServiceResolver;
use Pimcore\Bundle\StudioApiBundle\Dto\DataObject;
use Pimcore\Bundle\StudioApiBundle\Service\DataObjectSearchResult;
use Pimcore\Bundle\StudioApiBundle\Service\GenericData\DataObjectSearchAdapterInterface;
use Pimcore\Model\Element\ElementInterface;
Expand All @@ -34,7 +35,7 @@ public function searchDataObjects(DataObjectQuery $dataObjectQuery): DataObjectS
{
$searchResult = $this->searchService->search($dataObjectQuery->getSearch());
$result = array_map(
fn (int $id) => $this->serviceResolver->getElementById('object', $id),
static fn (int $id) => new DataObject($id),
$searchResult->getIds()
);

Expand Down
19 changes: 19 additions & 0 deletions tests/Unit/Service/Factory/QueryFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@

use Codeception\Test\Unit;
use Exception;
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\DataObject\DataObjectSearch;
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Interfaces\SearchInterface;
use Pimcore\Bundle\StaticResolverBundle\Models\DataObject\ClassDefinitionResolverInterface;
use Pimcore\Bundle\StudioApiBundle\Exception\InvalidQueryTypeException;
use Pimcore\Bundle\StudioApiBundle\Factory\QueryFactory;
use Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\AssetQuery;
use Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\AssetQueryProviderInterface;
use Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\DataObjectQuery;
use Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\DataObjectQueryProviderInterface;

final class QueryFactoryTest extends Unit
{
Expand Down Expand Up @@ -60,4 +64,19 @@ private function mockAssetAdapterInterface(): AssetQueryProviderInterface
},
]);
}

/**
* @throws Exception
*/
private function mockDataObjectAdapterInterface(): DataObjectQueryProviderInterface
{
return $this->makeEmpty(DataObjectQueryProviderInterface::class, [
'createDataObjectQuery' => function () {
return new DataObjectQuery(
$this->makeEmpty(DataObjectSearch::class),
$this->makeEmpty(ClassDefinitionResolverInterface::class)
);
},
]);
}
}

0 comments on commit 82ab961

Please sign in to comment.