From 1bad35b301c6f3e688f9f6d1d9d1cefb7738bd0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joachim=20L=C3=B8vgaard?= Date: Thu, 10 Oct 2024 12:20:57 +0200 Subject: [PATCH] Use an Image Attribute to trigger the image url generation instead of an interface --- src/DataMapper/ImageDataMapper.php | 36 +++++++++---------- src/Document/Attribute/Image.php | 17 +++++++++ src/Document/ImageAwareInterface.php | 13 ------- src/Document/Metadata/Image.php | 15 ++++++++ src/Document/Metadata/Metadata.php | 13 +++++++ src/Document/Metadata/MetadataInterface.php | 5 +++ src/Document/Product.php | 9 ++--- src/Resources/config/services/data_mapper.xml | 1 + 8 files changed, 71 insertions(+), 38 deletions(-) create mode 100644 src/Document/Attribute/Image.php delete mode 100644 src/Document/ImageAwareInterface.php create mode 100644 src/Document/Metadata/Image.php diff --git a/src/DataMapper/ImageDataMapper.php b/src/DataMapper/ImageDataMapper.php index 0fe7c63..f7d3fd6 100644 --- a/src/DataMapper/ImageDataMapper.php +++ b/src/DataMapper/ImageDataMapper.php @@ -6,24 +6,18 @@ use Liip\ImagineBundle\Imagine\Cache\CacheManager; use Setono\SyliusMeilisearchPlugin\Document\Document; -use Setono\SyliusMeilisearchPlugin\Document\ImageAwareInterface; +use Setono\SyliusMeilisearchPlugin\Document\Metadata\MetadataFactoryInterface; use Setono\SyliusMeilisearchPlugin\Model\IndexableInterface; use Setono\SyliusMeilisearchPlugin\Provider\IndexScope\IndexScope; -use Sylius\Component\Core\Model\ImageInterface; use Sylius\Component\Core\Model\ImagesAwareInterface; use Symfony\Component\Routing\Generator\UrlGeneratorInterface; use Webmozart\Assert\Assert; final class ImageDataMapper implements DataMapperInterface { - /** - * @param array, string> $entityToFilterSetMapping - */ public function __construct( private readonly CacheManager $cacheManager, - private readonly array $entityToFilterSetMapping = [], - // todo add this to the plugin configuration - private readonly string $defaultFilterSet = 'sylius_large', + private readonly MetadataFactoryInterface $metadataFactory, ) { } @@ -38,21 +32,25 @@ public function map( 'The given $source and $target is not supported', ); - $image = $source->getImages()->first(); - if (!$image instanceof ImageInterface) { - return; - } + $metadata = $this->metadataFactory->getMetadataFor($target); + foreach ($metadata->getImageAttributes() as $imageAttribute) { + $image = $imageAttribute->type === null ? $source->getImages()->first() : $source->getImagesByType($imageAttribute->type)->first(); + if (false === $image) { + continue; + } - $target->setImage($this->cacheManager->getBrowserPath( - path: (string) $image->getPath(), - filter: $this->entityToFilterSetMapping[$source::class] ?? $this->defaultFilterSet, - referenceType: UrlGeneratorInterface::ABSOLUTE_PATH, - )); + $imageUrl = $this->cacheManager->getBrowserPath( + path: (string) $image->getPath(), + filter: $imageAttribute->filterSet, + referenceType: UrlGeneratorInterface::ABSOLUTE_PATH, + ); + + $target->{$imageAttribute->name} = $imageUrl; + } } /** * @psalm-assert-if-true ImagesAwareInterface $source - * @psalm-assert-if-true ImageAwareInterface $target */ public function supports( IndexableInterface $source, @@ -60,6 +58,6 @@ public function supports( IndexScope $indexScope, array $context = [], ): bool { - return $source instanceof ImagesAwareInterface && $target instanceof ImageAwareInterface; + return $source instanceof ImagesAwareInterface && $this->metadataFactory->getMetadataFor($target)->getImageAttributes() !== []; } } diff --git a/src/Document/Attribute/Image.php b/src/Document/Attribute/Image.php new file mode 100644 index 0000000..79d8b31 --- /dev/null +++ b/src/Document/Attribute/Image.php @@ -0,0 +1,17 @@ +> */ private array $mappedProductAttributes = []; + /** @var array */ + private array $imageAttributes = []; + /** * @param class-string|Document $document */ @@ -108,6 +112,10 @@ private function loadAttributes(\ReflectionProperty|\ReflectionMethod $attribute Assert::same('array', (string) $attributesAware->getType()); $this->mappedProductAttributes[$name] = $attribute->codes; } + + if ($attribute instanceof ImageAttribute) { + $this->imageAttributes[$name] = new Image($name, $attribute->filterSet, $attribute->type); + } } } @@ -172,6 +180,11 @@ public function getMappedProductAttributes(): array return $this->mappedProductAttributes; } + public function getImageAttributes(): array + { + return $this->imageAttributes; + } + private static function isGetter(\ReflectionMethod $reflection): bool { if ($reflection->getNumberOfParameters() > 0) { diff --git a/src/Document/Metadata/MetadataInterface.php b/src/Document/Metadata/MetadataInterface.php index e7aa01d..cfa024b 100644 --- a/src/Document/Metadata/MetadataInterface.php +++ b/src/Document/Metadata/MetadataInterface.php @@ -82,4 +82,9 @@ public function getMappedProductOptions(): array; * @return array> */ public function getMappedProductAttributes(): array; + + /** + * @return array + */ + public function getImageAttributes(): array; } diff --git a/src/Document/Product.php b/src/Document/Product.php index 647c300..7624b86 100644 --- a/src/Document/Product.php +++ b/src/Document/Product.php @@ -6,10 +6,11 @@ use Setono\SyliusMeilisearchPlugin\Document\Attribute\Facetable; use Setono\SyliusMeilisearchPlugin\Document\Attribute\Filterable; +use Setono\SyliusMeilisearchPlugin\Document\Attribute\Image; use Setono\SyliusMeilisearchPlugin\Document\Attribute\Searchable; use Setono\SyliusMeilisearchPlugin\Document\Attribute\Sortable; -class Product extends Document implements UrlAwareInterface, ImageAwareInterface +class Product extends Document implements UrlAwareInterface { #[Searchable] public ?string $name = null; @@ -19,6 +20,7 @@ class Product extends Document implements UrlAwareInterface, ImageAwareInterface public ?string $url = null; + #[Image(filterSet: 'sylius_shop_product_thumbnail')] public ?string $image = null; /** @@ -62,9 +64,4 @@ public function setUrl(string $url): void { $this->url = $url; } - - public function setImage(string $image): void - { - $this->image = $image; - } } diff --git a/src/Resources/config/services/data_mapper.xml b/src/Resources/config/services/data_mapper.xml index 242e75d..96e20d3 100644 --- a/src/Resources/config/services/data_mapper.xml +++ b/src/Resources/config/services/data_mapper.xml @@ -16,6 +16,7 @@ +