Skip to content

Commit

Permalink
feat: add versions endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
lukmzig committed May 2, 2024
1 parent 37c199f commit a31d309
Show file tree
Hide file tree
Showing 61 changed files with 2,804 additions and 23 deletions.
66 changes: 66 additions & 0 deletions config/versions.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
services:
_defaults:
autowire: true
autoconfigure: true
public: false

# controllers are imported separately to make sure they're public
# and have a tag that allows actions to type-hint services
Pimcore\Bundle\StudioBackendBundle\Version\Controller\:
resource: '../src/Version/Controller'
public: true
tags: [ 'controller.service_arguments' ]

Pimcore\Bundle\StudioBackendBundle\Version\RepositoryInterface:
class: Pimcore\Bundle\StudioBackendBundle\Version\Repository

Pimcore\Bundle\StudioBackendBundle\Version\Hydrator\VersionHydratorInterface:
class: Pimcore\Bundle\StudioBackendBundle\Version\Hydrator\VersionHydrator

# Hydrators
Pimcore\Bundle\StudioBackendBundle\Version\Hydrator\AssetVersionHydratorInterface:
class: Pimcore\Bundle\StudioBackendBundle\Version\Hydrator\AssetVersionHydrator

Pimcore\Bundle\StudioBackendBundle\Version\Hydrator\DataObjectVersionHydratorInterface:
class: Pimcore\Bundle\StudioBackendBundle\Version\Hydrator\DataObjectVersionHydrator

Pimcore\Bundle\StudioBackendBundle\Version\Hydrator\DocumentVersionHydratorInterface:
class: Pimcore\Bundle\StudioBackendBundle\Version\Hydrator\DocumentVersionHydrator

# Publishers
Pimcore\Bundle\StudioBackendBundle\Version\Publisher\AssetVersionPublisherInterface:
class: Pimcore\Bundle\StudioBackendBundle\Version\Publisher\AssetVersionPublisher

Pimcore\Bundle\StudioBackendBundle\Version\Publisher\DataObjectVersionPublisherInterface:
class: Pimcore\Bundle\StudioBackendBundle\Version\Publisher\DataObjectVersionPublisher

Pimcore\Bundle\StudioBackendBundle\Version\Publisher\DocumentVersionPublisherInterface:
class: Pimcore\Bundle\StudioBackendBundle\Version\Publisher\DocumentVersionPublisher

# Services
Pimcore\Bundle\StudioBackendBundle\Version\Hydrator\VersionHydratorServiceInterface:
class: Pimcore\Bundle\StudioBackendBundle\Version\Hydrator\VersionHydratorService
arguments:
$versionHydratorLocator: '@listing.version_hydrator.service_locator'

Pimcore\Bundle\StudioBackendBundle\Version\Publisher\VersionPublisherServiceInterface:
class: Pimcore\Bundle\StudioBackendBundle\Version\Publisher\VersionPublisherService
arguments:
$versionPublisherLocator: '@version.element_publisher.service_locator'

#Service Locator
listing.version_hydrator.service_locator:
class: Symfony\Component\DependencyInjection\ServiceLocator
tags: [ 'container.service_locator' ]
arguments:
- Pimcore\Model\Asset: '@Pimcore\Bundle\StudioBackendBundle\Version\Hydrator\AssetVersionHydratorInterface'
Pimcore\Model\DataObject: '@Pimcore\Bundle\StudioBackendBundle\Version\Hydrator\DataObjectVersionHydratorInterface'
Pimcore\Model\Document: '@Pimcore\Bundle\StudioBackendBundle\Version\Hydrator\DocumentVersionHydratorInterface'

version.element_publisher.service_locator:
class: Symfony\Component\DependencyInjection\ServiceLocator
tags: [ 'container.service_locator' ]
arguments:
- Pimcore\Model\Asset: '@Pimcore\Bundle\StudioBackendBundle\Version\Publisher\AssetVersionPublisherInterface'
Pimcore\Model\DataObject: '@Pimcore\Bundle\StudioBackendBundle\Version\Publisher\DataObjectVersionPublisherInterface'
Pimcore\Model\Document: '@Pimcore\Bundle\StudioBackendBundle\Version\Publisher\DocumentVersionPublisherInterface'
3 changes: 2 additions & 1 deletion src/Asset/Controller/CollectionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\Property\AnyOfAsset;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\SuccessResponse;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Config\Tags;
use Pimcore\Bundle\StudioBackendBundle\Util\Constants\ElementTypes;
use Pimcore\Bundle\StudioBackendBundle\Util\Traits\PaginatedResponseTrait;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\Attribute\MapQueryString;
Expand Down Expand Up @@ -97,7 +98,7 @@ public function getAssets(#[MapQueryString] ElementParameters $parameters): Json

$assetQuery = $filterService->applyFilters(
$parameters,
OpenSearchFilterInterface::TYPE_ASSET
ElementTypes::TYPE_ASSET
);

$result = $this->assetSearchService->searchAssets($assetQuery);
Expand Down
19 changes: 9 additions & 10 deletions src/DataIndex/Adapter/DataObjectSearchAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,19 @@

namespace Pimcore\Bundle\StudioBackendBundle\DataIndex\Adapter;

use Exception;
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\DataObject\DataObjectSearchInterface;
use Pimcore\Bundle\GenericDataIndexBundle\Service\Search\SearchService\DataObject\DataObjectSearchServiceInterface;
use Pimcore\Bundle\StaticResolverBundle\Models\Element\ServiceResolver;
use Pimcore\Bundle\StudioBackendBundle\DataIndex\DataObjectSearchResult;
use Pimcore\Bundle\StudioBackendBundle\DataIndex\Query\QueryInterface;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Schema\DataObject;
use Pimcore\Bundle\StudioBackendBundle\Exception\ElementNotFoundException;
use Pimcore\Bundle\StudioBackendBundle\Exception\InvalidSearchException;
use Pimcore\Model\DataObject\Concrete;

final readonly class DataObjectSearchAdapter implements DataObjectSearchAdapterInterface
{
public function __construct(
private DataObjectSearchServiceInterface $searchService,
private ServiceResolver $serviceResolver
private DataObjectSearchServiceInterface $searchService
) {
}

Expand All @@ -53,9 +51,9 @@ public function searchDataObjects(QueryInterface $dataObjectQuery): DataObjectSe
}
$searchResult = $this->searchService->search($search);

$result = array_map(function ($id) {
return $this->getDataObjectById($id);
}, $searchResult->getIds());
$result = array_map(static function ($item) {
return new DataObject($item->getId(), $item->getClassName());
}, $searchResult->getItems());

return new DataObjectSearchResult(
$result,
Expand All @@ -65,11 +63,12 @@ public function searchDataObjects(QueryInterface $dataObjectQuery): DataObjectSe
);
}

/**
* @throws Exception
*/
public function getDataObjectById(int $id): DataObject
{
/** @var null|Concrete $dataObject */
$dataObject = $this->serviceResolver->getElementById('object', $id);

$dataObject = $this->searchService->byId($id);
if (!$dataObject) {
throw new ElementNotFoundException($id);
}
Expand Down
7 changes: 4 additions & 3 deletions src/DataIndex/OpenSearchFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Pimcore\Bundle\StudioBackendBundle\Factory\QueryFactoryInterface;
use Pimcore\Bundle\StudioBackendBundle\Filter\Service\FilterServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Request\CollectionParametersInterface;
use Pimcore\Bundle\StudioBackendBundle\Util\Constants\ElementTypes;

/**
* @internal
Expand Down Expand Up @@ -65,9 +66,9 @@ public function applyFilters(CollectionParametersInterface $parameters, string $
private function getTypeFilters(Filters $filters, string $type): array
{
return match($type) {
OpenSearchFilterInterface::TYPE_ASSET => $filters->getAssetFilters(),
OpenSearchFilterInterface::TYPE_DATA_OBJECT => $filters->getDataObjectFilters(),
OpenSearchFilterInterface::TYPE_DOCUMENT => $filters->getDocumentFilters(),
ElementTypes::TYPE_ASSET => $filters->getAssetFilters(),
ElementTypes::TYPE_DATA_OBJECT => $filters->getDataObjectFilters(),
ElementTypes::TYPE_DOCUMENT => $filters->getDocumentFilters(),
default => throw new InvalidFilterTypeException(400, "Unknown filter type: $type")
};
}
Expand Down
6 changes: 0 additions & 6 deletions src/DataIndex/OpenSearchFilterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,6 @@ interface OpenSearchFilterInterface
{
public const SERVICE_TYPE = 'open_search_filter';

public const TYPE_DATA_OBJECT = 'dataObject';

public const TYPE_ASSET = 'asset';

public const TYPE_DOCUMENT = 'document';

/**
* @throws InvalidQueryTypeException
*/
Expand Down
1 change: 1 addition & 0 deletions src/DependencyInjection/PimcoreStudioBackendExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public function load(array $configs, ContainerBuilder $container): void
$loader->load('security.yaml');
$loader->load('services.yaml');
$loader->load('translation.yaml');
$loader->load('versions.yaml');

$definition = $container->getDefinition(TokenServiceInterface::class);
$definition->setArgument('$tokenLifetime', $config['api_token']['lifetime']);
Expand Down
4 changes: 2 additions & 2 deletions src/Exception/AccessDeniedException.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
*/
final class AccessDeniedException extends AbstractApiException
{
public function __construct()
public function __construct(string $message = 'Bad credentials')
{
parent::__construct(401, 'Bad credentials');
parent::__construct(401, $message);
}
}
31 changes: 31 additions & 0 deletions src/Exception/ElementProcessingNotCompletedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?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\Exception;

/**
* @internal
*/
final class ElementProcessingNotCompletedException extends AbstractApiException
{
public function __construct(int $id)
{
parent::__construct(
202,
sprintf('Element with ID %d was not processed yet', $id)
);
}
}
32 changes: 32 additions & 0 deletions src/Exception/ElementPublishingFailedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?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\Exception;

/**
* @internal
*/
final class ElementPublishingFailedException extends AbstractApiException
{
public function __construct(int $id, ?string $error = null)
{
parent::__construct(500, sprintf(
'Failed to publish element with ID %s: %s',
$id,
$error ?? 'Unknown error'
));
}
}
31 changes: 31 additions & 0 deletions src/Exception/ElementUnsafeException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?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\Exception;

/**
* @internal
*/
final class ElementUnsafeException extends AbstractApiException
{
public function __construct(int $id)
{
parent::__construct(
200,
sprintf('Element with ID %d is not safe to preview', $id)
);
}
}
28 changes: 28 additions & 0 deletions src/Exception/InvalidElementTypeException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?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\StudioBackendBundle\Exception;

/**
* @internal
*/
final class InvalidElementTypeException extends AbstractApiException
{
public function __construct(string $type)
{
parent::__construct(400, sprintf(
'Invalid element type: %s',
$type
));
}
}
2 changes: 1 addition & 1 deletion src/OpenApi/Attributes/Parameters/Path/IdParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function __construct(string $type = 'element')
{
parent::__construct(
name: 'id',
description: 'Id of the ' . $type,
description: 'ID of the ' . $type,
in: 'path',
required: true,
schema: new Schema(type: 'integer', example: 83),
Expand Down
45 changes: 45 additions & 0 deletions src/OpenApi/Attributes/Parameters/Query/ElementTypeParameter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?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\OpenApi\Attributes\Parameters\Query;

use Attribute;
use OpenApi\Attributes\QueryParameter;
use OpenApi\Attributes\Schema;
use Pimcore\Bundle\StudioBackendBundle\Util\Constants\ElementTypes;

#[Attribute(Attribute::TARGET_METHOD)]
final class ElementTypeParameter extends QueryParameter
{
public function __construct()
{
parent::__construct(
name: 'elementType',
description: 'Filter elements by matching element type.',
in: 'query',
required: true,
schema: new Schema(
type: 'string',
enum: [
ElementTypes::TYPE_OBJECT,
ElementTypes::TYPE_ASSET,
ElementTypes::TYPE_DOCUMENT,
],
example: ElementTypes::TYPE_OBJECT,
),
);
}
}
Loading

0 comments on commit a31d309

Please sign in to comment.