Skip to content

Commit

Permalink
Improved the LocationGuesser API to be usable for relations as well
Browse files Browse the repository at this point in the history
Two ItemFactory instances are exposed, depending on the need:
- $siteItemFactory: locations and aliases are within the current site root only
- $relatedItemFactory: locations and aliases can be in other public siteaccesses / site roots
  • Loading branch information
Bertrand Dunogier committed Nov 27, 2020
1 parent 26ee4f0 commit 6eb2633
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace EzSystems\EzPlatformGraphQL\GraphQL\Resolver\LocationGuesser;

use eZ\Publish\API\Repository\LocationService;
use eZ\Publish\API\Repository\Values\Content\Content;

/**
* Returns all the locations the current user has access to.
*/
class AllAllowedLocationProvider implements LocationProvider
{
/**
* @var \eZ\Publish\API\Repository\LocationService
*/
private $locationService;

public function __construct(LocationService $locationService)
{
$this->locationService = $locationService;
}

public function getLocations(Content $content): LocationList
{
$list = new LocationList($content);

foreach ($this->locationService->loadLocations($content->contentInfo) as $location) {
$list->addLocation($location);
}

return $list;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace EzSystems\EzPlatformGraphQL\GraphQL\Resolver\LocationGuesser;

use eZ\Publish\API\Repository\SearchService;
use eZ\Publish\API\Repository\Values\Content\Content;
use eZ\Publish\API\Repository\Values\Content\LocationQuery;
use eZ\Publish\API\Repository\Values\Content\Query\Criterion;
use EzSystems\EzPlatformGraphQL\GraphQL\InputMapper\ContentCollectionFilterBuilder;

/**
* Returns the locations from the current site (e.g. within its tree root)
*/
class CurrentSiteLocationProvider implements LocationProvider
{
/**
* @var \eZ\Publish\API\Repository\SearchService
*/
private $searchService;
/**
* @var \EzSystems\EzPlatformGraphQL\GraphQL\InputMapper\ContentCollectionFilterBuilder
*/
private $filterBuilder;

public function __construct(SearchService $searchService, ContentCollectionFilterBuilder $filterBuilder)
{
$this->searchService = $searchService;
$this->filterBuilder = $filterBuilder;
}

public function getLocations(Content $content): LocationList
{
$query = new LocationQuery([
'filter' => new Criterion\LogicalAnd([
$this->filterBuilder->buildFilter(),
new Criterion\ContentId($content->id)
])
]);

$list = new LocationList($content);
foreach ($this->searchService->findLocations($query)->searchHits as $searchHit) {
$list->addLocation($searchHit->valueObject);
}

return $list;
}
}
21 changes: 8 additions & 13 deletions src/GraphQL/Resolver/LocationGuesser/FilterLocationGuesser.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace EzSystems\EzPlatformGraphQL\GraphQL\Resolver\LocationGuesser;

use eZ\Publish\API\Repository\LocationService;
use eZ\Publish\API\Repository\Values\Content\Content;
use eZ\Publish\API\Repository\Values\Content\Location;

/**
* Guesses a location based on voters.
* Guesses locations for a site by filtering out a provided list.
*/
class FilterLocationGuesser implements LocationGuesser
{
Expand All @@ -21,29 +23,22 @@ class FilterLocationGuesser implements LocationGuesser
private $filters;

/**
* @var \eZ\Publish\API\Repository\LocationService
* @var \EzSystems\EzPlatformGraphQL\GraphQL\Resolver\LocationGuesser\LocationProvider
*/
private $locationService;
private $provider;

/**
* @param LocationFilter[] $filters
*/
public function __construct(LocationService $locationService, array $filters)
public function __construct(LocationProvider $provider, array $filters)
{
$this->provider = $provider;
$this->filters = $filters;
$this->locationService = $locationService;
}

/**
* {@inheritdoc}
*/
public function guessLocation(Content $content): LocationGuess
{
$locationList = new LocationList($content);
foreach ($this->locationService->loadLocations($content->contentInfo) as $location) {
$locationList->addLocation($location);
}

$locationList = $this->provider->getLocations($content);
foreach ($this->filters as $filter) {
$filter->filter($content, $locationList);
if ($locationList->hasOneLocation()) {
Expand Down
1 change: 0 additions & 1 deletion src/GraphQL/Resolver/LocationGuesser/LocationGuesser.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
namespace EzSystems\EzPlatformGraphQL\GraphQL\Resolver\LocationGuesser;

use eZ\Publish\API\Repository\Values\Content\Content;
use eZ\Publish\API\Repository\Values\Content\Location;

interface LocationGuesser
{
Expand Down
14 changes: 14 additions & 0 deletions src/GraphQL/Resolver/LocationGuesser/LocationProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/

namespace EzSystems\EzPlatformGraphQL\GraphQL\Resolver\LocationGuesser;

use eZ\Publish\API\Repository\Values\Content\Content;

interface LocationProvider
{
public function getLocations(Content $content): LocationList;
}
6 changes: 2 additions & 4 deletions src/GraphQL/Resolver/RelationFieldResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ final class RelationFieldResolver
/** @var \EzSystems\EzPlatformGraphQL\GraphQL\ItemFactory */
private $itemFactory;

public function __construct(ContentLoader $contentLoader, ItemFactory $itemFactory)
public function __construct(ContentLoader $contentLoader, ItemFactory $relatedContentItemFactory)
{
$this->contentLoader = $contentLoader;
$this->itemFactory = $itemFactory;
$this->itemFactory = $relatedContentItemFactory;
}

public function resolveRelationFieldValue(Field $field, $multiple = false)
Expand All @@ -35,8 +35,6 @@ public function resolveRelationFieldValue(Field $field, $multiple = false)
return $multiple ? [] : null;
}

// @todo do we want to restrict results to the current siteaccess (tree root) ?
// What if the user has access to locations from other siteaccesses ?
$contentItems = $this->contentLoader->find(new Query(
['filter' => new Query\Criterion\ContentId($destinationContentIds)]
));
Expand Down
16 changes: 13 additions & 3 deletions src/Resources/config/services/location_guesser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,20 @@ services:

EzSystems\EzPlatformGraphQL\GraphQL\Resolver\LocationGuesser\MainLocationFilter: ~

EzSystems\EzPlatformGraphQL\GraphQL\Resolver\LocationGuesser\LocationGuesser:
class: 'EzSystems\EzPlatformGraphQL\GraphQL\Resolver\LocationGuesser\FilterLocationGuesser'
EzSystems\EzPlatformGraphQL\GraphQL\Resolver\LocationGuesser\CurrentSiteLocationProvider: ~

EzSystems\EzPlatformGraphQL\GraphQL\Resolver\LocationGuesser\AllAllowedLocationProvider: ~

EzSystems\EzPlatformGraphQL\GraphQL\Resolver\LocationGuesser\LocationGuesser\CurrentSiteContent:
class: EzSystems\EzPlatformGraphQL\GraphQL\Resolver\LocationGuesser\FilterLocationGuesser
arguments:
$provider: '@EzSystems\EzPlatformGraphQL\GraphQL\Resolver\LocationGuesser\CurrentSiteLocationProvider'
$filters:
- '@EzSystems\EzPlatformGraphQL\GraphQL\Resolver\LocationGuesser\TreeRootLocationFilter'
- '@EzSystems\EzPlatformGraphQL\GraphQL\Resolver\LocationGuesser\MainLocationFilter'

EzSystems\EzPlatformGraphQL\GraphQL\Resolver\LocationGuesser\LocationGuesser\RelatedContent:
class: EzSystems\EzPlatformGraphQL\GraphQL\Resolver\LocationGuesser\FilterLocationGuesser
arguments:
$provider: '@EzSystems\EzPlatformGraphQL\GraphQL\Resolver\LocationGuesser\AllAllowedLocationProvider'
$filters:
- '@EzSystems\EzPlatformGraphQL\GraphQL\Resolver\LocationGuesser\MainLocationFilter'
14 changes: 13 additions & 1 deletion src/Resources/config/services/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,19 @@ services:
arguments:
$siteAccessGroups: '%ezpublish.siteaccess.groups%'

EzSystems\EzPlatformGraphQL\GraphQL\ItemFactory: ~
EzSystems\EzPlatformGraphQL\GraphQL\ItemFactory $currentSiteItemFactory: '@EzSystems\EzPlatformGraphQL\GraphQL\ItemFactory\CurrentSite'

EzSystems\EzPlatformGraphQL\GraphQL\ItemFactory $relatedContentItemFactory: '@EzSystems\EzPlatformGraphQL\GraphQL\ItemFactory\RelatedContent'

EzSystems\EzPlatformGraphQL\GraphQL\ItemFactory\CurrentSite:
class: 'EzSystems\EzPlatformGraphQL\GraphQL\ItemFactory'
arguments:
$locationGuesser: '@EzSystems\EzPlatformGraphQL\GraphQL\Resolver\LocationGuesser\LocationGuesser\CurrentSiteContent'

EzSystems\EzPlatformGraphQL\GraphQL\ItemFactory\RelatedContent:
class: 'EzSystems\EzPlatformGraphQL\GraphQL\ItemFactory'
arguments:
$locationGuesser: '@EzSystems\EzPlatformGraphQL\GraphQL\Resolver\LocationGuesser\LocationGuesser\RelatedContent'

EzSystems\EzPlatformGraphQL\GraphQL\Resolver\SiteaccessGuesser\SiteaccessGuesser:
arguments:
Expand Down

0 comments on commit 6eb2633

Please sign in to comment.