Skip to content

Commit 70d4407

Browse files
committed
Renaming some index registry methods and introducing an interface
1 parent deb322e commit 70d4407

File tree

13 files changed

+50
-25
lines changed

13 files changed

+50
-25
lines changed

src/Config/IndexRegistry.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@
77
use Setono\SyliusMeilisearchPlugin\Exception\NonExistingIndexException;
88

99
/**
10-
* todo introduce interface
11-
*
1210
* @implements \IteratorAggregate<string, Index>
1311
*/
14-
final class IndexRegistry implements \IteratorAggregate
12+
final class IndexRegistry implements \IteratorAggregate, IndexRegistryInterface
1513
{
1614
/**
1715
* An array of indexes, indexed by the name of the index
@@ -28,6 +26,7 @@ public function add(Index $index): void
2826
foreach ($this->indexes as $existingIndex) {
2927
foreach ($index->resources as $resource) {
3028
if ($existingIndex->hasResource($resource)) {
29+
// todo why is this a problem?
3130
throw new \InvalidArgumentException(sprintf(
3231
'The resource "%s" is already defined on the index "%s"',
3332
$resource->name,
@@ -43,7 +42,7 @@ public function add(Index $index): void
4342
/**
4443
* @throws NonExistingIndexException if no index exists with the given name
4544
*/
46-
public function getByName(string $name): Index
45+
public function get(string $name): Index
4746
{
4847
if (!isset($this->indexes[$name])) {
4948
throw NonExistingIndexException::fromName($name, array_keys($this->indexes));
@@ -59,16 +58,13 @@ public function getByName(string $name): Index
5958
*
6059
* @throws \InvalidArgumentException if the given resource is not configured on any index
6160
*/
62-
public function getByResourceClass($class): Index
61+
public function getByResource(object|string $class): Index
6362
{
6463
if (is_object($class)) {
65-
$class = get_class($class);
64+
$class = $class::class;
6665
}
6766

6867
foreach ($this->indexes as $index) {
69-
// todo the old if here was
70-
// if (is_a($resource->resourceClass, $class, true) || is_a($class, $resource->resourceClass, true)) {
71-
// what was the reason for that?
7268
if ($index->hasResourceWithClass($class)) {
7369
return $index;
7470
}

src/Config/IndexRegistryInterface.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Setono\SyliusMeilisearchPlugin\Config;
6+
7+
use Setono\SyliusMeilisearchPlugin\Exception\NonExistingIndexException;
8+
9+
interface IndexRegistryInterface extends \Traversable
10+
{
11+
/**
12+
* @throws \InvalidArgumentException if one of the resources on the $index has already been configured on another index
13+
*/
14+
public function add(Index $index): void;
15+
16+
/**
17+
* @throws NonExistingIndexException if no index exists with the given name
18+
*/
19+
public function get(string $name): Index;
20+
21+
/**
22+
* This method returns the index where the $class is configured
23+
*
24+
* @param object|class-string $class
25+
*
26+
* @throws \InvalidArgumentException if the given resource is not configured on any index
27+
*/
28+
public function getByResource(object|string $class): Index;
29+
}

src/Config/IndexableResource.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ public function __construct(string $name, string $class)
4848
*
4949
* @param object|class-string $class
5050
*/
51-
public function is($class): bool
51+
public function is(object|string $class): bool
5252
{
5353
if (is_object($class)) {
54-
$class = get_class($class);
54+
$class = $class::class;
5555
}
5656

5757
return is_a($this->class, $class, true);

src/Controller/Action/SearchAction.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function __invoke(string $slug): Response
6464
}
6565

6666
try {
67-
$indexableResource = $this->indexRegistry->getByResourceClass(ProductInterface::class);
67+
$indexableResource = $this->indexRegistry->getByResource(ProductInterface::class);
6868
} catch (\InvalidArgumentException $e) {
6969
throw new NotFoundHttpException($e->getMessage(), $e);
7070
}

src/Exception/NonExistingIndexException.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ final class NonExistingIndexException extends \InvalidArgumentException
99
/**
1010
* @param list<string>|null $availableIndexes
1111
*/
12-
public static function fromName(string $name, array $availableIndexes = null): self
12+
public static function fromName(string $name, array $availableIndexes = []): self
1313
{
1414
$message = sprintf('No index exists with the name "%s".', $name);
1515

16-
if (null !== $availableIndexes && [] !== $availableIndexes) {
16+
if ([] !== $availableIndexes) {
1717
$message .= sprintf(' Available indexes are: [%s]', implode(', ', $availableIndexes));
1818
}
1919

src/Indexer/DefaultIndexer.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public function __construct(
9090
public function index(Index|string $index): void
9191
{
9292
if (is_string($index)) {
93-
$index = $this->indexRegistry->getByName($index);
93+
$index = $this->indexRegistry->get($index);
9494
}
9595

9696
foreach ($index->resources as $resource) {
@@ -101,7 +101,7 @@ public function index(Index|string $index): void
101101
public function indexResource(Index|string $index, string $resource): void
102102
{
103103
if (is_string($index)) {
104-
$index = $this->indexRegistry->getByName($index);
104+
$index = $this->indexRegistry->get($index);
105105
}
106106

107107
$indexableResource = $index->getResource($resource);
@@ -117,7 +117,7 @@ public function indexEntitiesWithIds(array $ids, string $type): void
117117
return;
118118
}
119119

120-
$index = $this->indexRegistry->getByResourceClass($type);
120+
$index = $this->indexRegistry->getByResource($type);
121121

122122
foreach ($this->indexScopeProvider->getAll($index) as $indexScope) {
123123
$algoliaIndex = $this->prepareIndex(
@@ -144,7 +144,7 @@ public function removeEntitiesWithIds(array $ids, string $type): void
144144
return;
145145
}
146146

147-
$index = $this->indexRegistry->getByResourceClass($type);
147+
$index = $this->indexRegistry->getByResource($type);
148148

149149
foreach ($this->indexScopeProvider->getAll($index) as $indexScope) {
150150
$algoliaIndex = $this->prepareIndex(

src/Javascript/Autocomplete/SourcesResolver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function getSources(): array
3434
{
3535
$sources = [];
3636
foreach ($this->searchIndexes as $searchIndex) {
37-
$index = $this->indexRegistry->getByName($searchIndex);
37+
$index = $this->indexRegistry->get($searchIndex);
3838
$sources[] = new Source($searchIndex, $this->indexNameResolver->resolve($index));
3939
}
4040

src/Message/Handler/IndexEntitiesHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function __invoke(IndexEntities $message): void
2222
{
2323
try {
2424
$this->indexRegistry
25-
->getByResourceClass($message->resource->class)
25+
->getByResource($message->resource->class)
2626
->indexer
2727
->indexEntitiesWithIds($message->ids, $message->resource->class)
2828
;

src/Message/Handler/IndexEntityHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function __invoke(IndexEntity $message): void
2222
{
2323
try {
2424
$this->indexRegistry
25-
->getByResourceClass($message->entityClass)
25+
->getByResource($message->entityClass)
2626
->indexer
2727
->indexEntityWithId($message->entityId, $message->entityClass)
2828
;

src/Message/Handler/IndexHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function __construct(IndexRegistry $indexRegistry)
2121
public function __invoke(Index $message): void
2222
{
2323
try {
24-
$this->indexRegistry->getByName($message->index)->indexer->index($message->index);
24+
$this->indexRegistry->get($message->index)->indexer->index($message->index);
2525
} catch (NonExistingIndexException $e) {
2626
throw new UnrecoverableMessageHandlingException($e->getMessage(), 0, $e);
2727
}

0 commit comments

Comments
 (0)