diff --git a/src/Exception/UnsupportedIndexLanguageException.php b/src/Exception/UnsupportedIndexLanguageException.php new file mode 100644 index 0000000..8676722 --- /dev/null +++ b/src/Exception/UnsupportedIndexLanguageException.php @@ -0,0 +1,35 @@ +code . ': ' . $message, + $code, + $previous + ); + } + + public function getIndex(): string + { + return $this->index; + } + + public function getLang(): ResourceLanguage + { + return $this->lang; + } +} diff --git a/src/Service/IndexName.php b/src/Service/IndexName.php index 2fe9163..1d3ac99 100644 --- a/src/Service/IndexName.php +++ b/src/Service/IndexName.php @@ -5,9 +5,14 @@ namespace Atoolo\Search\Service; use Atoolo\Resource\ResourceLanguage; +use Atoolo\Search\Exception\UnsupportedIndexLanguageException; interface IndexName { + /** + * @throws UnsupportedIndexLanguageException Is thrown if no valid index + * can be determined for the language. + */ public function name(ResourceLanguage $lang): string; /** diff --git a/src/Service/Indexer/InternalResourceIndexer.php b/src/Service/Indexer/InternalResourceIndexer.php index 3547f60..c633042 100644 --- a/src/Service/Indexer/InternalResourceIndexer.php +++ b/src/Service/Indexer/InternalResourceIndexer.php @@ -10,6 +10,7 @@ use Atoolo\Resource\ResourceLocation; use Atoolo\Search\Dto\Indexer\IndexerParameter; use Atoolo\Search\Dto\Indexer\IndexerStatus; +use Atoolo\Search\Exception\UnsupportedIndexLanguageException; use Atoolo\Search\Indexer; use Exception; use Psr\Log\LoggerInterface; @@ -248,23 +249,19 @@ private function indexTranslationSplittedResources( ); foreach ($splitterResult->getLanguages() as $lang) { - $langIndex = $this->indexService->getIndex($lang); - - if ($index === $langIndex) { - $this->handleError( - 'No Index for language "' . $lang->code . '" ' . - 'found (base index: "' . $index . '")' + try { + $langIndex = $this->indexService->getIndex($lang); + $this->indexResourcesPerLanguageIndex( + $processId, + $parameter, + $lang, + $langIndex, + $splitterResult->getTranslations($lang) ); + } catch (UnsupportedIndexLanguageException $e) { + $this->handleError($e->getMessage()); continue; } - - $this->indexResourcesPerLanguageIndex( - $processId, - $parameter, - $lang, - $langIndex, - $splitterResult->getTranslations($lang) - ); } } diff --git a/src/Service/ResourceChannelBasedIndexName.php b/src/Service/ResourceChannelBasedIndexName.php index f481dcc..dabf43a 100644 --- a/src/Service/ResourceChannelBasedIndexName.php +++ b/src/Service/ResourceChannelBasedIndexName.php @@ -6,6 +6,7 @@ use Atoolo\Resource\ResourceChannel; use Atoolo\Resource\ResourceLanguage; +use Atoolo\Search\Exception\UnsupportedIndexLanguageException; class ResourceChannelBasedIndexName implements IndexName { @@ -14,12 +15,17 @@ public function __construct( ) { } + /** + * @throws UnsupportedIndexLanguageException + */ public function name(ResourceLanguage $lang): string { $locale = $this->langToAvailableLocale($this->resourceChannel, $lang); + if (empty($locale)) { return $this->resourceChannel->searchIndex; } + return $this->resourceChannel->searchIndex . '-' . $locale; } @@ -40,6 +46,9 @@ public function names(): array return $names; } + /** + * @throws UnsupportedIndexLanguageException + */ private function langToAvailableLocale( ResourceChannel $resourceChannel, ResourceLanguage $lang @@ -56,6 +65,11 @@ private function langToAvailableLocale( return $availableLocale; } } - return ''; + throw new UnsupportedIndexLanguageException( + $resourceChannel->searchIndex, + $lang, + 'No valid index can be determined for the language ' . + $lang->code + ); } } diff --git a/test/Exception/UnsupportedIndexLanguageExceptionTest.php b/test/Exception/UnsupportedIndexLanguageExceptionTest.php new file mode 100644 index 0000000..f72b9d1 --- /dev/null +++ b/test/Exception/UnsupportedIndexLanguageExceptionTest.php @@ -0,0 +1,38 @@ +assertEquals( + 'test', + $e->getIndex(), + 'unexpected index' + ); + } + + public function testGetLang(): void + { + $e = new UnsupportedIndexLanguageException( + 'test', + ResourceLanguage::of('de') + ); + $this->assertEquals( + ResourceLanguage::of('de'), + $e->getLang(), + 'unexpected index' + ); + } +} diff --git a/test/Service/Indexer/InternalResourceIndexerTest.php b/test/Service/Indexer/InternalResourceIndexerTest.php index 2f884ce..b55cf3f 100644 --- a/test/Service/Indexer/InternalResourceIndexerTest.php +++ b/test/Service/Indexer/InternalResourceIndexerTest.php @@ -8,6 +8,7 @@ use Atoolo\Resource\ResourceLoader; use Atoolo\Resource\ResourceLocation; use Atoolo\Search\Dto\Indexer\IndexerConfiguration; +use Atoolo\Search\Exception\UnsupportedIndexLanguageException; use Atoolo\Search\Service\Indexer\DocumentEnricher; use Atoolo\Search\Service\Indexer\IndexerConfigurationLoader; use Atoolo\Search\Service\Indexer\IndexerProgressHandler; @@ -117,6 +118,13 @@ public function setUp(): void if ($lang->code === 'en') { return 'test-en_US'; } + if ($lang->code === 'fr') { + throw new UnsupportedIndexLanguageException( + 'test', + $lang, + 'unsupported language' + ); + } return 'test'; }); $this->solrIndexService->method('updater') diff --git a/test/Service/ResourceChannelBasedIndexNameTest.php b/test/Service/ResourceChannelBasedIndexNameTest.php index 84a501b..9f5b1dd 100644 --- a/test/Service/ResourceChannelBasedIndexNameTest.php +++ b/test/Service/ResourceChannelBasedIndexNameTest.php @@ -6,6 +6,7 @@ use Atoolo\Resource\ResourceChannel; use Atoolo\Resource\ResourceLanguage; +use Atoolo\Search\Exception\UnsupportedIndexLanguageException; use Atoolo\Search\Service\ResourceChannelBasedIndexName; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\TestCase; @@ -58,12 +59,8 @@ public function testNameWithLang(): void public function testNameWithUnsupportedLang(): void { - $this->assertEquals( - 'test', - $this->indexName->name(ResourceLanguage::of('it')), - 'The default index name should be returned ' . - 'if the language is not supported' - ); + $this->expectException(UnsupportedIndexLanguageException::class); + $this->indexName->name(ResourceLanguage::of('it')); } public function testNames(): void