Skip to content

Commit

Permalink
decoupled implementation by introducing BaseNameHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
konradoboza committed Mar 28, 2022
1 parent bdcbcd4 commit b377ddf
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 28 deletions.
17 changes: 13 additions & 4 deletions src/bundle/Resources/config/services/schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,6 @@ services:

Ibexa\GraphQL\Schema\Domain\Content\LanguagesIterator: ~

Ibexa\GraphQL\Schema\Domain\Content\NameHelper: ~

Ibexa\GraphQL\Schema\Domain\Content\Pluralizer: ~

Ibexa\GraphQL\Schema\Domain\NameValidator:
calls:
- method: setLogger
Expand All @@ -107,3 +103,16 @@ services:
Ibexa\GraphQL\Schema\ImagesVariationsBuilder: ~

Ibexa\GraphQL\Schema\SchemaGenerator: ~

Ibexa\GraphQL\Schema\Domain\Pluralizer: ~

Ibexa\GraphQL\Schema\Domain\BaseNameHelper:
abstract: true
arguments:
$pluralizer: '@Ibexa\GraphQL\Schema\Domain\Pluralizer'

Ibexa\GraphQL\Schema\Domain\Content\NameHelper:
parent: Ibexa\GraphQL\Schema\Domain\BaseNameHelper
arguments:
$fieldNameOverrides: '%ibexa.graphql.schema.content.field_name.override%'
$pluralizer: '@Ibexa\GraphQL\Schema\Domain\Pluralizer'
4 changes: 0 additions & 4 deletions src/bundle/Resources/config/services/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,3 @@ services:
$configResolver: '@ibexa.config.resolver'
$provider: '@ibexa.siteaccess.provider'
$siteAccessGroups: '%ibexa.site_access.groups%'

Ibexa\GraphQL\Schema\Domain\Content\NameHelper:
arguments:
$fieldNameOverrides: '%ibexa.graphql.schema.content.field_name.override%'
32 changes: 32 additions & 0 deletions src/lib/Schema/Domain/BaseNameHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace Ibexa\GraphQL\Schema\Domain;

use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;

abstract class BaseNameHelper
{
private CamelCaseToSnakeCaseNameConverter $caseConverter;

private Pluralizer $pluralizer;

public function __construct(Pluralizer $pluralizer)
{
$this->caseConverter = new CamelCaseToSnakeCaseNameConverter(null, false);
$this->pluralizer = $pluralizer;
}

public function toCamelCase(string $string): string
{
return $this->caseConverter->denormalize($string);
}

public function pluralize(string $name): string
{
return $this->pluralizer->pluralize($name);
}
}
26 changes: 7 additions & 19 deletions src/lib/Schema/Domain/Content/NameHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,32 @@
use Ibexa\Contracts\Core\Repository\Values\ContentType\ContentType;
use Ibexa\Contracts\Core\Repository\Values\ContentType\ContentTypeGroup;
use Ibexa\Contracts\Core\Repository\Values\ContentType\FieldDefinition;
use Ibexa\GraphQL\Schema\Domain\BaseNameHelper;
use Ibexa\GraphQL\Schema\Domain\Pluralizer;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;

class NameHelper implements LoggerAwareInterface
class NameHelper extends BaseNameHelper implements LoggerAwareInterface
{
use LoggerAwareTrait;

/**
* @var \Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter
*/
private $caseConverter;

/**
* @var string[]
*/
private $fieldNameOverrides;

private Pluralizer $pluralizer;
private array $fieldNameOverrides;

public function __construct(
array $fieldNameOverrides,
Pluralizer $pluralizer,
LoggerInterface $logger = null
) {
$this->caseConverter = new CamelCaseToSnakeCaseNameConverter(null, false);
parent::__construct($pluralizer);

$this->logger = $logger ?? new NullLogger();
$this->fieldNameOverrides = $fieldNameOverrides;
$this->pluralizer = $pluralizer;
}

/**
Expand All @@ -54,9 +49,7 @@ public function domainContentCollectionField(ContentType $contentType)

public function itemConnectionField(ContentType $contentType)
{
return $this->pluralizer->pluralize(
lcfirst($this->toCamelCase($contentType->identifier))
);
return $this->pluralize(lcfirst($this->toCamelCase($contentType->identifier)));
}

/**
Expand Down Expand Up @@ -245,11 +238,6 @@ public function fieldDefinitionField(FieldDefinition $fieldDefinition)
return $fieldName;
}

private function toCamelCase($string)
{
return $this->caseConverter->denormalize($string);
}

/**
* Removes potential spaces in content types groups names.
* (content types groups identifiers are actually their name).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace Ibexa\GraphQL\Schema\Domain\Content;
namespace Ibexa\GraphQL\Schema\Domain;

/**
* @internal
Expand Down

0 comments on commit b377ddf

Please sign in to comment.