Skip to content

Commit

Permalink
Migrate code to PHP 8.1, remove obsolete code
Browse files Browse the repository at this point in the history
  • Loading branch information
emodric committed Mar 13, 2023
1 parent 03f990b commit 0f4b9f6
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 79 deletions.
3 changes: 3 additions & 0 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
require_once __DIR__ . '/vendor/netgen/layouts-coding-standard/lib/PhpCsFixer/Config.php';

return (new Netgen\Layouts\CodingStandard\PhpCsFixer\Config())
->addRules([
'trailing_comma_in_multiline' => ['elements' => ['arrays', 'arguments', 'match', 'parameters']],
])
->setFinder(
PhpCsFixer\Finder::create()
->exclude(['vendor', 'node_modules'])
Expand Down
7 changes: 2 additions & 5 deletions bundle/Resources/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,17 @@ services:
class: Netgen\Layouts\Ibexa\RelationListQuery\Handler\RelationListQueryHandler
arguments:
- "@ibexa.api.service.location"
- "@netgen_layouts.ibexa.search_service"
- "@netgen_layouts.ibexa.content_provider"
- "@ibexa.config.resolver"
- "@Ibexa\\Core\\Helper\\TranslationHelper"
- "@netgen_layouts.ibexa.search_service"
tags:
- { name: netgen_layouts.query_type_handler, type: ibexa_content_relation_list }

netgen_layouts.reverse_relation_list_query.handler:
class: Netgen\Layouts\Ibexa\RelationListQuery\Handler\ReverseRelationListQueryHandler
arguments:
- "@ibexa.api.service.location"
- "@netgen_layouts.ibexa.content_provider"
- "@ibexa.api.service.content"
- "@netgen_layouts.ibexa.search_service"
- "@netgen_layouts.ibexa.content_provider"
- "@ibexa.config.resolver"
tags:
- { name: netgen_layouts.query_type_handler, type: ibexa_content_reverse_relation_list }
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
}
],
"require": {
"php": "^8.1",
"netgen/layouts-ibexa": "^1.3"
},
"require-dev": {
Expand Down
45 changes: 14 additions & 31 deletions lib/Handler/RelationListQueryHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
use Ibexa\Contracts\Core\Repository\Values\Content\Query\SortClause;
use Ibexa\Contracts\Core\Repository\Values\Content\Search\SearchHit;
use Ibexa\Contracts\Core\Repository\Values\ValueObject;
use Ibexa\Contracts\Core\SiteAccess\ConfigResolverInterface;
use Ibexa\Core\FieldType\RelationList\Value as RelationListValue;
use Ibexa\Core\Helper\TranslationHelper;
use Netgen\Layouts\API\Values\Collection\Query;
use Netgen\Layouts\Collection\QueryType\QueryTypeHandlerInterface;
use Netgen\Layouts\Ibexa\ContentProvider\ContentProviderInterface;
Expand Down Expand Up @@ -57,28 +55,19 @@ final class RelationListQueryHandler implements QueryTypeHandlerInterface
Location::SORT_FIELD_CONTENTOBJECT_ID => SortClause\ContentId::class,
];

private SearchService $searchService;

private ConfigResolverInterface $configResolver;

private TranslationHelper $translationHelper;

public function __construct(
LocationService $locationService,
SearchService $searchService,
ContentProviderInterface $contentProvider,
ConfigResolverInterface $configResolver,
TranslationHelper $translationHelper
private SearchService $searchService,
) {
$this->locationService = $locationService;
$this->searchService = $searchService;
$this->contentProvider = $contentProvider;
$this->configResolver = $configResolver;
$this->translationHelper = $translationHelper;
$this->setLocationService($locationService);
$this->setContentProvider($contentProvider);
}

public function buildParameters(ParameterBuilderInterface $builder): void
{
$advancedGroup = [self::GROUP_ADVANCED];

$builder->add(
'use_current_location',
ParameterType\Compound\BooleanType::class,
Expand Down Expand Up @@ -134,15 +123,15 @@ public function buildParameters(ParameterBuilderInterface $builder): void
ParameterType\BooleanType::class,
[
'default_value' => true,
'groups' => [self::GROUP_ADVANCED],
'groups' => $advancedGroup,
],
);

$builder->add(
'filter_by_content_type',
ParameterType\Compound\BooleanType::class,
[
'groups' => [self::GROUP_ADVANCED],
'groups' => $advancedGroup,
],
);

Expand All @@ -151,7 +140,7 @@ public function buildParameters(ParameterBuilderInterface $builder): void
IbexaParameterType\ContentTypeType::class,
[
'multiple' => true,
'groups' => [self::GROUP_ADVANCED],
'groups' => $advancedGroup,
],
);

Expand All @@ -164,7 +153,7 @@ public function buildParameters(ParameterBuilderInterface $builder): void
'Include content types' => 'include',
'Exclude content types' => 'exclude',
],
'groups' => [self::GROUP_ADVANCED],
'groups' => $advancedGroup,
],
);
}
Expand All @@ -185,10 +174,7 @@ public function getValues(Query $query, int $offset = 0, ?int $limit = null): it
// it can only be disabled if limit is not 0
$locationQuery->performCount = $locationQuery->limit === 0;

$searchResult = $this->searchService->findLocations(
$locationQuery,
['languages' => $this->configResolver->getParameter('languages')],
);
$searchResult = $this->searchService->findLocations($locationQuery);

/** @var \Ibexa\Contracts\Core\Repository\Values\Content\Location[] $locations */
$locations = array_map(
Expand All @@ -213,7 +199,6 @@ public function getCount(Query $query): int

$searchResult = $this->searchService->findLocations(
$this->buildLocationQuery($relatedContentIds, $query, true),
['languages' => $this->configResolver->getParameter('languages')],
);

return $searchResult->totalCount ?? 0;
Expand All @@ -233,7 +218,7 @@ public function isContextual(Query $query): bool
private function sortLocationsByField(
array $relatedContentIds,
array &$locations,
string $sortDirection
string $sortDirection,
): void {
$sortMap = array_flip($relatedContentIds);

Expand All @@ -260,15 +245,13 @@ static function (Location $location1, Location $location2) use ($sortMap, $sortD
*/
private function getRelatedContentIds(Query $query): array
{
$content = $this->getSelectedContent($query, $this->configResolver->getParameter('languages'));
$content = $this->getSelectedContent($query);

if ($content === null) {
return [];
}

$fieldDefinitionIdentifier = $query->getParameter('field_definition_identifier')->getValue();
$field = $this->translationHelper->getTranslatedField($content, $fieldDefinitionIdentifier);

$field = $content->getField($query->getParameter('field_definition_identifier')->getValue());
if ($field === null || !$field->value instanceof RelationListValue) {
return [];
}
Expand All @@ -286,7 +269,7 @@ private function buildLocationQuery(
Query $query,
bool $buildCountQuery = false,
int $offset = 0,
?int $limit = null
?int $limit = null,
): LocationQuery {
$locationQuery = new LocationQuery();
$sortType = $query->getParameter('sort_type')->getValue() ?? 'default';
Expand Down
58 changes: 20 additions & 38 deletions lib/Handler/ReverseRelationListQueryHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use Ibexa\Contracts\Core\Repository\Values\Content\Query\SortClause;
use Ibexa\Contracts\Core\Repository\Values\Content\Search\SearchHit;
use Ibexa\Contracts\Core\Repository\Values\ValueObject;
use Ibexa\Contracts\Core\SiteAccess\ConfigResolverInterface;
use Netgen\Layouts\API\Values\Collection\Query;
use Netgen\Layouts\Collection\QueryType\QueryTypeHandlerInterface;
use Netgen\Layouts\Ibexa\ContentProvider\ContentProviderInterface;
Expand Down Expand Up @@ -42,28 +41,20 @@ final class ReverseRelationListQueryHandler implements QueryTypeHandlerInterface
'content_name' => SortClause\ContentName::class,
];

private ContentService $contentService;

private SearchService $searchService;

private ConfigResolverInterface $configResolver;

public function __construct(
LocationService $locationService,
ContentService $contentService,
SearchService $searchService,
ContentProviderInterface $contentProvider,
ConfigResolverInterface $configResolver
private ContentService $contentService,
private SearchService $searchService,
) {
$this->contentService = $contentService;
$this->searchService = $searchService;
$this->contentProvider = $contentProvider;
$this->configResolver = $configResolver;
$this->locationService = $locationService;
$this->setLocationService($locationService);
$this->setContentProvider($contentProvider);
}

public function buildParameters(ParameterBuilderInterface $builder): void
{
$advancedGroup = [self::GROUP_ADVANCED];

$builder->add(
'use_current_location',
ParameterType\Compound\BooleanType::class,
Expand Down Expand Up @@ -109,7 +100,7 @@ public function buildParameters(ParameterBuilderInterface $builder): void
'filter_by_content_type',
ParameterType\Compound\BooleanType::class,
[
'groups' => [self::GROUP_ADVANCED],
'groups' => $advancedGroup,
],
);

Expand All @@ -118,7 +109,7 @@ public function buildParameters(ParameterBuilderInterface $builder): void
IbexaParameterType\ContentTypeType::class,
[
'multiple' => true,
'groups' => [self::GROUP_ADVANCED],
'groups' => $advancedGroup,
],
);

Expand All @@ -131,7 +122,7 @@ public function buildParameters(ParameterBuilderInterface $builder): void
'Include content types' => 'include',
'Exclude content types' => 'exclude',
],
'groups' => [self::GROUP_ADVANCED],
'groups' => $advancedGroup,
],
);

Expand All @@ -140,7 +131,7 @@ public function buildParameters(ParameterBuilderInterface $builder): void
ParameterType\TextLineType::class,
[
'required' => false,
'groups' => [self::GROUP_ADVANCED],
'groups' => $advancedGroup,
],
);

Expand All @@ -149,7 +140,7 @@ public function buildParameters(ParameterBuilderInterface $builder): void
ParameterType\BooleanType::class,
[
'default_value' => true,
'groups' => [self::GROUP_ADVANCED],
'groups' => $advancedGroup,
],
);
}
Expand All @@ -168,10 +159,7 @@ public function getValues(Query $query, int $offset = 0, ?int $limit = null): it
// it can only be disabled if limit is not 0
$locationQuery->performCount = $locationQuery->limit === 0;

$searchResult = $this->searchService->findLocations(
$locationQuery,
['languages' => $this->configResolver->getParameter('languages')],
);
$searchResult = $this->searchService->findLocations($locationQuery);

/** @var \Ibexa\Contracts\Core\Repository\Values\Content\Location[] $locations */
$locations = array_map(
Expand All @@ -184,20 +172,14 @@ public function getValues(Query $query, int $offset = 0, ?int $limit = null): it

public function getCount(Query $query): int
{
$content = $this->getSelectedContent($query, $this->configResolver->getParameter('languages'));
if ($content === null) {
return 0;
}

$relatedContentIds = $this->getReverseRelatedContentIds($query);
$reverseRelatedContentIds = $this->getReverseRelatedContentIds($query);

if (count($relatedContentIds) === 0) {
if (count($reverseRelatedContentIds) === 0) {
return 0;
}

$searchResult = $this->searchService->findLocations(
$this->buildLocationQuery($relatedContentIds, $query, true),
['languages' => $this->configResolver->getParameter('languages')],
$this->buildLocationQuery($reverseRelatedContentIds, $query, true),
);

return $searchResult->totalCount ?? 0;
Expand All @@ -215,15 +197,15 @@ public function isContextual(Query $query): bool
*/
private function getReverseRelatedContentIds(Query $query): array
{
$content = $this->getSelectedContent($query, $this->configResolver->getParameter('languages'));
$content = $this->getSelectedContent($query);

if ($content === null) {
return [];
}

$reverseRelations = $this->contentService->loadReverseRelations($content->contentInfo);
$contentIds = [];
foreach ($reverseRelations as $relation) {

foreach ($this->contentService->loadReverseRelations($content->contentInfo) as $relation) {
$contentIds[] = $relation->getSourceContentInfo()->id;
}

Expand All @@ -240,7 +222,7 @@ private function buildLocationQuery(
Query $query,
bool $buildCountQuery = false,
int $offset = 0,
?int $limit = null
?int $limit = null,
): LocationQuery {
$locationQuery = new LocationQuery();
$sortType = $query->getParameter('sort_type')->getValue() ?? 'default';
Expand Down Expand Up @@ -271,7 +253,7 @@ private function buildLocationQuery(
}

$fieldDefinitionIdentifier = $query->getParameter('field_definition_identifier')->getValue();
$selectedContent = $this->getSelectedContent($query, $this->configResolver->getParameter('languages'));
$selectedContent = $this->getSelectedContent($query);

if ($fieldDefinitionIdentifier !== null && $selectedContent !== null) {
$criteria[] = new Criterion\Field(
Expand Down
18 changes: 13 additions & 5 deletions lib/Handler/Traits/SelectedContentTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@ trait SelectedContentTrait

/**
* Returns the selected Content item.
*
* @param string[] $languages
*/
private function getSelectedContent(Query $query, ?array $languages = null): ?Content
private function getSelectedContent(Query $query): ?Content
{
if ($query->getParameter('use_current_location')->getValue() === true) {
return $this->contentProvider->provideContent();
Expand All @@ -33,9 +31,19 @@ private function getSelectedContent(Query $query, ?array $languages = null): ?Co
}

try {
return $this->locationService->loadLocation((int) $locationId, $languages)->getContent();
} catch (Throwable $t) {
return $this->locationService->loadLocation((int) $locationId)->getContent();
} catch (Throwable) {
return null;
}
}

private function setLocationService(LocationService $locationService): void
{
$this->locationService = $locationService;
}

private function setContentProvider(ContentProviderInterface $contentProvider): void
{
$this->contentProvider = $contentProvider;
}
}

0 comments on commit 0f4b9f6

Please sign in to comment.