From 743156347f6d1894c9b82239c06c24b5e88b51b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20W=C3=B3js?= Date: Fri, 22 Oct 2021 12:03:20 +0200 Subject: [PATCH] IBX-1085: Skipped schema generation for invalid Image Variation (#108) --- composer.json | 3 +- src/Resources/config/services/schema.yml | 8 ++++++ src/Schema/Domain/ImageVariationDomain.php | 32 ++++++++++++++++++---- src/Schema/Domain/NameValidator.php | 22 +++++++++++++++ src/Schema/ImagesVariationsBuilder.php | 2 ++ 5 files changed, 60 insertions(+), 7 deletions(-) create mode 100644 src/Schema/Domain/NameValidator.php diff --git a/composer.json b/composer.json index 09aeca6f..cdbe605c 100644 --- a/composer.json +++ b/composer.json @@ -28,7 +28,8 @@ }, "autoload": { "psr-4": { - "EzSystems\\EzPlatformGraphQL\\": "src" + "EzSystems\\EzPlatformGraphQL\\": "src", + "Ibexa\\GraphQL\\": "src" } }, "autoload-dev": { diff --git a/src/Resources/config/services/schema.yml b/src/Resources/config/services/schema.yml index 99a860d0..fa1bca18 100644 --- a/src/Resources/config/services/schema.yml +++ b/src/Resources/config/services/schema.yml @@ -49,3 +49,11 @@ services: decorates: EzSystems\EzPlatformGraphQL\Schema\Domain\Content\Mapper\FieldDefinition\FieldDefinitionMapper arguments: $innerMapper: '@EzSystems\EzPlatformGraphQL\Schema\Domain\Content\Mapper\FieldDefinition\SelectionFieldDefinitionMapper.inner' + + Ibexa\GraphQL\Schema\Domain\NameValidator: ~ + + EzSystems\EzPlatformGraphQL\Schema\Domain\ImageVariationDomain: + calls: + - method: setLogger + arguments: + - '@logger' diff --git a/src/Schema/Domain/ImageVariationDomain.php b/src/Schema/Domain/ImageVariationDomain.php index fdfdcb53..edb4b4f9 100644 --- a/src/Schema/Domain/ImageVariationDomain.php +++ b/src/Schema/Domain/ImageVariationDomain.php @@ -11,28 +11,48 @@ use EzSystems\EzPlatformGraphQL\Schema\Builder; use EzSystems\EzPlatformGraphQL\Schema\Domain; use Generator; +use Psr\Log\LoggerAwareInterface; +use Psr\Log\LoggerAwareTrait; +use Psr\Log\NullLogger; +use Ibexa\GraphQL\Schema\Domain\NameValidator; /** * Adds configured image variations to the ImageVariationIdentifier type. */ -class ImageVariationDomain implements Domain\Iterator, Schema\Worker +class ImageVariationDomain implements Domain\Iterator, Schema\Worker, LoggerAwareInterface { + use LoggerAwareTrait; + const TYPE = 'ImageVariationIdentifier'; const ARG = 'ImageVariation'; - /** - * @var ConfigResolverInterface - */ + /** @var \eZ\Publish\Core\MVC\ConfigResolverInterface */ private $configResolver; - public function __construct(ConfigResolverInterface $configResolver) - { + /** @var \Ibexa\GraphQL\Schema\Domain\NameValidator */ + private $nameValidator; + + public function __construct( + ConfigResolverInterface $configResolver, + NameValidator $nameValidator + ) { $this->configResolver = $configResolver; + $this->nameValidator = $nameValidator; + $this->logger = new NullLogger(); } public function iterate(): Generator { foreach ($this->configResolver->getParameter('image_variations') as $identifier => $variation) { + if (!$this->nameValidator->isValidName($identifier)) { + $message = "Skipped schema generation for Image Variation with identifier '%s'. " + . 'Please rename given image variation according to GraphQL specification ' + . '(http://spec.graphql.org/June2018/#sec-Names)'; + + $this->logger->warning(sprintf($message, $identifier)); + continue; + } + yield [self::ARG => ['identifier' => $identifier, 'variation' => $variation]]; } } diff --git a/src/Schema/Domain/NameValidator.php b/src/Schema/Domain/NameValidator.php new file mode 100644 index 00000000..4b9070a7 --- /dev/null +++ b/src/Schema/Domain/NameValidator.php @@ -0,0 +1,22 @@ +