Skip to content

Commit

Permalink
Removing the IndexResource command
Browse files Browse the repository at this point in the history
  • Loading branch information
loevgaard committed Jul 1, 2024
1 parent 1ff05d4 commit 2370167
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 114 deletions.
1 change: 1 addition & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public function getConfigTreeBuilder(): TreeBuilder
->cannotBeEmpty()
->defaultValue('setono_sylius_meilisearch.indexer.default')
->end()
// todo change this to entities instead
->arrayNode('resources')
->info('The Sylius resources that make up this index. Examples could be "sylius.product", "sylius.taxon", etc.')
->scalarPrototype()->end()
Expand Down
4 changes: 2 additions & 2 deletions src/EventSubscriber/Doctrine/EntityChangeSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ public function getSubscribedEvents(): array

public function update(LifecycleEventArgs $eventArgs): void
{
$this->dispatch($eventArgs, static fn (IndexableInterface $entity) => new IndexEntity($entity));
$this->dispatch($eventArgs, static fn (IndexableInterface $entity) => IndexEntity::new($entity));
}

public function remove(LifecycleEventArgs $eventArgs): void
{
$this->dispatch($eventArgs, static fn (IndexableInterface $entity) => new RemoveEntity($entity));
$this->dispatch($eventArgs, static fn (IndexableInterface $entity) => RemoveEntity::new($entity));
}

/**
Expand Down
29 changes: 14 additions & 15 deletions src/Indexer/DefaultIndexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use Setono\SyliusMeilisearchPlugin\Filter\Object\FilterInterface as ObjectFilterInterface;
use Setono\SyliusMeilisearchPlugin\IndexScope\IndexScope;
use Setono\SyliusMeilisearchPlugin\Message\Command\IndexEntities;
use Setono\SyliusMeilisearchPlugin\Message\Command\IndexResource;
use Setono\SyliusMeilisearchPlugin\Model\IndexableInterface;
use Setono\SyliusMeilisearchPlugin\Provider\IndexScope\IndexScopeProviderInterface;
use Setono\SyliusMeilisearchPlugin\Provider\IndexSettings\IndexSettingsProviderInterface;
Expand Down Expand Up @@ -62,20 +61,7 @@ public function index(Index|string $index): void
}

foreach ($index->resources as $resource) {
$this->commandBus->dispatch(new IndexResource($index, $resource));
}
}

public function indexResource(Index|string $index, string $resource): void
{
if (is_string($index)) {
$index = $this->indexRegistry->get($index);
}

$indexableResource = $index->getResource($resource);

foreach ($this->getIdBatches($indexableResource) as $ids) {
$this->commandBus->dispatch(new IndexEntities($indexableResource, $ids));
$this->indexResource($index, $resource);
}
}

Expand Down Expand Up @@ -126,6 +112,19 @@ public function removeEntitiesWithIds(array $ids, string $type): void
}
}

protected function indexResource(Index|string $index, IndexableResource|string $resource): void
{
if (is_string($index)) {
$index = $this->indexRegistry->get($index);
}

$indexableResource = $index->getResource($resource);

foreach ($this->getIdBatches($indexableResource) as $ids) {
$this->commandBus->dispatch(new IndexEntities($indexableResource, $ids));
}
}

/**
* @return \Generator<int, non-empty-list<mixed>>
*/
Expand Down
9 changes: 0 additions & 9 deletions src/Indexer/IndexerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Setono\SyliusMeilisearchPlugin\Config\Index;
use Setono\SyliusMeilisearchPlugin\Exception\NonExistingIndexException;
use Setono\SyliusMeilisearchPlugin\Exception\NonExistingResourceException;
use Setono\SyliusMeilisearchPlugin\Model\IndexableInterface;

interface IndexerInterface
Expand All @@ -18,14 +17,6 @@ interface IndexerInterface
*/
public function index(Index|string $index): void;

/**
* This method will index all entities for a given indexable resource on the given index
*
* @throws NonExistingIndexException if the $index is a string and the index doesn't exist
* @throws NonExistingResourceException if the $resource doesn't exist on the given $index
*/
public function indexResource(Index|string $index, string $resource): void;

/**
* Will index a single entity
*/
Expand Down
17 changes: 9 additions & 8 deletions src/Message/Command/EntityAwareCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@

abstract class EntityAwareCommand implements CommandInterface
{
/** @var class-string<IndexableInterface> */
public string $entityClass;

/** @var mixed */
public $entityId;
final public function __construct(
/** @var class-string<IndexableInterface> $class */
public readonly string $class,
/** @var mixed $id */
public readonly mixed $id,
) {
}

public function __construct(IndexableInterface $resource)
public static function new(IndexableInterface $object): static
{
$this->entityClass = $resource::class;
$this->entityId = $resource->getId();
return new static($object::class, $object->getId());
}
}
43 changes: 0 additions & 43 deletions src/Message/Command/IndexResource.php

This file was deleted.

8 changes: 4 additions & 4 deletions src/Message/Handler/IndexEntityHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@

namespace Setono\SyliusMeilisearchPlugin\Message\Handler;

use Setono\SyliusMeilisearchPlugin\Config\IndexRegistry;
use Setono\SyliusMeilisearchPlugin\Config\IndexRegistryInterface;
use Setono\SyliusMeilisearchPlugin\Message\Command\IndexEntity;
use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException;

final class IndexEntityHandler
{
public function __construct(private readonly IndexRegistry $indexRegistry)
public function __construct(private readonly IndexRegistryInterface $indexRegistry)
{
}

public function __invoke(IndexEntity $message): void
{
try {
$this->indexRegistry
->getByResource($message->entityClass)
->getByResource($message->class)
->indexer
->indexEntityWithId($message->entityId, $message->entityClass)
->indexEntityWithId($message->id, $message->class)
;
} catch (\InvalidArgumentException $e) {
// todo create better exception
Expand Down
31 changes: 0 additions & 31 deletions src/Message/Handler/IndexResourceHandler.php

This file was deleted.

3 changes: 1 addition & 2 deletions src/Resolver/SortBy/SortByResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ public function __construct(
private readonly IndexNameResolverInterface $indexNameResolver,
private readonly TranslatorInterface $translator,
private readonly LocaleContextInterface $localeContext,
)
{
) {
}

public function resolveFromIndexableResource(Index $index, string $locale = null): array
Expand Down

0 comments on commit 2370167

Please sign in to comment.