Skip to content

Commit

Permalink
IBX-1085: Skipped schema generation for invalid Image Variation (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamwojs authored Oct 22, 2021
1 parent 5a0096a commit 7431563
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 7 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
},
"autoload": {
"psr-4": {
"EzSystems\\EzPlatformGraphQL\\": "src"
"EzSystems\\EzPlatformGraphQL\\": "src",
"Ibexa\\GraphQL\\": "src"
}
},
"autoload-dev": {
Expand Down
8 changes: 8 additions & 0 deletions src/Resources/config/services/schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
32 changes: 26 additions & 6 deletions src/Schema/Domain/ImageVariationDomain.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]];
}
}
Expand Down
22 changes: 22 additions & 0 deletions src/Schema/Domain/NameValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?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 Ibexa\GraphQL\Schema\Domain;

/**
* Validates given name according to GraphQL specification. See http://spec.graphql.org/June2018/#sec-Names.
*/
final class NameValidator
{
private const NAME_PATTERN = '/^[_a-zA-Z][_a-zA-Z0-9]*$/';

public function isValidName(string $name): bool
{
return preg_match(self::NAME_PATTERN, $name) === 1;
}
}
2 changes: 2 additions & 0 deletions src/Schema/ImagesVariationsBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

/**
* Generates the ImageVariationIdentifier enum that indexes images variations identifiers.
*
* @deprecated since 1.0, will be removed in 4.0.
*/
class ImagesVariationsBuilder implements SchemaBuilder
{
Expand Down

0 comments on commit 7431563

Please sign in to comment.