Skip to content

Commit

Permalink
Add service locator instead of match
Browse files Browse the repository at this point in the history
  • Loading branch information
mattamon committed Mar 11, 2024
1 parent 0b509c9 commit e83cd27
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 46 deletions.
19 changes: 18 additions & 1 deletion config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ services:
# Services
Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\Hydrator\AssetHydratorServiceInterface:
class: Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\Hydrator\AssetHydratorService
arguments:
$assetHydratorLocator: '@generic_data_index.asset_hydrator.service_locator'

Pimcore\Bundle\StudioApiBundle\Service\AssetSearchServiceInterface:
class: Pimcore\Bundle\StudioApiBundle\Service\AssetSearchService
Expand Down Expand Up @@ -108,4 +110,19 @@ services:

#Decorators
Pimcore\Bundle\StudioApiBundle\ApiPlatform\OpenApiFactoryDecorator:
decorates: 'api_platform.openapi.factory'
decorates: 'api_platform.openapi.factory'

#Service Locator
generic_data_index.asset_hydrator.service_locator:
class: Symfony\Component\DependencyInjection\ServiceLocator
tags: ['container.service_locator']
arguments:
-
Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Asset\SearchResult\SearchResultItem\Archive: '@Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\Hydrator\Asset\ArchiveHydratorInterface'
Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Asset\SearchResult\SearchResultItem\Audio: '@Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\Hydrator\Asset\AudioHydratorInterface'
Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Asset\SearchResult\SearchResultItem\Document: '@Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\Hydrator\Asset\DocumentHydratorInterface'
Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Asset\SearchResult\SearchResultItem\Folder: '@Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\Hydrator\Asset\FolderHydratorInterface'
Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Asset\SearchResult\SearchResultItem\Image: '@Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\Hydrator\Asset\ImageHydratorInterface'
Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Asset\SearchResult\SearchResultItem\Text: '@Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\Hydrator\Asset\TextHydratorInterface'
Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Asset\SearchResult\SearchResultItem\Unknown: '@Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\Hydrator\Asset\UnknownHydratorInterface'
Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Asset\SearchResult\SearchResultItem\Video: '@Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\Hydrator\Asset\VideoHydratorInterface'
55 changes: 10 additions & 45 deletions src/Service/GenericData/V1/Hydrator/AssetHydratorService.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,64 +17,29 @@
namespace Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\Hydrator;

use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Asset\SearchResult\AssetSearchResultItem;
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Asset\SearchResult\SearchResultItem\Archive as ArchiveItem;
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Asset\SearchResult\SearchResultItem\Audio as AudioItem;
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Asset\SearchResult\SearchResultItem\Document as DocumentItem;
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Asset\SearchResult\SearchResultItem\Folder as FolderItem;
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Asset\SearchResult\SearchResultItem\Image as ImageItem;
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Asset\SearchResult\SearchResultItem\Text as TextItem;
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Asset\SearchResult\SearchResultItem\Unknown as UnknownItem;
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Asset\SearchResult\SearchResultItem\Video as VideoItem;
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\Hydrator\Asset\ArchiveHydratorInterface;
use Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\Hydrator\Asset\AudioHydratorInterface;
use Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\Hydrator\Asset\DocumentHydratorInterface;
use Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\Hydrator\Asset\FolderHydratorInterface;
use Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\Hydrator\Asset\ImageHydratorInterface;
use Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\Hydrator\Asset\TextHydratorInterface;
use Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\Hydrator\Asset\UnknownHydratorInterface;
use Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\Hydrator\Asset\VideoHydratorInterface;
use Symfony\Contracts\Service\ServiceProviderInterface;

final readonly class AssetHydratorService implements AssetHydratorServiceInterface
{
public function __construct(
private ArchiveHydratorInterface $archiveHydrator,
private AudioHydratorInterface $audioHydrator,
private DocumentHydratorInterface $documentHydrator,
private FolderHydratorInterface $folderHydrator,
private ImageHydratorInterface $imageHydrator,
private TextHydratorInterface $textHydrator,
private UnknownHydratorInterface $unknownHydrator,
private VideoHydratorInterface $videoHydrator,
private ServiceProviderInterface $assetHydratorLocator,
private AssetHydratorInterface $assetHydrator
) {
}

/**
* @param AssetSearchResultItem $item
*
* @return Asset|Archive|Audio|Document|Folder|Image|Text|Unknown|Video
* @return Asset
*/
public function hydrate(AssetSearchResultItem $item): Asset|Archive|Audio|Document|Folder|Image|Text|Unknown|Video
public function hydrate(AssetSearchResultItem $item): Asset
{
return match (true) {
$item instanceof ArchiveItem => $this->archiveHydrator->hydrate($item),
$item instanceof AudioItem => $this->audioHydrator->hydrate($item),
$item instanceof DocumentItem => $this->documentHydrator->hydrate($item),
$item instanceof FolderItem => $this->folderHydrator->hydrate($item),
$item instanceof ImageItem => $this->imageHydrator->hydrate($item),
$item instanceof TextItem => $this->textHydrator->hydrate($item),
$item instanceof UnknownItem => $this->unknownHydrator->hydrate($item),
$item instanceof VideoItem => $this->videoHydrator->hydrate($item),
default => $this->assetHydrator->hydrate($item)
};
$class = get_class($item);
if($this->assetHydratorLocator->has($class)) {
return $this->assetHydratorLocator->get($class)->hydrate($item);
}

return $this->assetHydrator->hydrate($item);
}
}

0 comments on commit e83cd27

Please sign in to comment.