Skip to content

Commit

Permalink
IBX-190: Added try/catch block for methods using SearchService::findC…
Browse files Browse the repository at this point in the history
…ontent
  • Loading branch information
ciastektk committed Aug 20, 2021
1 parent 809c71d commit e979082
Showing 1 changed file with 33 additions and 24 deletions.
57 changes: 33 additions & 24 deletions src/lib/Storage/ContentDataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace Ibexa\Personalization\Storage;

use eZ\Publish\API\Repository\ContentService;
use eZ\Publish\API\Repository\Exceptions\InvalidArgumentException;
use eZ\Publish\API\Repository\Exceptions\NotFoundException;
use eZ\Publish\API\Repository\Exceptions\UnauthorizedException;
use eZ\Publish\API\Repository\SearchService;
Expand All @@ -22,59 +23,67 @@
use Ibexa\Personalization\Value\Storage\Item;
use Ibexa\Personalization\Value\Storage\ItemList;
use Ibexa\Personalization\Value\Storage\ItemType;
use Psr\Log\LoggerInterface;

final class ContentDataSource implements DataSourceInterface
{
private SearchService $searchService;

private ContentService $contentService;

private QueryType $queryType;

private DataResolverInterface $dataResolver;
private LoggerInterface $logger;

public function __construct(
SearchService $searchService,
ContentService $contentService,
QueryType $queryType,
DataResolverInterface $dataResolver
DataResolverInterface $dataResolver,
LoggerInterface $logger
) {
$this->searchService = $searchService;
$this->contentService = $contentService;
$this->queryType = $queryType;
$this->dataResolver = $dataResolver;
$this->logger = $logger;
}

/**
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
*/
public function countItems(CriteriaInterface $criteria): int
{
$query = $this->queryType->getQuery(['criteria' => $criteria]);
$query->limit = 0;
$languageFilter = ['languages' => $criteria->getLanguages()];

return $this->searchService->findContent($query, $languageFilter)->totalCount ?? 0;
try {
$query = $this->queryType->getQuery(['criteria' => $criteria]);
$query->limit = 0;
$languageFilter = ['languages' => $criteria->getLanguages()];

return $this->searchService->findContent($query, $languageFilter)->totalCount ?? 0;
} catch (NotFoundException $exception) {
$this->logger->error($exception->getMessage());
return 0;
}
}

/**
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
*/
public function fetchItems(CriteriaInterface $criteria): iterable
{
$query = $this->queryType->getQuery(['criteria' => $criteria]);
$query->performCount = false;
$query->limit = $criteria->getLimit();
$query->offset = $criteria->getOffset();
$languageFilter = ['languages' => $criteria->getLanguages()];

$items = [];

foreach ($this->searchService->findContent($query, $languageFilter) as $hit) {
$items[] = $this->createItem($hit->valueObject);
try {
$query = $this->queryType->getQuery(['criteria' => $criteria]);
$query->performCount = false;
$query->limit = $criteria->getLimit();
$query->offset = $criteria->getOffset();
$languageFilter = ['languages' => $criteria->getLanguages()];

$items = [];

foreach ($this->searchService->findContent($query, $languageFilter) as $hit) {
$items[] = $this->createItem($hit->valueObject);
}

return new ItemList($items);
} catch (NotFoundException | InvalidArgumentException $exception) {
$this->logger->error($exception->getMessage());
return new ItemList([]);
}

return new ItemList($items);
}

public function fetchItem(string $id, string $language): ItemInterface
Expand Down

0 comments on commit e979082

Please sign in to comment.