API-Skeletons /
doctrine-graphql
| 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 array<array-key, mixed> |
||
|
0 ignored issues
–
show
Documentation
Bug
introduced
by
Loading history...
|
|||
| 28 | */ |
||
| 29 | public function __invoke(array $entityClasses): array |
||
| 30 | { |
||
| 31 | foreach ($entityClasses as $entityClass) { |
||
| 32 | // Get extract by value or reference |
||
| 33 | $byValue = $this->config->getGlobalByValue() ?? true; |
||
| 34 | |||
| 35 | // Save entity-level metadata |
||
| 36 | $this->metadataConfig[$entityClass] = [ |
||
| 37 | 'entityClass' => $entityClass, |
||
| 38 | 'byValue' => $byValue, |
||
| 39 | 'namingStrategy' => null, |
||
| 40 | 'fields' => [], |
||
| 41 | 'filters' => [], |
||
| 42 | 'excludeCriteria' => [], |
||
| 43 | 'description' => $entityClass, |
||
| 44 | 'typeName' => $this->getTypeName($entityClass), |
||
| 45 | ]; |
||
| 46 | |||
| 47 | $this->buildFieldMetadata($entityClass); |
||
| 48 | $this->buildAssociationMetadata($entityClass); |
||
| 49 | } |
||
| 50 | |||
| 51 | return $this->metadataConfig; |
||
| 52 | } |
||
| 53 | |||
| 54 | private function buildFieldMetadata(string $entityClass): void |
||
| 55 | { |
||
| 56 | $entityClassMetadata = $this->entityManager->getMetadataFactory()->getMetadataFor($entityClass); |
||
| 57 | |||
| 58 | foreach ($entityClassMetadata->getFieldNames() as $fieldName) { |
||
| 59 | if (in_array($fieldName, $this->config->getGlobalIgnore())) { |
||
| 60 | continue; |
||
| 61 | } |
||
| 62 | |||
| 63 | $this->metadataConfig[$entityClass]['fields'][$fieldName] = [ |
||
| 64 | 'description' => $fieldName, |
||
| 65 | 'type' => $entityClassMetadata->getTypeOfField($fieldName), |
||
| 66 | 'strategy' => $this->getDefaultStrategy($entityClassMetadata->getTypeOfField($fieldName)), |
||
| 67 | 'excludeCriteria' => [], |
||
| 68 | ]; |
||
| 69 | } |
||
| 70 | } |
||
| 71 | |||
| 72 | private function buildAssociationMetadata(string $entityClass): void |
||
| 73 | { |
||
| 74 | $entityClassMetadata = $this->entityManager->getMetadataFactory()->getMetadataFor($entityClass); |
||
| 75 | |||
| 76 | foreach ($entityClassMetadata->getAssociationNames() as $associationName) { |
||
| 77 | if (in_array($associationName, $this->config->getGlobalIgnore())) { |
||
| 78 | continue; |
||
| 79 | } |
||
| 80 | |||
| 81 | $this->metadataConfig[$entityClass]['fields'][$associationName] = [ |
||
| 82 | 'excludeCriteria' => [], |
||
| 83 | 'description' => $associationName, |
||
| 84 | 'filterCriteriaEventName' => null, |
||
| 85 | 'strategy' => Strategy\AssociationDefault::class, |
||
| 86 | ]; |
||
| 87 | } |
||
| 88 | } |
||
| 89 | } |
||
| 90 |