Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IBX-1085: Skipped schema generation for invalid Image Variation #108

Merged
merged 8 commits into from
Oct 22, 2021
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
27 changes: 21 additions & 6 deletions src/Schema/Domain/ImageVariationDomain.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,43 @@
use EzSystems\EzPlatformGraphQL\Schema\Builder;
use EzSystems\EzPlatformGraphQL\Schema\Domain;
use Generator;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Psr\Log\NullLogger;

/**
* 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 \EzSystems\EzPlatformGraphQL\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)) {
$this->logger->warning("Skipped schema generation for Image Variation with identifier '$identifier'. Please rename given image variation according to GraphQL specification (http://spec.graphql.org/June2018/#sec-Names)");
adamwojs marked this conversation as resolved.
Show resolved Hide resolved
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;
adamwojs marked this conversation as resolved.
Show resolved Hide resolved
adamwojs marked this conversation as resolved.
Show resolved Hide resolved
}
}
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