Skip to content

Commit

Permalink
Merge pull request #38 from wilr/wilr-patch-1
Browse files Browse the repository at this point in the history
Catch exception when no keys
  • Loading branch information
wilr authored Mar 7, 2021
2 parents bcaf860 + 053fcc6 commit e339b93
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions src/Service/AlgoliaService.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use SilverStripe\Control\Director;
use SilverStripe\Core\Injector\Injectable;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Dev\Debug;
use SilverStripe\Security\Security;

class AlgoliaService
{
Expand Down Expand Up @@ -56,10 +58,30 @@ public function getClient()
*/
public function initIndexes($item = null)
{
if (!Security::database_is_ready()) {
return [];
}

try {
$client = $this->getClient();

if (!$client) {
return [];
}
} catch (Exception $e) {
Injector::inst()->create(LoggerInterface::class)->error($e);

if (Director::isDev()) {
Debug::message($e->getMessage());
}

return [];
}

if (!$item) {
return array_map(
function ($indexName) {
return $this->getClient()->initIndex($this->environmentizeIndex($indexName));
function ($indexName) use ($client) {
return $client->initIndex($this->environmentizeIndex($indexName));
},
array_keys($this->indexes)
);
Expand Down Expand Up @@ -90,7 +112,7 @@ function ($indexName) {
$output = [];

foreach ($matches as $index) {
$output[$index] = $this->getClient()->initIndex($this->environmentizeIndex($index));
$output[$index] = $client->initIndex($this->environmentizeIndex($index));
}

return $output;
Expand Down

0 comments on commit e339b93

Please sign in to comment.