Skip to content

Commit 787f8ad

Browse files
authored
Merge pull request #124 from TomHAnderson/hotfix/improve-scrutinizer
Simplified classes
2 parents 3e26d78 + 501acbe commit 787f8ad

File tree

4 files changed

+145
-121
lines changed

4 files changed

+145
-121
lines changed

src/Criteria/CriteriaFactory.php

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ public function get(
4545
}
4646

4747
$fields = [];
48-
$classMetadata = $this->entityManager->getClassMetadata($targetEntity->getEntityClass());
4948
$entityMetadata = $targetEntity->getMetadataConfig();
5049
$allowedFilters = Filters::toArray();
5150

@@ -65,6 +64,28 @@ public function get(
6564
});
6665
}
6766

67+
$this->addFields($targetEntity, $typeName, $allowedFilters, $fields);
68+
$this->addAssociations($targetEntity, $typeName, $allowedFilters, $fields);
69+
70+
$inputObject = new InputObjectType([
71+
'name' => $typeName,
72+
'fields' => static fn () => $fields,
73+
]);
74+
75+
$this->typeManager->set($typeName, $inputObject);
76+
77+
return $inputObject;
78+
}
79+
80+
/**
81+
* @param string[] $allowedFilters
82+
* @param array<int, FiltersInputObjectType> $fields
83+
*/
84+
protected function addFields(Entity $targetEntity, string $typeName, array $allowedFilters, array &$fields): void
85+
{
86+
$classMetadata = $this->entityManager->getClassMetadata($targetEntity->getEntityClass());
87+
$entityMetadata = $targetEntity->getMetadataConfig();
88+
6889
foreach ($classMetadata->getFieldNames() as $fieldName) {
6990
// Only process fields that are in the graphql metadata
7091
if (! in_array($fieldName, array_keys($entityMetadata['fields']))) {
@@ -98,6 +119,16 @@ static function ($value) use ($fieldExcludeCriteria) {
98119
'description' => 'Filters for ' . $fieldName,
99120
];
100121
}
122+
}
123+
124+
/**
125+
* @param string[] $allowedFilters
126+
* @param array<int, FiltersInputObjectType> $fields
127+
*/
128+
protected function addAssociations(Entity $targetEntity, string $typeName, array $allowedFilters, array &$fields): void
129+
{
130+
$classMetadata = $this->entityManager->getClassMetadata($targetEntity->getEntityClass());
131+
$entityMetadata = $targetEntity->getMetadataConfig();
101132

102133
// Add eq filter for to-one associations
103134
foreach ($classMetadata->getAssociationNames() as $associationName) {
@@ -122,14 +153,5 @@ static function ($value) use ($fieldExcludeCriteria) {
122153
}
123154
}
124155
}
125-
126-
$inputObject = new InputObjectType([
127-
'name' => $typeName,
128-
'fields' => static fn () => $fields,
129-
]);
130-
131-
$this->typeManager->set($typeName, $inputObject);
132-
133-
return $inputObject;
134156
}
135157
}

src/Input/InputFactory.php

Lines changed: 58 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,51 @@ public function __construct(
2929
}
3030

3131
/**
32-
* @param string[] $optionalFields
33-
* @param array<array-key, InputObjectField> $fields
32+
* @param string[] $requiredFields An optional list of just the required fields you want for the mutation.
33+
* This allows specific fields per mutation.
34+
* @param string[] $optionalFields An optional list of optional fields you want for the mutation.
35+
* This allows specific fields per mutation.
3436
*
35-
* @return array<array-key, InputObjectField>
37+
* @throws Error
38+
*/
39+
public function get(string $id, array $requiredFields = [], array $optionalFields = []): InputObjectType
40+
{
41+
$fields = [];
42+
$targetEntity = $this->metadata->get($id);
43+
44+
if (! count($requiredFields) && ! count($optionalFields)) {
45+
$this->addAllFieldsAsRequired($targetEntity, $fields);
46+
} else {
47+
$this->addRequiredFields($targetEntity, $requiredFields, $fields);
48+
$this->addOptionalFields($targetEntity, $optionalFields, $fields);
49+
}
50+
51+
return new InputObjectType([
52+
'name' => $targetEntity->getTypeName() . '_Input',
53+
'description' => $targetEntity->getDescription(),
54+
'fields' => static fn () => $fields,
55+
]);
56+
}
57+
58+
/**
59+
* @param string[] $optionalFields
60+
* @param array<int, InputObjectField> $fields
3661
*/
3762
protected function addOptionalFields(
3863
mixed $targetEntity,
3964
array $optionalFields,
40-
array $fields,
41-
): array {
65+
array &$fields,
66+
): void {
4267
foreach ($this->entityManager->getClassMetadata($targetEntity->getEntityClass())->getFieldNames() as $fieldName) {
4368
if (! in_array($fieldName, $optionalFields) && $optionalFields !== ['*']) {
4469
continue;
4570
}
4671

72+
/**
73+
* Do not include identifiers as input. In the majority of cases there will be
74+
* no reason to set or update an identifier. For the case where an identifier
75+
* should be set or updated, this factory is not the correct solution.
76+
*/
4777
if ($optionalFields === ['*'] && $this->entityManager->getClassMetadata($targetEntity->getEntityClass())->isIdentifier($fieldName)) {
4878
continue;
4979
}
@@ -54,27 +84,31 @@ protected function addOptionalFields(
5484
'type' => $this->typeManager->get($targetEntity->getMetadataConfig()['fields'][$fieldName]['type']),
5585
]);
5686
}
57-
58-
return $fields;
5987
}
6088

6189
/**
62-
* @param string[] $requiredFields
63-
* @param array<array-key, InputObjectField> $fields
64-
*
65-
* @return array<array-key, InputObjectField>
90+
* @param string[] $requiredFields
91+
* @param array<int, InputObjectField> $fields
6692
*/
6793
protected function addRequiredFields(
6894
mixed $targetEntity,
6995
array $requiredFields,
70-
array $fields,
71-
): array {
96+
array &$fields,
97+
): void {
7298
foreach ($this->entityManager->getClassMetadata($targetEntity->getEntityClass())->getFieldNames() as $fieldName) {
7399
if (! in_array($fieldName, $requiredFields) && $requiredFields !== ['*']) {
74100
continue;
75101
}
76102

77-
if ($requiredFields === ['*'] && $this->entityManager->getClassMetadata($targetEntity->getEntityClass())->isIdentifier($fieldName)) {
103+
/**
104+
* Do not include identifiers as input. In the majority of cases there will be
105+
* no reason to set or update an identifier. For the case where an identifier
106+
* should be set or updated, this factory is not the correct solution.
107+
*/
108+
if (
109+
$requiredFields === ['*']
110+
&& $this->entityManager->getClassMetadata($targetEntity->getEntityClass())->isIdentifier($fieldName)
111+
) {
78112
continue;
79113
}
80114

@@ -85,21 +119,22 @@ protected function addRequiredFields(
85119
$fields[$fieldName] = new InputObjectField([
86120
'name' => $fieldName,
87121
'description' => (string) $targetEntity->getMetadataConfig()['fields'][$fieldName]['description'],
88-
'type' => Type::nonNull($this->typeManager->get($targetEntity->getMetadataConfig()['fields'][$fieldName]['type'])),
122+
'type' => Type::nonNull($this->typeManager->get(
123+
$targetEntity->getMetadataConfig()['fields'][$fieldName]['type'],
124+
)),
89125
]);
90126
}
91-
92-
return $fields;
93127
}
94128

95-
/**
96-
* @param array<array-key, InputObjectField> $fields
97-
*
98-
* @return array<array-key, InputObjectField>
99-
*/
100-
protected function addAllFieldsAsRequired(mixed $targetEntity, array $fields): array
129+
/** @param array<int, InputObjectField> $fields */
130+
protected function addAllFieldsAsRequired(mixed $targetEntity, array &$fields): void
101131
{
102132
foreach ($this->entityManager->getClassMetadata($targetEntity->getEntityClass())->getFieldNames() as $fieldName) {
133+
/**
134+
* Do not include identifiers as input. In the majority of cases there will be
135+
* no reason to set or update an identifier. For the case where an identifier
136+
* should be set or updated, this factory is not the correct solution.
137+
*/
103138
if ($this->entityManager->getClassMetadata($targetEntity->getEntityClass())->isIdentifier($fieldName)) {
104139
continue;
105140
}
@@ -110,40 +145,5 @@ protected function addAllFieldsAsRequired(mixed $targetEntity, array $fields): a
110145
'type' => Type::nonNull($this->typeManager->get($targetEntity->getMetadataConfig()['fields'][$fieldName]['type'])),
111146
]);
112147
}
113-
114-
return $fields;
115-
}
116-
117-
/**
118-
* @param string[] $requiredFields An optional list of just the required fields you want for the mutation.
119-
* This allows specific fields per mutation.
120-
* @param string[] $optionalFields An optional list of optional fields you want for the mutation.
121-
* This allows specific fields per mutation.
122-
*
123-
* @throws Error
124-
*/
125-
public function get(string $id, array $requiredFields = [], array $optionalFields = []): InputObjectType
126-
{
127-
$fields = [];
128-
$targetEntity = $this->metadata->get($id);
129-
130-
/**
131-
* Do not include identifiers as input. In the majority of cases there will be
132-
* no reason to set or update an identifier. For the case where an identifier
133-
* should be set or updated, this facotry is not the correct solution.
134-
*/
135-
136-
if (! count($requiredFields) && ! count($optionalFields)) {
137-
$fields = $this->addAllFieldsAsRequired($targetEntity, $fields);
138-
} else {
139-
$fields = $this->addRequiredFields($targetEntity, $requiredFields, $fields);
140-
$fields = $this->addOptionalFields($targetEntity, $optionalFields, $fields);
141-
}
142-
143-
return new InputObjectType([
144-
'name' => $targetEntity->getTypeName() . '_Input',
145-
'description' => $targetEntity->getDescription(),
146-
'fields' => static fn () => $fields,
147-
]);
148148
}
149149
}

src/Metadata/GlobalEnable.php

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function __construct(
2424
/**
2525
* @param string[] $entityClasses
2626
*
27-
* @return mixed[]
27+
* @return array<int, mixed>
2828
*/
2929
public function __invoke(array $entityClasses): array
3030
{
@@ -60,21 +60,16 @@ private function buildFieldMetadata(string $entityClass): void
6060
continue;
6161
}
6262

63-
$this->metadataConfig[$entityClass]['fields'][$fieldName]['description']
64-
= $fieldName;
65-
66-
$this->metadataConfig[$entityClass]['fields'][$fieldName]['type']
67-
= $entityClassMetadata->getTypeOfField($fieldName);
68-
69-
// Set default strategy based on field type
70-
$this->metadataConfig[$entityClass]['fields'][$fieldName]['strategy']
71-
= $this->getDefaultStrategy($entityClassMetadata->getTypeOfField($fieldName));
72-
73-
$this->metadataConfig[$entityClass]['fields'][$fieldName]['excludeCriteria'] = [];
63+
$this->metadataConfig[$entityClass]['fields'][$fieldName] = [
64+
'description' => $fieldName,
65+
'type' => $entityClassMetadata->getTypeOfField($fieldName),
66+
'strategy' => $this->getDefaultStrategy($entityClassMetadata->getTypeOfField($fieldName)),
67+
'excludeCriteria' => [],
68+
];
7469
}
7570
}
7671

77-
public function buildAssociationMetadata(string $entityClass): void
72+
private function buildAssociationMetadata(string $entityClass): void
7873
{
7974
$entityClassMetadata = $this->entityManager->getMetadataFactory()->getMetadataFor($entityClass);
8075

@@ -83,14 +78,12 @@ public function buildAssociationMetadata(string $entityClass): void
8378
continue;
8479
}
8580

86-
$this->metadataConfig[$entityClass]['fields'][$associationName]['excludeCriteria'] = [];
87-
$this->metadataConfig[$entityClass]['fields'][$associationName]['description'] = $associationName;
88-
$this->metadataConfig[$entityClass]['fields'][$associationName]['filterCriteriaEventName']
89-
= null;
90-
91-
// NullifyOwningAssociation is not used for globalEnable
92-
$this->metadataConfig[$entityClass]['fields'][$associationName]['strategy'] =
93-
Strategy\AssociationDefault::class;
81+
$this->metadataConfig[$entityClass]['fields'][$associationName] = [
82+
'excludeCriteria' => [],
83+
'description' => $associationName,
84+
'filterCriteriaEventName' => null,
85+
'strategy' => Strategy\AssociationDefault::class,
86+
];
9487
}
9588
}
9689
}

0 commit comments

Comments
 (0)