Skip to content

Commit

Permalink
Merge pull request #121 from TomHAnderson/hotfix/globalEnable
Browse files Browse the repository at this point in the history
Moved GlobalEnable logic to its own class
  • Loading branch information
TomHAnderson authored Feb 1, 2023
2 parents d0cb76b + d3e2f19 commit ea9e693
Show file tree
Hide file tree
Showing 4 changed files with 188 additions and 135 deletions.
82 changes: 82 additions & 0 deletions src/Metadata/AbstractMetadataFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

declare(strict_types=1);

namespace ApiSkeletons\Doctrine\GraphQL\Metadata;

use ApiSkeletons\Doctrine\GraphQL\Config;
use ApiSkeletons\Doctrine\GraphQL\Hydrator\Strategy;

use function str_replace;
use function strlen;
use function strpos;
use function substr;

/**
* This ancestor class contains functions common to the MetadataFactory
* and GlobalEnable
*/
abstract class AbstractMetadataFactory
{
protected Config $config;

protected function getDefaultStrategy(string|null $fieldType): string
{
// Set default strategy based on field type
switch ($fieldType) {
case 'tinyint':
case 'smallint':
case 'integer':
case 'int':
return Strategy\ToInteger::class;

case 'boolean':
return Strategy\ToBoolean::class;

case 'decimal':
case 'float':
return Strategy\ToFloat::class;

default:
return Strategy\FieldDefault::class;
}
}

/**
* Compute the GraphQL type name
*/
protected function getTypeName(string $entityClass): string
{
return $this->appendGroupSuffix($this->stripEntityPrefix($entityClass));
}

/**
* Strip the configured entityPrefix from the type name
*/
protected function stripEntityPrefix(string $entityClass): string
{
if ($this->config->getEntityPrefix() !== null) {
if (strpos($entityClass, $this->config->getEntityPrefix()) === 0) {
$entityClass = substr($entityClass, strlen($this->config->getEntityPrefix()));
}
}

return str_replace('\\', '_', $entityClass);
}

/**
* Append the configured groupSuffix from the type name
*/
protected function appendGroupSuffix(string $entityClass): string
{
if ($this->config->getGroupSuffix() !== null) {
if ($this->config->getGroupSuffix()) {
$entityClass .= '_' . $this->config->getGroupSuffix();
}
} else {
$entityClass .= '_' . $this->config->getGroup();
}

return $entityClass;
}
}
93 changes: 93 additions & 0 deletions src/Metadata/GlobalEnable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

declare(strict_types=1);

namespace ApiSkeletons\Doctrine\GraphQL\Metadata;

use ApiSkeletons\Doctrine\GraphQL\Config;
use ApiSkeletons\Doctrine\GraphQL\Hydrator\Strategy;
use Doctrine\ORM\EntityManager;

use function in_array;

final class GlobalEnable extends AbstractMetadataFactory
{
/** @var mixed[] */
private array $metadataConfig = [];

public function __construct(
private EntityManager $entityManager,
protected Config $config,
) {
}

/**
* @param string[] $entityClasses
*
* @return mixed[]
*/
public function __invoke(array $entityClasses): array
{
$globalIgnore = $this->config->getGlobalIgnore();

foreach ($entityClasses as $entityClass) {
// Get extract by value or reference
$byValue = $this->config->getGlobalByValue() ?? true;

// Save entity-level metadata
$this->metadataConfig[$entityClass] = [
'entityClass' => $entityClass,
'byValue' => $byValue,
'namingStrategy' => null,
'fields' => [],
'filters' => [],
'excludeCriteria' => [],
'description' => $entityClass,
'typeName' => $this->getTypeName($entityClass),
];

// Fetch fields
$entityClassMetadata = $this->entityManager->getMetadataFactory()->getMetadataFor($entityClass);
$fieldNames = $entityClassMetadata->getFieldNames();

foreach ($fieldNames as $fieldName) {
if (in_array($fieldName, $globalIgnore)) {
continue;
}

$this->metadataConfig[$entityClass]['fields'][$fieldName]['description'] =
$fieldName;

$this->metadataConfig[$entityClass]['fields'][$fieldName]['type'] =
$entityClassMetadata->getTypeOfField($fieldName);

// Set default strategy based on field type
$this->metadataConfig[$entityClass]['fields'][$fieldName]['strategy'] =
$this->getDefaultStrategy($entityClassMetadata->getTypeOfField($fieldName));

$this->metadataConfig[$entityClass]['fields'][$fieldName]['excludeCriteria'] = [];
}

// Fetch attributes for associations
$associationNames = $this->entityManager->getMetadataFactory()
->getMetadataFor($entityClass)->getAssociationNames();

foreach ($associationNames as $associationName) {
if (in_array($associationName, $globalIgnore)) {
continue;
}

$this->metadataConfig[$entityClass]['fields'][$associationName]['excludeCriteria'] = [];
$this->metadataConfig[$entityClass]['fields'][$associationName]['description'] = $associationName;
$this->metadataConfig[$entityClass]['fields'][$associationName]['filterCriteriaEventName']
= null;

// NullifyOwningAssociation is not used for globalEnable
$this->metadataConfig[$entityClass]['fields'][$associationName]['strategy'] =
Strategy\AssociationDefault::class;
}
}

return $this->metadataConfig;
}
}
139 changes: 4 additions & 135 deletions src/Metadata/MetadataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,8 @@
use ReflectionClass;

use function assert;
use function in_array;
use function str_replace;
use function strlen;
use function strpos;
use function substr;

class MetadataFactory
class MetadataFactory extends AbstractMetadataFactory
{
protected Metadata|null $metadata = null;
protected EntityManager $entityManager;
Expand Down Expand Up @@ -48,74 +43,6 @@ public function getMetadata(): Metadata
return $this->buildMetadata();
}

/** @param string[] $entityClasses */
private function globalEnable(array $entityClasses): Metadata
{
$globalIgnore = $this->config->getGlobalIgnore();

foreach ($entityClasses as $entityClass) {
// Get extract by value or reference
$byValue = $this->config->getGlobalByValue() ?? true;

// Save entity-level metadata
$this->metadataConfig[$entityClass] = [
'entityClass' => $entityClass,
'byValue' => $byValue,
'namingStrategy' => null,
'fields' => [],
'filters' => [],
'excludeCriteria' => [],
'description' => $entityClass,
'typeName' => $this->getTypeName($entityClass),
];

// Fetch fields
$entityClassMetadata = $this->entityManager->getMetadataFactory()->getMetadataFor($entityClass);
$fieldNames = $entityClassMetadata->getFieldNames();

foreach ($fieldNames as $fieldName) {
if (in_array($fieldName, $globalIgnore)) {
continue;
}

$this->metadataConfig[$entityClass]['fields'][$fieldName]['description'] =
$fieldName;

$this->metadataConfig[$entityClass]['fields'][$fieldName]['type'] =
$entityClassMetadata->getTypeOfField($fieldName);

// Set default strategy based on field type
$this->metadataConfig[$entityClass]['fields'][$fieldName]['strategy'] =
$this->getDefaultStrategy($entityClassMetadata->getTypeOfField($fieldName));

$this->metadataConfig[$entityClass]['fields'][$fieldName]['excludeCriteria'] = [];
}

// Fetch attributes for associations
$associationNames = $this->entityManager->getMetadataFactory()
->getMetadataFor($entityClass)->getAssociationNames();

foreach ($associationNames as $associationName) {
if (in_array($associationName, $globalIgnore)) {
continue;
}

$this->metadataConfig[$entityClass]['fields'][$associationName]['excludeCriteria'] = [];
$this->metadataConfig[$entityClass]['fields'][$associationName]['description'] = $associationName;
$this->metadataConfig[$entityClass]['fields'][$associationName]['filterCriteriaEventName']
= null;

// NullifyOwningAssociation is not used for globalEnable
$this->metadataConfig[$entityClass]['fields'][$associationName]['strategy'] =
Strategy\AssociationDefault::class;
}
}

$this->metadata = new Metadata($this->container, $this->metadataConfig);

return $this->metadata;
}

protected function buildMetadata(): Metadata
{
$entityClasses = [];
Expand All @@ -124,7 +51,9 @@ protected function buildMetadata(): Metadata
}

if ($this->config->getGlobalEnable()) {
return $this->globalEnable($entityClasses);
$globalEnable = $this->container->get(GlobalEnable::class);

return new Metadata($this->container, $globalEnable($entityClasses));
}

foreach ($entityClasses as $entityClass) {
Expand Down Expand Up @@ -286,64 +215,4 @@ private function buildMetadataConfigForAssociations(
}
}
}

/**
* Strip the configured entityPrefix from the type name
*/
private function stripEntityPrefix(string $entityClass): string
{
if ($this->config->getEntityPrefix() !== null) {
if (strpos($entityClass, $this->config->getEntityPrefix()) === 0) {
$entityClass = substr($entityClass, strlen($this->config->getEntityPrefix()));
}
}

return str_replace('\\', '_', $entityClass);
}

/**
* Append the configured groupSuffix from the type name
*/
private function appendGroupSuffix(string $entityClass): string
{
if ($this->config->getGroupSuffix() !== null) {
if ($this->config->getGroupSuffix()) {
$entityClass .= '_' . $this->config->getGroupSuffix();
}
} else {
$entityClass .= '_' . $this->config->getGroup();
}

return $entityClass;
}

/**
* Compute the GraphQL type name
*/
private function getTypeName(string $entityClass): string
{
return $this->appendGroupSuffix($this->stripEntityPrefix($entityClass));
}

private function getDefaultStrategy(string|null $fieldType): string
{
// Set default strategy based on field type
switch ($fieldType) {
case 'tinyint':
case 'smallint':
case 'integer':
case 'int':
return Strategy\ToInteger::class;

case 'boolean':
return Strategy\ToBoolean::class;

case 'decimal':
case 'float':
return Strategy\ToFloat::class;

default:
return Strategy\FieldDefault::class;
}
}
}
9 changes: 9 additions & 0 deletions src/Services.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ static function (ContainerInterface $container) use ($metadataConfig) {
return (new Metadata\MetadataFactory($container, $metadataConfig))->getMetadata();
},
)
->set(
Metadata\GlobalEnable::class,
static function (ContainerInterface $container) {
return new Metadata\GlobalEnable(
$container->get(EntityManager::class),
$container->get(Config::class),
);
},
)
->set(
Resolve\FieldResolver::class,
static function (ContainerInterface $container) {
Expand Down

0 comments on commit ea9e693

Please sign in to comment.