-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Re-architectured the location guesser API
- Loading branch information
Bertrand Dunogier
committed
Nov 24, 2020
1 parent
4317f51
commit 84392e6
Showing
16 changed files
with
382 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?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\Exception; | ||
|
||
use Exception; | ||
use eZ\Publish\API\Repository\Values\Content\Content; | ||
|
||
class NoValidLocationsException extends Exception | ||
{ | ||
/** | ||
* @var \eZ\Publish\API\Repository\Values\Content\Content|\eZ\Publish\API\Repository\Values\Content\Content[] | ||
*/ | ||
private $content; | ||
|
||
/** | ||
* NoValidLocationsException constructor. | ||
* | ||
* @param \eZ\Publish\API\Repository\Values\Content\Content $content | ||
*/ | ||
public function __construct(Content $content) | ||
{ | ||
parent::__construct("No valid location could be determined for content #{$content->id}"); | ||
$this->content = $content; | ||
} | ||
|
||
public function getContent(): Content | ||
{ | ||
return $this->content; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?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; | ||
|
||
use eZ\Publish\API\Repository\Values\Content\Content; | ||
use eZ\Publish\API\Repository\Values\Content\Location; | ||
use EzSystems\EzPlatformGraphQL\GraphQL\Value\Item; | ||
|
||
class ItemFactory | ||
{ | ||
/** | ||
* @var \EzSystems\EzPlatformGraphQL\GraphQL\Resolver\LocationGuesser\LocationGuesser | ||
*/ | ||
private $locationGuesser; | ||
|
||
public function __construct(Resolver\LocationGuesser\LocationGuesser $locationGuesser) | ||
{ | ||
$this->locationGuesser = $locationGuesser; | ||
} | ||
|
||
public function fromContent(Content $content): Item | ||
{ | ||
return new Item($this->locationGuesser, null, $content); | ||
} | ||
|
||
public function fromLocation(Location $location) | ||
{ | ||
return new Item($this->locationGuesser, $location, null); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
src/GraphQL/Resolver/LocationGuesser/FilterLocationGuesser.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?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\LocationService; | ||
use eZ\Publish\API\Repository\Values\Content\Content; | ||
use eZ\Publish\API\Repository\Values\Content\Location; | ||
use EzSystems\EzPlatformGraphQL\Exception\MultipleValidLocationsException; | ||
|
||
/** | ||
* Guesses a location based on voters. | ||
*/ | ||
class FilterLocationGuesser implements LocationGuesser | ||
{ | ||
/** | ||
* @var \EzSystems\EzPlatformGraphQL\GraphQL\Resolver\LocationGuesser\LocationFilter[] | ||
*/ | ||
private $filters; | ||
|
||
/** | ||
* @var \eZ\Publish\API\Repository\LocationService | ||
*/ | ||
private $locationService; | ||
|
||
/** | ||
* @param LocationFilter[] $filters | ||
*/ | ||
public function __construct(LocationService $locationService, array $filters) | ||
{ | ||
$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); | ||
} | ||
|
||
foreach ($this->filters as $filter) { | ||
$filter->filter($content, $locationList); | ||
if ($locationList->hasOneLocation()) { | ||
return new LocationGuess($content, $locationList->getLocations()); | ||
} | ||
} | ||
|
||
return new LocationGuess($content, $locationList->getLocations()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?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; | ||
use eZ\Publish\API\Repository\Values\Content\Location; | ||
|
||
interface LocationFilter | ||
{ | ||
/** | ||
* Given a Content and a LocationList, filters out locations. | ||
* | ||
* @param \eZ\Publish\API\Repository\Values\Content\Content $content | ||
* @param \EzSystems\EzPlatformGraphQL\GraphQL\Resolver\LocationGuesser\LocationList $locationList | ||
*/ | ||
public function filter(Content $content, LocationList $locationList): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,49 @@ | ||
<?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\Values\Content\Content; | ||
use eZ\Publish\API\Repository\Values\Content\Location; | ||
use EzSystems\EzPlatformGraphQL\Exception; | ||
|
||
/** | ||
* The result of the guesser's work. | ||
*/ | ||
class LocationGuess | ||
{ | ||
private $content; | ||
|
||
private $locations; | ||
|
||
public function __construct(Content $content, array $locations) | ||
{ | ||
$this->content = $content; | ||
$this->locations = $locations; | ||
} | ||
|
||
/** | ||
* @var Location | ||
* Returns the location guess result if the guess was successful. | ||
* | ||
* @return \eZ\Publish\API\Repository\Values\Content\Location | ||
* | ||
* @throws \EzSystems\EzPlatformGraphQL\Exception\MultipleValidLocationsException | ||
* @throws \EzSystems\EzPlatformGraphQL\GraphQL\Resolver\LocationGuesser\NoValidLocationsException | ||
*/ | ||
public $location; | ||
public function getLocation(): Location | ||
{ | ||
if (count($this->locations) > 1) { | ||
throw new Exception\MultipleValidLocationsException($this->content, $this->locations); | ||
} else if (count($this->locations) === 0) { | ||
throw new NoValidLocationsException($this->content); | ||
} | ||
|
||
return $this->locations[0]; | ||
} | ||
|
||
public function __construct(Location $location) | ||
public function isSuccessful(): bool | ||
{ | ||
$this->location = $location; | ||
return count($this->locations) === 1; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.