Skip to content

Commit

Permalink
Merge pull request #35 from Setono/refactor-image-url
Browse files Browse the repository at this point in the history
Use an Image Attribute to trigger the image url generation instead of an interface
  • Loading branch information
loevgaard authored Oct 10, 2024
2 parents cec6f0d + 1bad35b commit fd9b59d
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 38 deletions.
36 changes: 17 additions & 19 deletions src/DataMapper/ImageDataMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<class-string<IndexableInterface>, 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,
) {
}

Expand All @@ -38,28 +32,32 @@ 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,
Document $target,
IndexScope $indexScope,
array $context = [],
): bool {
return $source instanceof ImagesAwareInterface && $target instanceof ImageAwareInterface;
return $source instanceof ImagesAwareInterface && $this->metadataFactory->getMetadataFor($target)->getImageAttributes() !== [];
}
}
17 changes: 17 additions & 0 deletions src/Document/Attribute/Image.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Setono\SyliusMeilisearchPlugin\Document\Attribute;

use Attribute;

#[Attribute(Attribute::TARGET_PROPERTY)]
class Image
{
public function __construct(
public readonly string $filterSet,
public readonly ?string $type = null,
) {
}
}
13 changes: 0 additions & 13 deletions src/Document/ImageAwareInterface.php

This file was deleted.

15 changes: 15 additions & 0 deletions src/Document/Metadata/Image.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Setono\SyliusMeilisearchPlugin\Document\Metadata;

final class Image
{
public function __construct(
public readonly string $name,
public readonly string $filterSet,
public readonly ?string $type,
) {
}
}
13 changes: 13 additions & 0 deletions src/Document/Metadata/Metadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Setono\SyliusMeilisearchPlugin\Document\Attribute\Facetable as FacetableAttribute;
use Setono\SyliusMeilisearchPlugin\Document\Attribute\Filterable as FilterableAttribute;
use Setono\SyliusMeilisearchPlugin\Document\Attribute\Image as ImageAttribute;
use Setono\SyliusMeilisearchPlugin\Document\Attribute\MapProductAttribute;
use Setono\SyliusMeilisearchPlugin\Document\Attribute\MapProductOption;
use Setono\SyliusMeilisearchPlugin\Document\Attribute\Searchable as SearchableAttribute;
Expand Down Expand Up @@ -37,6 +38,9 @@ final class Metadata implements MetadataInterface
/** @var array<string, list<string>> */
private array $mappedProductAttributes = [];

/** @var array<string, Image> */
private array $imageAttributes = [];

/**
* @param class-string<Document>|Document $document
*/
Expand Down Expand Up @@ -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);
}
}
}

Expand Down Expand Up @@ -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) {
Expand Down
5 changes: 5 additions & 0 deletions src/Document/Metadata/MetadataInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,9 @@ public function getMappedProductOptions(): array;
* @return array<string, list<string>>
*/
public function getMappedProductAttributes(): array;

/**
* @return array<string, Image>
*/
public function getImageAttributes(): array;
}
9 changes: 3 additions & 6 deletions src/Document/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

/**
Expand Down Expand Up @@ -62,9 +64,4 @@ public function setUrl(string $url): void
{
$this->url = $url;
}

public function setImage(string $image): void
{
$this->image = $image;
}
}
1 change: 1 addition & 0 deletions src/Resources/config/services/data_mapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

<service id="Setono\SyliusMeilisearchPlugin\DataMapper\ImageDataMapper">
<argument type="service" id="liip_imagine.cache.manager"/>
<argument type="service" id="Setono\SyliusMeilisearchPlugin\Document\Metadata\MetadataFactoryInterface"/>

<tag name="setono_sylius_meilisearch.data_mapper" priority="100"/>
</service>
Expand Down

0 comments on commit fd9b59d

Please sign in to comment.