Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use entities in the configuration instead of resources #5

Merged
merged 1 commit into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 0 additions & 16 deletions .github/dependabot.yml

This file was deleted.

43 changes: 13 additions & 30 deletions src/Config/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@
namespace Setono\SyliusMeilisearchPlugin\Config;

use Setono\SyliusMeilisearchPlugin\Document\Document;
use Setono\SyliusMeilisearchPlugin\Exception\NonExistingResourceException;
use Setono\SyliusMeilisearchPlugin\Indexer\IndexerInterface;
use Setono\SyliusMeilisearchPlugin\Model\IndexableInterface;

final class Index implements \Stringable
{
public function __construct(
/**
* This is the name you gave the index in the configuration.
* This name is also used when resolving the final index name in Meilisearch, so do not change this unless you know what you're doing
* The name is also used when resolving the final index name in Meilisearch, so do not change this unless you know what you're doing
*/
public readonly string $name,

/**
* This is the FQCN for the document that is mapped to an index in Algolia.
* If you are indexing products this could be Setono\SyliusMeilisearchPlugin\Document\Product
Expand All @@ -24,12 +25,13 @@ public function __construct(
*/
public readonly string $document,
public readonly IndexerInterface $indexer,

/**
* An array of resources, indexed by the resource name
* A list of entities that should be indexed in this index
*
* @var array<string, IndexableResource> $resources
* @var list<class-string<IndexableInterface>> $entities
*/
public readonly array $resources,
public readonly array $entities,
public readonly ?string $prefix = null,
) {
if (!is_a($document, Document::class, true)) {
Expand All @@ -41,36 +43,17 @@ public function __construct(
}
}

public function hasResource(string|IndexableResource $resource): bool
{
if ($resource instanceof IndexableResource) {
$resource = $resource->name;
}

return isset($this->resources[$resource]);
}

/**
* @throws NonExistingResourceException if a resource with the given name doesn't exist on this index
* @param class-string|object $entity
*/
public function getResource(string $name): IndexableResource
public function hasEntity(string|object $entity): bool
{
if (!$this->hasResource($name)) {
throw NonExistingResourceException::fromNameAndIndex($name, $this->name, array_keys($this->resources));
if (is_object($entity)) {
$entity = $entity::class;
}

return $this->resources[$name];
}

/**
* Returns true if any of the resources configured on this index is an instance of the given class
*
* @param class-string $class
*/
public function hasResourceWithClass(string $class): bool
{
foreach ($this->resources as $resource) {
if ($resource->is($class)) {
foreach ($this->entities as $concreteEntity) {
if (is_a($concreteEntity, $entity, true)) {
return true;
}
}
Expand Down
37 changes: 9 additions & 28 deletions src/Config/IndexRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,10 @@ final class IndexRegistry implements \IteratorAggregate, IndexRegistryInterface
*/
private array $indexes = [];

/**
* @throws \InvalidArgumentException if one of the resources on the $index has already been configured on another index
*/
public function add(Index $index): void
{
foreach ($this->indexes as $existingIndex) {
foreach ($index->resources as $resource) {
if ($existingIndex->hasResource($resource)) {
// todo why is this a problem?
throw new \InvalidArgumentException(sprintf(
'The resource "%s" is already defined on the index "%s"',
$resource->name,
$existingIndex->name,
));
}
}
if (isset($this->indexes[$index->name])) {
throw new \InvalidArgumentException(sprintf('An index with the name %s already exists', $index->name));
}

$this->indexes[$index->name] = $index;
Expand All @@ -51,26 +39,19 @@ public function get(string $name): Index
return $this->indexes[$name];
}

/**
* This method returns the index where the $class is configured
*
* @param object|class-string $class
*
* @throws \InvalidArgumentException if the given resource is not configured on any index
*/
public function getByResource(object|string $class): Index
public function getByEntity(object|string $entity): array
{
if (is_object($class)) {
$class = $class::class;
}
$entity = is_object($entity) ? $entity::class : $entity;

$indexes = [];

foreach ($this->indexes as $index) {
if ($index->hasResourceWithClass($class)) {
return $index;
if ($index->hasEntity($entity)) {
$indexes[] = $index;
}
}

throw new \InvalidArgumentException(sprintf('No index exists having a resource that is an instance of %s', $class));
return $indexes;
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/Config/IndexRegistryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
interface IndexRegistryInterface extends \Traversable
{
/**
* @throws \InvalidArgumentException if one of the resources on the $index has already been configured on another index
* @throws \InvalidArgumentException if an index with the same name already exists
*/
public function add(Index $index): void;

Expand All @@ -24,9 +24,9 @@ public function get(string $name): Index;
/**
* This method returns the index where the $class is configured
*
* @param object|class-string $class
* @param object|class-string $entity
*
* @throws \InvalidArgumentException if the given resource is not configured on any index
* @return list<Index>
*/
public function getByResource(object|string $class): Index;
public function getByEntity(object|string $entity): array;
}
53 changes: 0 additions & 53 deletions src/Config/IndexableResource.php

This file was deleted.

11 changes: 3 additions & 8 deletions src/Controller/Action/SearchAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,13 @@ public function __invoke(string $slug): Response
));
}

try {
$indexableResource = $this->indexRegistry->getByResource(ProductInterface::class);
} catch (\InvalidArgumentException $e) {
throw new NotFoundHttpException($e->getMessage(), $e);
}

$index = $this->indexNameResolver->resolve($indexableResource);
// todo we should get the index from the plugin configuration
$index = $this->indexNameResolver->resolve(ProductInterface::class);

$response = new Response($this->twig->render('@SetonoSyliusMeilisearchPlugin/shop/product/index.html.twig', [
'index' => $index,
'taxon' => $taxon,
'sortBy' => $this->sortByResolver->resolveFromIndexableResource($indexableResource),
//'sortBy' => $this->sortByResolver->resolveFromIndexableResource($indexableResource),
]));

$event = new ProductIndexEvent($response, $index, $taxon, $slug, $locale);
Expand Down
6 changes: 3 additions & 3 deletions src/DataMapper/CompositeDataMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Setono\SyliusMeilisearchPlugin\Document\Document;
use Setono\SyliusMeilisearchPlugin\IndexScope\IndexScope;
use Sylius\Component\Resource\Model\ResourceInterface;
use Setono\SyliusMeilisearchPlugin\Model\IndexableInterface;

final class CompositeDataMapper implements DataMapperInterface
{
Expand All @@ -18,7 +18,7 @@ public function add(DataMapperInterface $dataMapper): void
$this->dataMappers[] = $dataMapper;
}

public function map(ResourceInterface $source, Document $target, IndexScope $indexScope, array $context = []): void
public function map(IndexableInterface $source, Document $target, IndexScope $indexScope, array $context = []): void
{
foreach ($this->dataMappers as $dataMapper) {
if ($dataMapper->supports($source, $target, $indexScope, $context)) {
Expand All @@ -27,7 +27,7 @@ public function map(ResourceInterface $source, Document $target, IndexScope $ind
}
}

public function supports(ResourceInterface $source, Document $target, IndexScope $indexScope, array $context = []): bool
public function supports(IndexableInterface $source, Document $target, IndexScope $indexScope, array $context = []): bool
{
foreach ($this->dataMappers as $dataMapper) {
if ($dataMapper->supports($source, $target, $indexScope, $context)) {
Expand Down
6 changes: 3 additions & 3 deletions src/DataMapper/DataMapperInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Setono\SyliusMeilisearchPlugin\Document\Document;
use Setono\SyliusMeilisearchPlugin\IndexScope\IndexScope;
use Sylius\Component\Resource\Model\ResourceInterface;
use Setono\SyliusMeilisearchPlugin\Model\IndexableInterface;

interface DataMapperInterface
{
Expand All @@ -15,12 +15,12 @@
*
* @param array<string, mixed> $context
*/
public function map(ResourceInterface $source, Document $target, IndexScope $indexScope, array $context = []): void;
public function map(IndexableInterface $source, Document $target, IndexScope $indexScope, array $context = []): void;

Check failure on line 18 in src/DataMapper/DataMapperInterface.php

View workflow job for this annotation

GitHub Actions / Backwards Compatibility Check

The parameter $source of Setono\SyliusMeilisearchPlugin\DataMapper\DataMapperInterface#map() changed from Sylius\Component\Resource\Model\ResourceInterface to a non-contravariant Setono\SyliusMeilisearchPlugin\Model\IndexableInterface

Check failure on line 18 in src/DataMapper/DataMapperInterface.php

View workflow job for this annotation

GitHub Actions / Backwards Compatibility Check

The parameter $source of Setono\SyliusMeilisearchPlugin\DataMapper\DataMapperInterface#map() changed from Sylius\Component\Resource\Model\ResourceInterface to Setono\SyliusMeilisearchPlugin\Model\IndexableInterface

/**
* Returns true if this data mapper supports the given $source and $target
*
* @param array<string, mixed> $context
*/
public function supports(ResourceInterface $source, Document $target, IndexScope $indexScope, array $context = []): bool;
public function supports(IndexableInterface $source, Document $target, IndexScope $indexScope, array $context = []): bool;

Check failure on line 25 in src/DataMapper/DataMapperInterface.php

View workflow job for this annotation

GitHub Actions / Backwards Compatibility Check

The parameter $source of Setono\SyliusMeilisearchPlugin\DataMapper\DataMapperInterface#supports() changed from Sylius\Component\Resource\Model\ResourceInterface to a non-contravariant Setono\SyliusMeilisearchPlugin\Model\IndexableInterface

Check failure on line 25 in src/DataMapper/DataMapperInterface.php

View workflow job for this annotation

GitHub Actions / Backwards Compatibility Check

The parameter $source of Setono\SyliusMeilisearchPlugin\DataMapper\DataMapperInterface#supports() changed from Sylius\Component\Resource\Model\ResourceInterface to Setono\SyliusMeilisearchPlugin\Model\IndexableInterface
}
12 changes: 6 additions & 6 deletions src/DataMapper/ImageUrlsDataMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,26 @@
use Setono\SyliusMeilisearchPlugin\Document\Document;
use Setono\SyliusMeilisearchPlugin\Document\ImageUrlsAwareInterface;
use Setono\SyliusMeilisearchPlugin\IndexScope\IndexScope;
use Setono\SyliusMeilisearchPlugin\Model\IndexableInterface;
use Sylius\Component\Core\Model\ImagesAwareInterface;
use Sylius\Component\Resource\Model\ResourceInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Webmozart\Assert\Assert;

final class ImageUrlsDataMapper implements DataMapperInterface
{
/**
* @param array<class-string<ResourceInterface>, string> $resourceToFilterSetMapping
* @param array<class-string<IndexableInterface>, string> $entityToFilterSetMapping
*/
public function __construct(
private readonly CacheManager $cacheManager,
private readonly array $resourceToFilterSetMapping = [],
private readonly array $entityToFilterSetMapping = [],
// todo add this to the plugin configuration
private readonly string $defaultFilterSet = 'sylius_large',
) {
}

public function map(

Check failure on line 29 in src/DataMapper/ImageUrlsDataMapper.php

View workflow job for this annotation

GitHub Actions / Backwards Compatibility Check

The parameter $source of Setono\SyliusMeilisearchPlugin\DataMapper\ImageUrlsDataMapper#map() changed from Sylius\Component\Resource\Model\ResourceInterface to a non-contravariant Setono\SyliusMeilisearchPlugin\Model\IndexableInterface
ResourceInterface $source,
IndexableInterface $source,
Document $target,
IndexScope $indexScope,
array $context = [],
Expand All @@ -42,7 +42,7 @@
foreach ($source->getImages() as $image) {
$imageUrls[] = $this->cacheManager->getBrowserPath(
(string) $image->getPath(),
$this->resourceToFilterSetMapping[$source::class] ?? $this->defaultFilterSet,
$this->entityToFilterSetMapping[$source::class] ?? $this->defaultFilterSet,
[],
null,
UrlGeneratorInterface::ABSOLUTE_PATH,
Expand All @@ -56,8 +56,8 @@
* @psalm-assert-if-true ImagesAwareInterface $source
* @psalm-assert-if-true ImageUrlsAwareInterface $target
*/
public function supports(

Check failure on line 59 in src/DataMapper/ImageUrlsDataMapper.php

View workflow job for this annotation

GitHub Actions / Backwards Compatibility Check

The parameter $source of Setono\SyliusMeilisearchPlugin\DataMapper\ImageUrlsDataMapper#supports() changed from Sylius\Component\Resource\Model\ResourceInterface to a non-contravariant Setono\SyliusMeilisearchPlugin\Model\IndexableInterface
ResourceInterface $source,
IndexableInterface $source,
Document $target,
IndexScope $indexScope,
array $context = [],
Expand Down
Loading
Loading