Skip to content

Commit ea9e693

Browse files
authored
Merge pull request #121 from TomHAnderson/hotfix/globalEnable
Moved GlobalEnable logic to its own class
2 parents d0cb76b + d3e2f19 commit ea9e693

File tree

4 files changed

+188
-135
lines changed

4 files changed

+188
-135
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace ApiSkeletons\Doctrine\GraphQL\Metadata;
6+
7+
use ApiSkeletons\Doctrine\GraphQL\Config;
8+
use ApiSkeletons\Doctrine\GraphQL\Hydrator\Strategy;
9+
10+
use function str_replace;
11+
use function strlen;
12+
use function strpos;
13+
use function substr;
14+
15+
/**
16+
* This ancestor class contains functions common to the MetadataFactory
17+
* and GlobalEnable
18+
*/
19+
abstract class AbstractMetadataFactory
20+
{
21+
protected Config $config;
22+
23+
protected function getDefaultStrategy(string|null $fieldType): string
24+
{
25+
// Set default strategy based on field type
26+
switch ($fieldType) {
27+
case 'tinyint':
28+
case 'smallint':
29+
case 'integer':
30+
case 'int':
31+
return Strategy\ToInteger::class;
32+
33+
case 'boolean':
34+
return Strategy\ToBoolean::class;
35+
36+
case 'decimal':
37+
case 'float':
38+
return Strategy\ToFloat::class;
39+
40+
default:
41+
return Strategy\FieldDefault::class;
42+
}
43+
}
44+
45+
/**
46+
* Compute the GraphQL type name
47+
*/
48+
protected function getTypeName(string $entityClass): string
49+
{
50+
return $this->appendGroupSuffix($this->stripEntityPrefix($entityClass));
51+
}
52+
53+
/**
54+
* Strip the configured entityPrefix from the type name
55+
*/
56+
protected function stripEntityPrefix(string $entityClass): string
57+
{
58+
if ($this->config->getEntityPrefix() !== null) {
59+
if (strpos($entityClass, $this->config->getEntityPrefix()) === 0) {
60+
$entityClass = substr($entityClass, strlen($this->config->getEntityPrefix()));
61+
}
62+
}
63+
64+
return str_replace('\\', '_', $entityClass);
65+
}
66+
67+
/**
68+
* Append the configured groupSuffix from the type name
69+
*/
70+
protected function appendGroupSuffix(string $entityClass): string
71+
{
72+
if ($this->config->getGroupSuffix() !== null) {
73+
if ($this->config->getGroupSuffix()) {
74+
$entityClass .= '_' . $this->config->getGroupSuffix();
75+
}
76+
} else {
77+
$entityClass .= '_' . $this->config->getGroup();
78+
}
79+
80+
return $entityClass;
81+
}
82+
}

src/Metadata/GlobalEnable.php

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace ApiSkeletons\Doctrine\GraphQL\Metadata;
6+
7+
use ApiSkeletons\Doctrine\GraphQL\Config;
8+
use ApiSkeletons\Doctrine\GraphQL\Hydrator\Strategy;
9+
use Doctrine\ORM\EntityManager;
10+
11+
use function in_array;
12+
13+
final class GlobalEnable extends AbstractMetadataFactory
14+
{
15+
/** @var mixed[] */
16+
private array $metadataConfig = [];
17+
18+
public function __construct(
19+
private EntityManager $entityManager,
20+
protected Config $config,
21+
) {
22+
}
23+
24+
/**
25+
* @param string[] $entityClasses
26+
*
27+
* @return mixed[]
28+
*/
29+
public function __invoke(array $entityClasses): array
30+
{
31+
$globalIgnore = $this->config->getGlobalIgnore();
32+
33+
foreach ($entityClasses as $entityClass) {
34+
// Get extract by value or reference
35+
$byValue = $this->config->getGlobalByValue() ?? true;
36+
37+
// Save entity-level metadata
38+
$this->metadataConfig[$entityClass] = [
39+
'entityClass' => $entityClass,
40+
'byValue' => $byValue,
41+
'namingStrategy' => null,
42+
'fields' => [],
43+
'filters' => [],
44+
'excludeCriteria' => [],
45+
'description' => $entityClass,
46+
'typeName' => $this->getTypeName($entityClass),
47+
];
48+
49+
// Fetch fields
50+
$entityClassMetadata = $this->entityManager->getMetadataFactory()->getMetadataFor($entityClass);
51+
$fieldNames = $entityClassMetadata->getFieldNames();
52+
53+
foreach ($fieldNames as $fieldName) {
54+
if (in_array($fieldName, $globalIgnore)) {
55+
continue;
56+
}
57+
58+
$this->metadataConfig[$entityClass]['fields'][$fieldName]['description'] =
59+
$fieldName;
60+
61+
$this->metadataConfig[$entityClass]['fields'][$fieldName]['type'] =
62+
$entityClassMetadata->getTypeOfField($fieldName);
63+
64+
// Set default strategy based on field type
65+
$this->metadataConfig[$entityClass]['fields'][$fieldName]['strategy'] =
66+
$this->getDefaultStrategy($entityClassMetadata->getTypeOfField($fieldName));
67+
68+
$this->metadataConfig[$entityClass]['fields'][$fieldName]['excludeCriteria'] = [];
69+
}
70+
71+
// Fetch attributes for associations
72+
$associationNames = $this->entityManager->getMetadataFactory()
73+
->getMetadataFor($entityClass)->getAssociationNames();
74+
75+
foreach ($associationNames as $associationName) {
76+
if (in_array($associationName, $globalIgnore)) {
77+
continue;
78+
}
79+
80+
$this->metadataConfig[$entityClass]['fields'][$associationName]['excludeCriteria'] = [];
81+
$this->metadataConfig[$entityClass]['fields'][$associationName]['description'] = $associationName;
82+
$this->metadataConfig[$entityClass]['fields'][$associationName]['filterCriteriaEventName']
83+
= null;
84+
85+
// NullifyOwningAssociation is not used for globalEnable
86+
$this->metadataConfig[$entityClass]['fields'][$associationName]['strategy'] =
87+
Strategy\AssociationDefault::class;
88+
}
89+
}
90+
91+
return $this->metadataConfig;
92+
}
93+
}

src/Metadata/MetadataFactory.php

Lines changed: 4 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,8 @@
1414
use ReflectionClass;
1515

1616
use function assert;
17-
use function in_array;
18-
use function str_replace;
19-
use function strlen;
20-
use function strpos;
21-
use function substr;
2217

23-
class MetadataFactory
18+
class MetadataFactory extends AbstractMetadataFactory
2419
{
2520
protected Metadata|null $metadata = null;
2621
protected EntityManager $entityManager;
@@ -48,74 +43,6 @@ public function getMetadata(): Metadata
4843
return $this->buildMetadata();
4944
}
5045

51-
/** @param string[] $entityClasses */
52-
private function globalEnable(array $entityClasses): Metadata
53-
{
54-
$globalIgnore = $this->config->getGlobalIgnore();
55-
56-
foreach ($entityClasses as $entityClass) {
57-
// Get extract by value or reference
58-
$byValue = $this->config->getGlobalByValue() ?? true;
59-
60-
// Save entity-level metadata
61-
$this->metadataConfig[$entityClass] = [
62-
'entityClass' => $entityClass,
63-
'byValue' => $byValue,
64-
'namingStrategy' => null,
65-
'fields' => [],
66-
'filters' => [],
67-
'excludeCriteria' => [],
68-
'description' => $entityClass,
69-
'typeName' => $this->getTypeName($entityClass),
70-
];
71-
72-
// Fetch fields
73-
$entityClassMetadata = $this->entityManager->getMetadataFactory()->getMetadataFor($entityClass);
74-
$fieldNames = $entityClassMetadata->getFieldNames();
75-
76-
foreach ($fieldNames as $fieldName) {
77-
if (in_array($fieldName, $globalIgnore)) {
78-
continue;
79-
}
80-
81-
$this->metadataConfig[$entityClass]['fields'][$fieldName]['description'] =
82-
$fieldName;
83-
84-
$this->metadataConfig[$entityClass]['fields'][$fieldName]['type'] =
85-
$entityClassMetadata->getTypeOfField($fieldName);
86-
87-
// Set default strategy based on field type
88-
$this->metadataConfig[$entityClass]['fields'][$fieldName]['strategy'] =
89-
$this->getDefaultStrategy($entityClassMetadata->getTypeOfField($fieldName));
90-
91-
$this->metadataConfig[$entityClass]['fields'][$fieldName]['excludeCriteria'] = [];
92-
}
93-
94-
// Fetch attributes for associations
95-
$associationNames = $this->entityManager->getMetadataFactory()
96-
->getMetadataFor($entityClass)->getAssociationNames();
97-
98-
foreach ($associationNames as $associationName) {
99-
if (in_array($associationName, $globalIgnore)) {
100-
continue;
101-
}
102-
103-
$this->metadataConfig[$entityClass]['fields'][$associationName]['excludeCriteria'] = [];
104-
$this->metadataConfig[$entityClass]['fields'][$associationName]['description'] = $associationName;
105-
$this->metadataConfig[$entityClass]['fields'][$associationName]['filterCriteriaEventName']
106-
= null;
107-
108-
// NullifyOwningAssociation is not used for globalEnable
109-
$this->metadataConfig[$entityClass]['fields'][$associationName]['strategy'] =
110-
Strategy\AssociationDefault::class;
111-
}
112-
}
113-
114-
$this->metadata = new Metadata($this->container, $this->metadataConfig);
115-
116-
return $this->metadata;
117-
}
118-
11946
protected function buildMetadata(): Metadata
12047
{
12148
$entityClasses = [];
@@ -124,7 +51,9 @@ protected function buildMetadata(): Metadata
12451
}
12552

12653
if ($this->config->getGlobalEnable()) {
127-
return $this->globalEnable($entityClasses);
54+
$globalEnable = $this->container->get(GlobalEnable::class);
55+
56+
return new Metadata($this->container, $globalEnable($entityClasses));
12857
}
12958

13059
foreach ($entityClasses as $entityClass) {
@@ -286,64 +215,4 @@ private function buildMetadataConfigForAssociations(
286215
}
287216
}
288217
}
289-
290-
/**
291-
* Strip the configured entityPrefix from the type name
292-
*/
293-
private function stripEntityPrefix(string $entityClass): string
294-
{
295-
if ($this->config->getEntityPrefix() !== null) {
296-
if (strpos($entityClass, $this->config->getEntityPrefix()) === 0) {
297-
$entityClass = substr($entityClass, strlen($this->config->getEntityPrefix()));
298-
}
299-
}
300-
301-
return str_replace('\\', '_', $entityClass);
302-
}
303-
304-
/**
305-
* Append the configured groupSuffix from the type name
306-
*/
307-
private function appendGroupSuffix(string $entityClass): string
308-
{
309-
if ($this->config->getGroupSuffix() !== null) {
310-
if ($this->config->getGroupSuffix()) {
311-
$entityClass .= '_' . $this->config->getGroupSuffix();
312-
}
313-
} else {
314-
$entityClass .= '_' . $this->config->getGroup();
315-
}
316-
317-
return $entityClass;
318-
}
319-
320-
/**
321-
* Compute the GraphQL type name
322-
*/
323-
private function getTypeName(string $entityClass): string
324-
{
325-
return $this->appendGroupSuffix($this->stripEntityPrefix($entityClass));
326-
}
327-
328-
private function getDefaultStrategy(string|null $fieldType): string
329-
{
330-
// Set default strategy based on field type
331-
switch ($fieldType) {
332-
case 'tinyint':
333-
case 'smallint':
334-
case 'integer':
335-
case 'int':
336-
return Strategy\ToInteger::class;
337-
338-
case 'boolean':
339-
return Strategy\ToBoolean::class;
340-
341-
case 'decimal':
342-
case 'float':
343-
return Strategy\ToFloat::class;
344-
345-
default:
346-
return Strategy\FieldDefault::class;
347-
}
348-
}
349218
}

src/Services.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ static function (ContainerInterface $container) use ($metadataConfig) {
5252
return (new Metadata\MetadataFactory($container, $metadataConfig))->getMetadata();
5353
},
5454
)
55+
->set(
56+
Metadata\GlobalEnable::class,
57+
static function (ContainerInterface $container) {
58+
return new Metadata\GlobalEnable(
59+
$container->get(EntityManager::class),
60+
$container->get(Config::class),
61+
);
62+
},
63+
)
5564
->set(
5665
Resolve\FieldResolver::class,
5766
static function (ContainerInterface $container) {

0 commit comments

Comments
 (0)