Skip to content

Commit

Permalink
Used SortingDefinitionRegistry
Browse files Browse the repository at this point in the history
  • Loading branch information
kisztof committed Dec 28, 2023
1 parent 9735882 commit da74366
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,11 @@

namespace Ibexa\Search\EventDispatcher\EventListener;

use Ibexa\Bundle\Core\ApiLoader\RepositoryConfigurationProvider;
use Ibexa\Contracts\Core\Exception\InvalidArgumentException;
use Ibexa\Contracts\Core\Repository\Values\Content\Query;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\SortClause\ContentName;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\SortClause\ContentTranslatedName;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\SortClause\DateModified;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\SortClause\Score;
use Ibexa\Contracts\Search\Event\BuildSuggestionCollectionEvent;
use Ibexa\Contracts\Search\Mapper\SearchHitToContentSuggestionMapperInterface;
use Ibexa\Contracts\Search\SortingDefinition\SortingDefinitionRegistryInterface;
use Ibexa\Core\Repository\SiteAccessAware\SearchService;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
Expand All @@ -26,20 +22,20 @@ final class ContentSuggestionSubscriber implements EventSubscriberInterface, Log
{
use LoggerAwareTrait;

private RepositoryConfigurationProvider $configurationProvider;

private SearchService $searchService;

private SearchHitToContentSuggestionMapperInterface $contentSuggestionMapper;

private SortingDefinitionRegistryInterface $sortingDefinitionRegistry;

public function __construct(
RepositoryConfigurationProvider $configurationProvider,
SearchService $searchService,
SearchHitToContentSuggestionMapperInterface $contentSuggestionMapper
SearchHitToContentSuggestionMapperInterface $contentSuggestionMapper,
SortingDefinitionRegistryInterface $sortingDefinitionRegistry
) {
$this->configurationProvider = $configurationProvider;
$this->searchService = $searchService;
$this->contentSuggestionMapper = $contentSuggestionMapper;
$this->sortingDefinitionRegistry = $sortingDefinitionRegistry;
}

public static function getSubscribedEvents(): array
Expand Down Expand Up @@ -78,41 +74,15 @@ public function onBuildSuggestionCollectionEvent(
return $event;
}

/**
* @return \Ibexa\Contracts\Core\Repository\Values\Content\Query\SortClause[]
*/
private function getSortClauses(): array
{
$sortClauses = [];

if ($this->isLegacySearchEngine()) {
$sortClauses[] = new ContentName(Query::SORT_DESC);
} else {
$sortClauses[] = new ContentTranslatedName(Query::SORT_DESC);
}

if ($this->searchService->supports(SearchService::CAPABILITY_SCORING)) {
$sortClauses[] = new Score(Query::SORT_DESC);
} else {
$sortClauses[] = new DateModified(Query::SORT_DESC);
}

return $sortClauses;
}

private function isLegacySearchEngine(): bool
{
$config = $this->configurationProvider->getRepositoryConfig();

return $config['search']['engine'] === 'legacy';
}

private function getQuery(string $value, int $limit): Query
{
$query = new Query();
$query->query = new Query\Criterion\FullText($value);
$query->limit = $limit;
$query->sortClauses = $this->getSortClauses();
$defaultSortClauses = $this->sortingDefinitionRegistry->getDefaultSortingDefinition();
if ($defaultSortClauses !== null) {
$query->sortClauses = $defaultSortClauses->getSortClauses();
}

return $query;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@
use Ibexa\Contracts\Core\Repository\SearchService as SearchServiceInterface;
use Ibexa\Contracts\Core\Repository\Values\Content\Content;
use Ibexa\Contracts\Core\Repository\Values\Content\Query;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\SortClause\ContentName;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\SortClause\ContentTranslatedName;
use Ibexa\Contracts\Core\Repository\Values\Content\Search\SearchHit;
use Ibexa\Contracts\Core\Repository\Values\Content\Search\SearchResult;
use Ibexa\Contracts\Core\Repository\Values\ContentType\ContentType;
use Ibexa\Contracts\Search\Event\BuildSuggestionCollectionEvent;
use Ibexa\Contracts\Search\Mapper\SearchHitToContentSuggestionMapperInterface;
use Ibexa\Contracts\Search\Model\Suggestion\ContentSuggestion as ContentSuggestionModel;
use Ibexa\Contracts\Search\Model\Suggestion\Suggestion;
use Ibexa\Contracts\Search\SortingDefinition\SortingDefinitionInterface;
use Ibexa\Contracts\Search\SortingDefinition\SortingDefinitionRegistryInterface;
use Ibexa\Core\Repository\SiteAccessAware\SearchService;
use Ibexa\Core\Repository\Values\Content\Location;
use Ibexa\Search\EventDispatcher\EventListener\ContentSuggestionSubscriber;
Expand Down Expand Up @@ -104,8 +108,9 @@ public function testOnContentSuggestionWithException(): void

$searchService = $this->getSearchServiceMockWithException();
$mapper = $this->getSearchHitToContentSuggestionMapperMock();
$sortingDefinitionRegistry = $this->getSortingDefinitionRegistryMock();

$subscriber = new ContentSuggestionSubscriber($this->configProviderMock, $searchService, $mapper);
$subscriber = new ContentSuggestionSubscriber($searchService, $mapper, $sortingDefinitionRegistry);
$subscriber->setLogger($this->loggerMock);

$event = new BuildSuggestionCollectionEvent($query);
Expand All @@ -123,7 +128,9 @@ private function performTestOnContentSuggestion(): void
$searchService = $this->getSearchServiceMock();
$mapper = $this->getSearchHitToContentSuggestionMapperMock();

$subscriber = new ContentSuggestionSubscriber($this->configProviderMock, $searchService, $mapper);
$sortingDefinitionRegistry = $this->getSortingDefinitionRegistryMock();

$subscriber = new ContentSuggestionSubscriber($searchService, $mapper, $sortingDefinitionRegistry);

$event = new BuildSuggestionCollectionEvent($query);
$subscriber->onBuildSuggestionCollectionEvent($event);
Expand Down Expand Up @@ -159,15 +166,17 @@ private function getSearchServiceMockWithException(): SearchService
$searchServiceMock = $this->createMock(SearchService::class);
$searchServiceMock
->method('findContent')
->willThrowException(new InvalidArgumentException(
'$item',
sprintf(
'Argument 1 passed to %s() must be an instance of %s, %s given',
'SuggestionCollection::append',
Suggestion::class,
get_debug_type('type'),
->willThrowException(
new InvalidArgumentException(
'$item',
sprintf(
'Argument 1 passed to %s() must be an instance of %s, %s given',
'SuggestionCollection::append',
Suggestion::class,
get_debug_type('type'),
)
)
));
);

return $searchServiceMock;
}
Expand All @@ -193,4 +202,36 @@ private function getSearchHitToContentSuggestionMapperMock(): SearchHitToContent

return $searchHitToContentSuggestionMapperMock;
}

private function getSortingDefinitionRegistryMock(): SortingDefinitionRegistryInterface
{
$sortingDefinitionRegistryMock = $this->createMock(SortingDefinitionRegistryInterface::class);

$sortingDefinitionRegistryMock
->method('getDefaultSortingDefinition')
->willReturn($this->getSortingDefinitionMock());

return $sortingDefinitionRegistryMock;
}

private function getSortingDefinitionMock(): SortingDefinitionInterface
{
$sortingDefinitionMock = $this->createMock(SortingDefinitionInterface::class);
$sortingDefinitionMock
->method('getSortClauses')
->willReturn($this->getSortClauses());

return $sortingDefinitionMock;
}

/**
* @return array<\Ibexa\Contracts\Core\Repository\Values\Content\Query\SortClause>
*/
private function getSortClauses(): array
{
return [
new ContentTranslatedName(Query::SORT_ASC),
new ContentName(Query::SORT_DESC),
];
}
}

0 comments on commit da74366

Please sign in to comment.