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
Prev Previous commit
Next Next commit
IBX-1085: Skipped schema generation for invalid Image Variation (acco…
…rding to GraphQL spec)
  • Loading branch information
adamwojs committed Oct 18, 2021
commit 46a861c5e162e63da5204041fea3ed09914d41dc
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)");
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 EzSystems\EzPlatformGraphQL\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;
}
}