Skip to content

Commit

Permalink
refactor: throw UnsupportedIndexLanguageException for unsupported lan…
Browse files Browse the repository at this point in the history
…guage
  • Loading branch information
sitepark-veltrup committed Apr 29, 2024
1 parent 390c685 commit b3a9b32
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 21 deletions.
35 changes: 35 additions & 0 deletions src/Exception/UnsupportedIndexLanguageException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Atoolo\Search\Exception;

use Atoolo\Resource\ResourceLanguage;
use RuntimeException;

class UnsupportedIndexLanguageException extends RuntimeException
{
public function __construct(
private readonly string $index,
private readonly ResourceLanguage $lang,
string $message = "",
int $code = 0,
?\Throwable $previous = null
) {
parent::__construct(
$index . '/' . $lang->code . ': ' . $message,
$code,
$previous
);
}

public function getIndex(): string
{
return $this->index;
}

public function getLang(): ResourceLanguage
{
return $this->lang;
}
}
5 changes: 5 additions & 0 deletions src/Service/IndexName.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down
25 changes: 11 additions & 14 deletions src/Service/Indexer/InternalResourceIndexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
);
}
}

Expand Down
16 changes: 15 additions & 1 deletion src/Service/ResourceChannelBasedIndexName.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Atoolo\Resource\ResourceChannel;
use Atoolo\Resource\ResourceLanguage;
use Atoolo\Search\Exception\UnsupportedIndexLanguageException;

class ResourceChannelBasedIndexName implements IndexName
{
Expand All @@ -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;
}

Expand All @@ -40,6 +46,9 @@ public function names(): array
return $names;
}

/**
* @throws UnsupportedIndexLanguageException
*/
private function langToAvailableLocale(
ResourceChannel $resourceChannel,
ResourceLanguage $lang
Expand All @@ -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
);
}
}
38 changes: 38 additions & 0 deletions test/Exception/UnsupportedIndexLanguageExceptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace Atoolo\Search\Test\Exception;

use Atoolo\Resource\ResourceLanguage;
use Atoolo\Search\Exception\UnsupportedIndexLanguageException;
use PHPUnit\Framework\TestCase;

class UnsupportedIndexLanguageExceptionTest extends TestCase
{
public function testGetIndex(): void
{
$e = new UnsupportedIndexLanguageException(
'test',
ResourceLanguage::of('de')
);
$this->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'
);
}
}
8 changes: 8 additions & 0 deletions test/Service/Indexer/InternalResourceIndexerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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')
Expand Down
9 changes: 3 additions & 6 deletions test/Service/ResourceChannelBasedIndexNameTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b3a9b32

Please sign in to comment.