Skip to content

Commit

Permalink
Apply blue print to assets (#76)
Browse files Browse the repository at this point in the history
* Introduce asset service

* Apply php-cs-fixer changes

* Consistency check

* Refactor controllers and add custom settings event

* Apply php-cs-fixer changes

* Add new lines

---------

Co-authored-by: mattamon <mattamon@users.noreply.github.com>
  • Loading branch information
mattamon and mattamon authored May 24, 2024
1 parent 06c1489 commit f4d2afb
Show file tree
Hide file tree
Showing 15 changed files with 399 additions and 47 deletions.
11 changes: 10 additions & 1 deletion config/assets.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,13 @@ services:

# Encoder
Pimcore\Bundle\StudioBackendBundle\Asset\Encoder\TextEncoderInterface:
class: Pimcore\Bundle\StudioBackendBundle\Asset\Encoder\TextEncoder
class: Pimcore\Bundle\StudioBackendBundle\Asset\Encoder\TextEncoder

Pimcore\Bundle\StudioBackendBundle\Asset\Service\AssetServiceInterface:
class: Pimcore\Bundle\StudioBackendBundle\Asset\Service\AssetService

Pimcore\Bundle\StudioBackendBundle\Asset\Service\CustomSettingsServiceInterface:
class: Pimcore\Bundle\StudioBackendBundle\Asset\Service\CustomSettingsService

Pimcore\Bundle\StudioBackendBundle\Asset\Service\DataServiceInterface:
class: Pimcore\Bundle\StudioBackendBundle\Asset\Service\DataService
17 changes: 5 additions & 12 deletions src/Asset/Controller/CollectionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

use OpenApi\Attributes\Get;
use Pimcore\Bundle\StudioBackendBundle\Asset\Attributes\Response\Property\AnyOfAsset;
use Pimcore\Bundle\StudioBackendBundle\Asset\Service\AssetServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Controller\AbstractApiController;
use Pimcore\Bundle\StudioBackendBundle\DataIndex\AssetSearchServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\DataIndex\OpenSearchFilterInterface;
Expand Down Expand Up @@ -53,8 +54,7 @@ final class CollectionController extends AbstractApiController

public function __construct(
SerializerInterface $serializer,
private readonly AssetSearchServiceInterface $assetSearchService,
private readonly FilterServiceProviderInterface $filterServiceProvider
private readonly AssetServiceInterface $assetService,
) {
parent::__construct($serializer);
}
Expand Down Expand Up @@ -90,19 +90,12 @@ public function __construct(
])]
public function getAssets(#[MapQueryString] ElementParameters $parameters): JsonResponse
{
$filterService = $this->filterServiceProvider->create(OpenSearchFilterInterface::SERVICE_TYPE);

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

$result = $this->assetSearchService->searchAssets($assetQuery);
$collection = $this->assetService->getAssets($parameters);

return $this->getPaginatedCollection(
$this->serializer,
$result->getItems(),
$result->getTotalItems()
$collection->getItems(),
$collection->getTotalItems()
);
}
}
23 changes: 3 additions & 20 deletions src/Asset/Controller/CustomSettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,15 @@
namespace Pimcore\Bundle\StudioBackendBundle\Asset\Controller;

use OpenApi\Attributes\Get;
use Pimcore\Bundle\StaticResolverBundle\Models\Element\ServiceResolverInterface;
use Pimcore\Bundle\StudioBackendBundle\Asset\Attributes\Response\Content\CustomSettingsJson;
use Pimcore\Bundle\StudioBackendBundle\Asset\Hydrator\CustomSettingsHydratorInterface;
use Pimcore\Bundle\StudioBackendBundle\Asset\Service\CustomSettingsServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Controller\AbstractApiController;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Parameters\Path\IdParameter;
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\Security\Service\SecurityServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Util\Constants\ElementPermissions;
use Pimcore\Bundle\StudioBackendBundle\Util\Constants\ElementTypes;
use Pimcore\Bundle\StudioBackendBundle\Util\Constants\HttpResponseCodes;
use Pimcore\Bundle\StudioBackendBundle\Util\Traits\ElementProviderTrait;
use Pimcore\Model\Asset;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Serializer\SerializerInterface;
Expand All @@ -44,9 +39,7 @@ final class CustomSettingsController extends AbstractApiController

public function __construct(
SerializerInterface $serializer,
private readonly CustomSettingsHydratorInterface $hydrator,
private readonly SecurityServiceInterface $securityService,
private readonly ServiceResolverInterface $serviceResolver,
private readonly CustomSettingsServiceInterface $customSettingsService
) {
parent::__construct($serializer);
}
Expand All @@ -72,16 +65,6 @@ public function __construct(
])]
public function getAssetCustomSettingsById(int $id): JsonResponse
{
/** @var Asset $asset */
$asset = $this->getElement($this->serviceResolver, ElementTypes::TYPE_ASSET, $id);
$this->securityService->hasElementPermission(
$asset,
$this->securityService->getCurrentUser(),
ElementPermissions::VIEW_PERMISSION
);

return $this->jsonResponse(
$this->hydrator->hydrate($asset->getCustomSettings())
);
return $this->jsonResponse($this->customSettingsService->getCustomSettings($id));
}
}
10 changes: 3 additions & 7 deletions src/Asset/Controller/Data/TextController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
namespace Pimcore\Bundle\StudioBackendBundle\Asset\Controller\Data;

use OpenApi\Attributes\Get;
use Pimcore\Bundle\StaticResolverBundle\Models\Element\ServiceResolverInterface;
use Pimcore\Bundle\StudioBackendBundle\Asset\Encoder\TextEncoderInterface;
use Pimcore\Bundle\StudioBackendBundle\Asset\Service\DataServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Controller\AbstractApiController;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Parameters\Path\IdParameter;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\Content\DataJson;
Expand All @@ -40,8 +39,7 @@ final class TextController extends AbstractApiController

public function __construct(
SerializerInterface $serializer,
private readonly ServiceResolverInterface $serviceResolver,
private readonly TextEncoderInterface $textEncoder,
private readonly DataServiceInterface $dataService

) {
parent::__construct($serializer);
Expand All @@ -68,8 +66,6 @@ public function __construct(
])]
public function getTextData(int $id): JsonResponse
{
$element = $this->getElement($this->serviceResolver, 'asset', $id);

return $this->jsonResponse(['data' => $this->textEncoder->encodeUTF8($element)]);
return $this->jsonResponse(['data' => $this->dataService->getUTF8EncodedData($id)]);
}
}
6 changes: 3 additions & 3 deletions src/Asset/Controller/GetController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

use OpenApi\Attributes\Get;
use Pimcore\Bundle\StudioBackendBundle\Asset\Attributes\Response\Content\OneOfAssetJson;
use Pimcore\Bundle\StudioBackendBundle\Asset\Service\AssetServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Controller\AbstractApiController;
use Pimcore\Bundle\StudioBackendBundle\DataIndex\AssetSearchServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Parameters\Path\IdParameter;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\DefaultResponses;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\SuccessResponse;
Expand All @@ -36,7 +36,7 @@ final class GetController extends AbstractApiController
{
public function __construct(
SerializerInterface $serializer,
private readonly AssetSearchServiceInterface $assetSearchService,
private readonly AssetServiceInterface $assetService,
) {
parent::__construct($serializer);
}
Expand All @@ -63,6 +63,6 @@ public function __construct(
])]
public function getAssetById(int $id): JsonResponse
{
return $this->jsonResponse($this->assetSearchService->getAssetById($id));
return $this->jsonResponse($this->assetService->getAsset($id));
}
}
39 changes: 39 additions & 0 deletions src/Asset/Event/AssetEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?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\Asset\Event;

use Pimcore\Bundle\StudioBackendBundle\Asset\Schema\Asset;
use Pimcore\Bundle\StudioBackendBundle\Event\AbstractPreResponseEvent;

final class AssetEvent extends AbstractPreResponseEvent
{
public const EVENT_NAME = 'pre_response.asset';
public function __construct(
private readonly Asset $asset
)
{
parent::__construct($asset);
}

/**
* Use this to get additional infos out of the response object
*/
public function getAsset(): Asset
{
return $this->asset;
}
}
39 changes: 39 additions & 0 deletions src/Asset/Event/CustomSettingsEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?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\Asset\Event;

use Pimcore\Bundle\StudioBackendBundle\Asset\Schema\CustomSettings;
use Pimcore\Bundle\StudioBackendBundle\Event\AbstractPreResponseEvent;

final class CustomSettingsEvent extends AbstractPreResponseEvent
{
public const EVENT_NAME = 'pre_response.asset_custom_settings';
public function __construct(
private readonly CustomSettings $customSettings
)
{
parent::__construct($customSettings);
}

/**
* Use this to get additional infos out of the response object
*/
public function getCustomSettings(): CustomSettings
{
return $this->customSettings;
}
}
6 changes: 5 additions & 1 deletion src/Asset/Schema/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
use OpenApi\Attributes\Schema;
use Pimcore\Bundle\StudioBackendBundle\Asset\Schema\Type\Permissions;
use Pimcore\Bundle\StudioBackendBundle\Response\Element;
use Pimcore\Bundle\StudioBackendBundle\Util\Schema\AdditionalAttributesInterface;
use Pimcore\Bundle\StudioBackendBundle\Util\Traits\AdditionalAttributesTrait;

/**
* @internal
Expand All @@ -29,8 +31,10 @@
title: 'Asset',
type: 'object'
)]
class Asset extends Element
class Asset extends Element implements AdditionalAttributesInterface
{
use AdditionalAttributesTrait;

public function __construct(
#[Property(description: 'IconName', type: 'string', example: 'pimcore_icon_pdf')]
private readonly string $iconName,
Expand Down
10 changes: 7 additions & 3 deletions src/Asset/Schema/CustomSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
use OpenApi\Attributes\Property;
use OpenApi\Attributes\Schema;
use Pimcore\Bundle\StudioBackendBundle\Asset\Schema\CustomSettings\FixedCustomSettings;
use Pimcore\Bundle\StudioBackendBundle\Util\Schema\AdditionalAttributesInterface;
use Pimcore\Bundle\StudioBackendBundle\Util\Traits\AdditionalAttributesTrait;

/**
* @internal
Expand All @@ -28,22 +30,24 @@
title: 'CustomSettings',
type: 'object'
)]
final readonly class CustomSettings
final class CustomSettings implements AdditionalAttributesInterface
{
use AdditionalAttributesTrait;

public function __construct(
#[Property(
description: 'fixed custom settings',
type: FixedCustomSettings::class,
example: '{ embeddedMetadata: { FileSize: 360 KiB }, checksum: b3685e8348e7ac4d30d0268f7e58902a }')
]
private ?FixedCustomSettings $fixedCustomSettings = null,
private readonly ?FixedCustomSettings $fixedCustomSettings = null,
#[Property(
description: 'dynamic custom settings - can be any key-value pair',
type: 'array',
items: new Items(),
example: '{ imageWidth: 1280, imageHeight: 720 }')
]
private array $dynamicCustomSettings = [],
private readonly array $dynamicCustomSettings = [],
) {

}
Expand Down
88 changes: 88 additions & 0 deletions src/Asset/Service/AssetService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?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\Asset\Service;

use Pimcore\Bundle\StudioBackendBundle\Asset\Event\AssetEvent;
use Pimcore\Bundle\StudioBackendBundle\Asset\Schema\Asset;
use Pimcore\Bundle\StudioBackendBundle\Asset\Schema\Type\Archive;
use Pimcore\Bundle\StudioBackendBundle\Asset\Schema\Type\Audio;
use Pimcore\Bundle\StudioBackendBundle\Asset\Schema\Type\Document;
use Pimcore\Bundle\StudioBackendBundle\Asset\Schema\Type\Folder;
use Pimcore\Bundle\StudioBackendBundle\Asset\Schema\Type\Image;
use Pimcore\Bundle\StudioBackendBundle\Asset\Schema\Type\Text;
use Pimcore\Bundle\StudioBackendBundle\Asset\Schema\Type\Unknown;
use Pimcore\Bundle\StudioBackendBundle\Asset\Schema\Type\Video;
use Pimcore\Bundle\StudioBackendBundle\DataIndex\AssetSearchServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\DataIndex\OpenSearchFilterInterface;
use Pimcore\Bundle\StudioBackendBundle\DataIndex\Request\ElementParameters;
use Pimcore\Bundle\StudioBackendBundle\Filter\Service\FilterServiceProviderInterface;
use Pimcore\Bundle\StudioBackendBundle\Response\Collection;
use Pimcore\Bundle\StudioBackendBundle\Security\Service\SecurityServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Util\Constants\ElementTypes;
use Pimcore\Model\Element\ElementInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

/**
* @internal
*/
final readonly class AssetService implements AssetServiceInterface
{
public function __construct(
private AssetSearchServiceInterface $assetSearchService,
private FilterServiceProviderInterface $filterServiceProvider,
private EventDispatcherInterface $eventDispatcher
)
{
}


public function getAssets(ElementParameters $parameters): Collection
{
$filterService = $this->filterServiceProvider->create(OpenSearchFilterInterface::SERVICE_TYPE);

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

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

$items = $result->getItems();

foreach ($items as $item) {
$this->eventDispatcher->dispatch(
new AssetEvent($item),
AssetEvent::EVENT_NAME
);

}

return new Collection($result->getTotalItems(), $items);
}

public function getAsset(int $id): Asset|Archive|Audio|Document|Folder|Image|Text|Unknown|Video
{
$asset = $this->assetSearchService->getAssetById($id);

$this->eventDispatcher->dispatch(
new AssetEvent($asset),
AssetEvent::EVENT_NAME
);

return $asset;
}
}
Loading

0 comments on commit f4d2afb

Please sign in to comment.