1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace TheCodingMachine\GraphQL\Controllers; |
5
|
|
|
|
6
|
|
|
use phpDocumentor\Reflection\Type; |
7
|
|
|
use phpDocumentor\Reflection\Types\Array_; |
8
|
|
|
use phpDocumentor\Reflection\Types\Mixed; |
9
|
|
|
use phpDocumentor\Reflection\Types\Object_; |
10
|
|
|
use phpDocumentor\Reflection\Types\String_; |
11
|
|
|
use Psr\Container\ContainerInterface; |
12
|
|
|
use Roave\BetterReflection\Reflection\ReflectionClass; |
13
|
|
|
use Roave\BetterReflection\Reflection\ReflectionMethod; |
14
|
|
|
use Doctrine\Common\Annotations\Reader; |
15
|
|
|
use phpDocumentor\Reflection\Types\Integer; |
16
|
|
|
use TheCodingMachine\GraphQL\Controllers\Annotations\Query; |
17
|
|
|
use TheCodingMachine\GraphQL\Controllers\Security\AuthenticationServiceInterface; |
18
|
|
|
use TheCodingMachine\GraphQL\Controllers\Security\AuthorizationServiceInterface; |
19
|
|
|
use Youshido\GraphQL\Field\Field; |
20
|
|
|
use Youshido\GraphQL\Type\ListType\ListType; |
21
|
|
|
use Youshido\GraphQL\Type\NonNullType; |
22
|
|
|
use Youshido\GraphQL\Type\Scalar\IntType; |
23
|
|
|
use Youshido\GraphQL\Type\Scalar\StringType; |
24
|
|
|
use Youshido\GraphQL\Type\TypeInterface; |
25
|
|
|
use Youshido\GraphQL\Type\Union\UnionType; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* A query provider that looks into all controllers of your application to fetch queries. |
29
|
|
|
*/ |
30
|
|
|
class AggregateControllerQueryProvider implements QueryProviderInterface |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* @var Reader |
34
|
|
|
*/ |
35
|
|
|
private $annotationReader; |
36
|
|
|
/** |
37
|
|
|
* @var TypeMapperInterface |
38
|
|
|
*/ |
39
|
|
|
private $typeMapper; |
40
|
|
|
/** |
41
|
|
|
* @var HydratorInterface |
42
|
|
|
*/ |
43
|
|
|
private $hydrator; |
44
|
|
|
/** |
45
|
|
|
* @var array|\string[] |
46
|
|
|
*/ |
47
|
|
|
private $controllers; |
48
|
|
|
/** |
49
|
|
|
* @var ContainerInterface |
50
|
|
|
*/ |
51
|
|
|
private $container; |
52
|
|
|
/** |
53
|
|
|
* @var AuthenticationServiceInterface |
54
|
|
|
*/ |
55
|
|
|
private $authenticationService; |
56
|
|
|
/** |
57
|
|
|
* @var AuthorizationServiceInterface |
58
|
|
|
*/ |
59
|
|
|
private $authorizationService; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param string[] $controllers A list of controllers name in the container. |
63
|
|
|
*/ |
64
|
|
View Code Duplication |
public function __construct(array $controllers, ContainerInterface $container, Reader $annotationReader, TypeMapperInterface $typeMapper, HydratorInterface $hydrator, AuthenticationServiceInterface $authenticationService, AuthorizationServiceInterface $authorizationService) |
|
|
|
|
65
|
|
|
{ |
66
|
|
|
$this->controllers = $controllers; |
67
|
|
|
$this->container = $container; |
68
|
|
|
$this->annotationReader = $annotationReader; |
69
|
|
|
$this->typeMapper = $typeMapper; |
70
|
|
|
$this->hydrator = $hydrator; |
71
|
|
|
$this->authenticationService = $authenticationService; |
72
|
|
|
$this->authorizationService = $authorizationService; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return Field[] |
77
|
|
|
*/ |
78
|
|
View Code Duplication |
public function getQueries(): array |
|
|
|
|
79
|
|
|
{ |
80
|
|
|
$queryList = []; |
81
|
|
|
|
82
|
|
|
foreach ($this->controllers as $controllerName) { |
83
|
|
|
$controller = $this->container->get($controllerName); |
84
|
|
|
$queryProvider = new ControllerQueryProvider($controller, $this->annotationReader, $this->typeMapper, $this->hydrator, $this->authenticationService, $this->authorizationService); |
85
|
|
|
$queryList = array_merge($queryList, $queryProvider->getQueries()); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $queryList; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return Field[] |
93
|
|
|
*/ |
94
|
|
View Code Duplication |
public function getMutations(): array |
|
|
|
|
95
|
|
|
{ |
96
|
|
|
$mutationList = []; |
97
|
|
|
|
98
|
|
|
foreach ($this->controllers as $controllerName) { |
99
|
|
|
$controller = $this->container->get($controllerName); |
100
|
|
|
$queryProvider = new ControllerQueryProvider($controller, $this->annotationReader, $this->typeMapper, $this->hydrator); |
|
|
|
|
101
|
|
|
$mutationList = array_merge($mutationList, $queryProvider->getMutations()); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $mutationList; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
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.