1
|
|
|
<?php |
2
|
|
|
/******************************************************************************* |
3
|
|
|
* This file is part of the GraphQL Bundle package. |
4
|
|
|
* |
5
|
|
|
* (c) YnloUltratech <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
******************************************************************************/ |
10
|
|
|
|
11
|
|
|
namespace Ynlo\GraphQLBundle\Definition\Loader\Annotation; |
12
|
|
|
|
13
|
|
|
use Ynlo\GraphQLBundle\Annotation; |
14
|
|
|
use Ynlo\GraphQLBundle\Definition\ArgumentDefinition; |
15
|
|
|
use Ynlo\GraphQLBundle\Definition\ConnectionDefinitionBuilder; |
16
|
|
|
use Ynlo\GraphQLBundle\Definition\QueryDefinition; |
17
|
|
|
use Ynlo\GraphQLBundle\Definition\Registry\DefinitionManager; |
18
|
|
|
use Ynlo\GraphQLBundle\Util\TypeUtil; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Parse Query annotation to fetch queries |
22
|
|
|
*/ |
23
|
|
|
class QueryAnnotationParser implements AnnotationParserInterface |
24
|
|
|
{ |
25
|
|
|
use AnnotationReaderAwareTrait; |
26
|
|
|
use AnnotationParserHelper; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var ConnectionDefinitionBuilder |
30
|
|
|
*/ |
31
|
|
|
protected $connectionBuilder; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var DefinitionManager |
35
|
|
|
*/ |
36
|
|
|
protected $definitionManager; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param ConnectionDefinitionBuilder $connectionBuilder |
40
|
|
|
*/ |
41
|
1 |
|
public function __construct(ConnectionDefinitionBuilder $connectionBuilder) |
42
|
|
|
{ |
43
|
1 |
|
$this->connectionBuilder = $connectionBuilder; |
44
|
1 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritdoc} |
48
|
|
|
*/ |
49
|
1 |
|
public function supports($annotation): bool |
50
|
|
|
{ |
51
|
1 |
|
return $annotation instanceof Annotation\Query; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
1 |
|
public function parse($annotation, \ReflectionClass $refClass, DefinitionManager $definitionManager) |
58
|
|
|
{ |
59
|
1 |
|
$this->definitionManager = $definitionManager; |
60
|
|
|
|
61
|
|
|
/** @var Annotation\Query $annotation */ |
62
|
1 |
|
$query = new QueryDefinition(); |
63
|
|
|
|
64
|
1 |
View Code Duplication |
if ($annotation->name) { |
|
|
|
|
65
|
1 |
|
$query->setName($annotation->name); |
66
|
|
|
} else { |
67
|
1 |
|
$query->setName(lcfirst($this->getDefaultName($refClass, $definitionManager))); |
68
|
|
|
} |
69
|
|
|
|
70
|
1 |
|
if ($definitionManager->hasQuery($query->getName())) { |
71
|
|
|
$query = $definitionManager->getQuery($query->getName()); |
72
|
|
|
} else { |
73
|
1 |
|
$definitionManager->addQuery($query); |
74
|
|
|
} |
75
|
|
|
|
76
|
1 |
|
$objectDefinition = $this->getObjectDefinition($refClass, $definitionManager); |
77
|
1 |
|
if ($objectDefinition) { |
78
|
1 |
|
$query->setType($objectDefinition->getName()); |
79
|
1 |
|
$query->setList($annotation->list); |
80
|
|
|
} else { |
81
|
|
|
$error = sprintf('Does not exist any valid type for class "%s"', $refClass->getName()); |
82
|
|
|
throw new \RuntimeException($error); |
83
|
|
|
} |
84
|
|
|
|
85
|
1 |
|
$argAnnotations = $this->reader->getClassAnnotations($refClass); |
86
|
1 |
View Code Duplication |
foreach ($argAnnotations as $argAnnotation) { |
|
|
|
|
87
|
1 |
|
if ($argAnnotation instanceof Annotation\Argument) { |
88
|
1 |
|
$arg = new ArgumentDefinition(); |
89
|
1 |
|
$arg->setName($argAnnotation->name); |
90
|
1 |
|
$arg->setDescription($argAnnotation->description); |
91
|
1 |
|
$arg->setInternalName($argAnnotation->internalName); |
92
|
1 |
|
$arg->setDefaultValue($argAnnotation->defaultValue); |
93
|
1 |
|
$arg->setType(TypeUtil::normalize($argAnnotation->type)); |
94
|
1 |
|
$arg->setList(TypeUtil::isTypeList($argAnnotation->type)); |
95
|
1 |
|
$arg->setNonNullList(TypeUtil::isTypeNonNullList($argAnnotation->type)); |
96
|
1 |
|
$arg->setNonNull(TypeUtil::isTypeNonNull($argAnnotation->type)); |
97
|
1 |
|
$query->addArgument($arg); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
1 |
|
$query->setResolver($refClass->getName()); |
102
|
1 |
|
$query->setDeprecationReason($annotation->deprecationReason); |
103
|
1 |
|
$query->setDescription($annotation->description); |
104
|
|
|
|
105
|
|
|
/** @var Annotation\Connection $connection */ |
106
|
1 |
View Code Duplication |
if ($connection = $this->reader->getClassAnnotation($refClass, Annotation\Connection::class)) { |
|
|
|
|
107
|
1 |
|
$this->connectionBuilder->setEndpoint($this->definitionManager->getEndpoint()); |
108
|
1 |
|
$this->connectionBuilder->setLimit($connection->limit); |
109
|
1 |
|
$this->connectionBuilder->setParentField($connection->parentField); |
110
|
1 |
|
$this->connectionBuilder->build($query, $query->getType()); |
111
|
1 |
|
$query->setResolver($refClass->getName()); |
112
|
|
|
} |
113
|
1 |
|
} |
114
|
|
|
} |
115
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.