Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
kisztof committed Oct 24, 2023
1 parent b1689ab commit 6efdc5b
Show file tree
Hide file tree
Showing 25 changed files with 476 additions and 120 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function __construct(int $defaultLimit)
$this->defaultLimit = $defaultLimit;
}

public function apply(Request $request, ParamConverter $configuration)
public function apply(Request $request, ParamConverter $configuration): bool
{
$query = $request->get('query');
$limit = $request->query->getInt('limit', $this->defaultLimit);
Expand All @@ -31,9 +31,11 @@ public function apply(Request $request, ParamConverter $configuration)
$suggestionQuery = new SuggestionQuery($query, $limit, $language);

$request->attributes->set($configuration->getName(), $suggestionQuery);

return true;
}

public function supports(ParamConverter $configuration)
public function supports(ParamConverter $configuration): bool
{
return SuggestionQuery::class === $configuration->getClass();
}
Expand Down
4 changes: 4 additions & 0 deletions src/bundle/Resources/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,7 @@ services:
$defaultLimit: '%ibexa.site_access.config.default.search.suggestion.min_query_length%'
tags:
- { name: 'request.param_converter', priority: 0 }

Ibexa\Search\Provider\ParentLocationProvider: ~

Ibexa\Contracts\Search\Provider\ParentLocationProvider: '@Ibexa\Search\Provider\ParentLocationProvider'
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
*/
declare(strict_types=1);

namespace Ibexa\Search\EventDispatcher\Event;
namespace Ibexa\Contracts\Search\Event;

use Ibexa\Search\Model\Suggestion\SuggestionCollection;
use Ibexa\Search\Model\SuggestionQuery;

abstract class AbstractSuggestion
final class SuggestionEvent
{
private SuggestionCollection $suggestionCollection;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
*/
declare(strict_types=1);

namespace Ibexa\Search\Model\Suggestion;
namespace Ibexa\Contracts\Search\Model\Suggestion;

use Ibexa\Contracts\Core\Repository\Values\ValueObject;
use Ibexa\Search\Model\Suggestion\ParentLocationCollection;

abstract class Suggestion extends ValueObject
{
Expand All @@ -18,22 +19,21 @@ abstract class Suggestion extends ValueObject

private string $pathString;

/** @var array<int, ?string> */
private array $parentsLocation;
private ParentLocationCollection $parentsLocation;

/**
* @param array<int, ?string> $parentsLocation
* @param array<\Ibexa\Search\Model\Suggestion\ParentLocation> $parentLocations
*/
public function __construct(
float $score,
string $name,
string $pathString = '',
array $parentsLocation = []
array $parentLocations = []
) {
$this->score = $score;
$this->name = $name;
$this->pathString = $pathString;
$this->parentsLocation = $parentsLocation;
$this->parentsLocation = new ParentLocationCollection($parentLocations);

parent::__construct();
}
Expand All @@ -53,18 +53,10 @@ public function getPathString(): string
return $this->pathString;
}

/**
* @return array<int, ?string>
*/
public function getParentsLocation(): array
public function getParentLocations(): ParentLocationCollection
{
return $this->parentsLocation;
}

public function addPath(int $locationId, string $name): void
{
$this->parentsLocation[$locationId] = $name;
}

abstract public function getType(): string;
}
19 changes: 19 additions & 0 deletions src/contracts/Provider/ParentLocationProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Contracts\Search\Provider;

interface ParentLocationProvider
{
/**
* @param array<int> $parentLocationIds
*
* @return array<\Ibexa\Search\Model\Suggestion\ParentLocation>
*/
public function provide(array $parentLocationIds): array;
}
13 changes: 0 additions & 13 deletions src/lib/EventDispatcher/Event/ContentSuggestion.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,10 @@
namespace Ibexa\Search\EventDispatcher\EventListener;

use Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException;
use Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException;
use Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException;
use Ibexa\Contracts\Core\Repository\LocationService;
use Ibexa\Contracts\Core\Repository\Values\Content\Query;
use Ibexa\Contracts\Search\Event\SuggestionEvent;
use Ibexa\Contracts\Search\Mapper\SearchHitToContentSuggestionMapper;
use Ibexa\Core\Repository\SiteAccessAware\SearchService;
use Ibexa\Search\EventDispatcher\Event\ContentSuggestion;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
Expand All @@ -28,26 +25,22 @@ final class ContentSuggestionSubscriber implements EventSubscriberInterface, Log

private SearchHitToContentSuggestionMapper $contentSuggestionMapper;

private LocationService $locationService;

public function __construct(
SearchService $searchService,
LocationService $locationService,
SearchHitToContentSuggestionMapper $contentSuggestionMapper
) {
$this->searchService = $searchService;
$this->contentSuggestionMapper = $contentSuggestionMapper;
$this->locationService = $locationService;
}

public static function getSubscribedEvents(): array
{
return [
ContentSuggestion::class => 'onContentSuggestion',
SuggestionEvent::class => 'onContentSuggestion',
];
}

public function onContentSuggestion(ContentSuggestion $event): ContentSuggestion
public function onContentSuggestion(SuggestionEvent $event): SuggestionEvent
{
$query = $event->getQuery();

Expand All @@ -65,21 +58,13 @@ public function onContentSuggestion(ContentSuggestion $event): ContentSuggestion
try {
$languageFilter = $language ? ['languages' => [$language]] : [];
$searchResult = $this->searchService->findContent($query, $languageFilter);
$collection = $event->getSuggestionCollection();
$suggestionCollection = $event->getSuggestionCollection();
foreach ($searchResult as $result) {
$mappedResult = $this->contentSuggestionMapper->map($result);
if ($mappedResult === null) {
$contentSuggestion = $this->contentSuggestionMapper->map($result);
if ($contentSuggestion === null) {
continue;
}

foreach ($mappedResult->getParentsLocation() as $locationId => $name) {
try {
$location = $this->locationService->loadLocation($locationId);
$mappedResult->addPath($locationId, $location->getContent()->getName() ?? '');
} catch (NotFoundException|UnauthorizedException $e) {
}
}
$collection->append($mappedResult);
$suggestionCollection->append($contentSuggestion);
}
} catch (InvalidArgumentException $e) {
$this->logger ? $this->logger->error($e) : null;
Expand Down
20 changes: 15 additions & 5 deletions src/lib/Mapper/SearchHitToContentSuggestionMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,22 @@
use Ibexa\Contracts\Core\Repository\Values\Content\Search\SearchHit;
use Ibexa\Contracts\Core\SiteAccess\ConfigResolverInterface;
use Ibexa\Contracts\Search\Mapper\SearchHitToContentSuggestionMapper as SearchHitToContentSuggestionMapperInterface;
use Ibexa\Contracts\Search\Provider\ParentLocationProvider as ParentLocationProviderInterface;
use Ibexa\Core\Repository\Values\Content\Content;
use Ibexa\Search\Model\Suggestion\ContentSuggestion;

final class SearchHitToContentSuggestionMapper implements SearchHitToContentSuggestionMapperInterface
{
private ConfigResolverInterface $configResolver;

public function __construct(ConfigResolverInterface $configResolver)
{
private ParentLocationProviderInterface $parentLocationProvider;

public function __construct(
ParentLocationProviderInterface $parentLocationProvider,
ConfigResolverInterface $configResolver
) {
$this->configResolver = $configResolver;
$this->parentLocationProvider = $parentLocationProvider;
}

public function map(SearchHit $searchHit): ?ContentSuggestion
Expand All @@ -42,16 +48,20 @@ public function map(SearchHit $searchHit): ?ContentSuggestion
$parentsLocation = $mainLocation->path;
$position = array_search((string)$rootLocationId, $parentsLocation);
if ($position !== false) {
$parentsLocation = array_slice($parentsLocation, (int)$position + 1);
$parentsLocation = array_slice($parentsLocation, (int)$position);
}

return new ContentSuggestion(
$parentCollection = $this->parentLocationProvider->provide($parentsLocation);

$suggestion = new ContentSuggestion(
$searchHit->score ?? 50,
$content->getContentType()->identifier,
$content->getName() ?? '',
$content->getVersionInfo()->getContentInfo()->getId(),
implode('/', $parentsLocation),
array_fill_keys($parentsLocation, '')
$parentCollection
);

return $suggestion;
}
}
8 changes: 5 additions & 3 deletions src/lib/Model/Suggestion/ContentSuggestion.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,26 @@

namespace Ibexa\Search\Model\Suggestion;

use Ibexa\Contracts\Search\Model\Suggestion\Suggestion;

final class ContentSuggestion extends Suggestion
{
private int $contentId;

private string $contentTypeIdentifier;

/**
* @param array<int, ?string> $parentLocation
* @param array<\Ibexa\Search\Model\Suggestion\ParentLocation> $parentLocations
*/
public function __construct(
float $score,
string $contentTypeIdentifier,
string $name,
int $contentId,
string $pathString = '',
array $parentLocation = []
array $parentLocations = []
) {
parent::__construct($score, $name, $pathString, $parentLocation);
parent::__construct($score, $name, $pathString, $parentLocations);
$this->contentId = $contentId;
$this->contentTypeIdentifier = $contentTypeIdentifier;
}
Expand Down
40 changes: 40 additions & 0 deletions src/lib/Model/Suggestion/ParentLocation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Search\Model\Suggestion;

final class ParentLocation
{
private int $contentId;

private int $locationId;

private string $name;

public function __construct(int $contentId, int $locationId, string $name)
{
$this->contentId = $contentId;
$this->locationId = $locationId;
$this->name = $name;
}

public function getContentId(): int
{
return $this->contentId;
}

public function getLocationId(): int
{
return $this->locationId;
}

public function getName(): string
{
return $this->name;
}
}
39 changes: 39 additions & 0 deletions src/lib/Model/Suggestion/ParentLocationCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Search\Model\Suggestion;

use Ibexa\Contracts\Core\Collection\MutableArrayList;
use Ibexa\Contracts\Core\Exception\InvalidArgumentException;
use Ibexa\Contracts\Search\Model\Suggestion\Suggestion;

/**
* @template-extends \Ibexa\Contracts\Core\Collection\MutableArrayList<\Ibexa\Search\Model\Suggestion\ParentLocation>
*/
final class ParentLocationCollection extends MutableArrayList
{
/**
* @param mixed $item
*/
public function append($item): void
{
if (!$item instanceof ParentLocation) {
throw new InvalidArgumentException(
'$item',
sprintf(
'Argument 1 passed to %s::append() must be an instance of %s, %s given',
__CLASS__,
Suggestion::class,
\is_object($item) ? \get_class($item) : \gettype($item)
)
);
}

parent::append($item);
}
}
3 changes: 2 additions & 1 deletion src/lib/Model/Suggestion/SuggestionCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@

use Ibexa\Contracts\Core\Collection\MutableArrayList;
use Ibexa\Contracts\Core\Exception\InvalidArgumentException;
use Ibexa\Contracts\Search\Model\Suggestion\Suggestion;

/**
* @template-extends \Ibexa\Contracts\Core\Collection\MutableArrayList<\Ibexa\Search\Model\Suggestion\Suggestion>
* @template-extends \Ibexa\Contracts\Core\Collection\MutableArrayList<\Ibexa\Contracts\Search\Model\Suggestion\Suggestion>
*/
final class SuggestionCollection extends MutableArrayList
{
Expand Down
Loading

0 comments on commit 6efdc5b

Please sign in to comment.