Skip to content

Commit

Permalink
Introduce asset service
Browse files Browse the repository at this point in the history
  • Loading branch information
mattamon committed May 24, 2024
1 parent 06c1489 commit 75dd826
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 14 deletions.
5 changes: 4 additions & 1 deletion config/assets.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,7 @@ 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
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()
);
}
}
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;
}
}
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
67 changes: 67 additions & 0 deletions src/Asset/Service/AssetService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?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\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\Note\Event\NoteEvent;
use Pimcore\Bundle\StudioBackendBundle\Response\Collection;
use Pimcore\Bundle\StudioBackendBundle\Util\Constants\ElementTypes;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

/**
* @internal
*/
final class AssetService implements AssetServiceInterface
{
public function __construct(
private readonly AssetSearchServiceInterface $assetSearchService,
private readonly FilterServiceProviderInterface $filterServiceProvider,
private readonly 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);
}
}
28 changes: 28 additions & 0 deletions src/Asset/Service/AssetServiceInterface.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 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\DataIndex\Request\ElementParameters;
use Pimcore\Bundle\StudioBackendBundle\Response\Collection;

/**
* @internal
*/
interface AssetServiceInterface
{
public function getAssets(ElementParameters $parameters): Collection;
}

0 comments on commit 75dd826

Please sign in to comment.