Completed
Pull Request — master (#19)
by David
13:03
created

Registry::getHydrator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
4
namespace TheCodingMachine\GraphQL\Controllers\Registry;
5
6
use Doctrine\Common\Annotations\Reader;
7
use Psr\Container\ContainerExceptionInterface;
8
use Psr\Container\ContainerInterface;
9
use Psr\Container\NotFoundExceptionInterface;
10
use TheCodingMachine\GraphQL\Controllers\HydratorInterface;
11
use TheCodingMachine\GraphQL\Controllers\Security\AuthenticationServiceInterface;
12
use TheCodingMachine\GraphQL\Controllers\Security\AuthorizationServiceInterface;
13
use TheCodingMachine\GraphQL\Controllers\TypeMapperInterface;
14
use Youshido\GraphQL\Type\Object\AbstractObjectType;
15
16
/**
17
 * The role of the registry is to provide access to all GraphQL types.
18
 * If the type is not found, it can be queried from the container, or if not in the container, it can be created from the Registry itself.
19
 */
20
class Registry implements RegistryInterface
21
{
22
23
    /**
24
     * @var ContainerInterface
25
     */
26
    private $container;
27
28
    /**
29
     * @var AbstractObjectType[]
30
     */
31
    private $values = [];
32
    /**
33
     * @var null|AuthorizationServiceInterface
34
     */
35
    private $authorizationService;
36
    /**
37
     * @var AuthenticationServiceInterface
38
     */
39
    private $authenticationService;
40
    /**
41
     * @var Reader
42
     */
43
    private $annotationReader;
44
    /**
45
     * @var TypeMapperInterface
46
     */
47
    private $typeMapper;
48
    /**
49
     * @var HydratorInterface
50
     */
51
    private $hydrator;
52
53
    /**
54
     * @param ContainerInterface $container The proxied container.
55
     */
56
    public function __construct(ContainerInterface $container, AuthorizationServiceInterface $authorizationService, AuthenticationServiceInterface $authenticationService, Reader $annotationReader, TypeMapperInterface $typeMapper, HydratorInterface $hydrator)
57
    {
58
        $this->container = $container;
59
        $this->authorizationService = $authorizationService;
60
        $this->authenticationService = $authenticationService;
61
        $this->annotationReader = $annotationReader;
62
        $this->typeMapper = $typeMapper;
63
        $this->hydrator = $hydrator;
64
    }
65
66
    /**
67
     * Finds an entry of the container by its identifier and returns it.
68
     *
69
     * @param string $id Identifier of the entry to look for.
70
     *
71
     * @throws NotFoundExceptionInterface  No entry was found for **this** identifier.
72
     * @throws ContainerExceptionInterface Error while retrieving the entry.
73
     *
74
     * @return mixed Entry.
75
     */
76
    public function get($id)
77
    {
78
        if (isset($this->values[$id])) {
79
            return $this->values[$id];
80
        }
81
        if ($this->container->has($id)) {
82
            return $this->container->get($id);
83
        }
84
85
        if (is_a($id, AbstractObjectType::class, true)) {
86
            $this->values[$id] = new $id($this);
87
            return $this->values[$id];
88
        }
89
90
        throw NotFoundException::notFound($id);
91
    }
92
93
    /**
94
     * Returns true if the container can return an entry for the given identifier.
95
     * Returns false otherwise.
96
     *
97
     * `has($id)` returning true does not mean that `get($id)` will not throw an exception.
98
     * It does however mean that `get($id)` will not throw a `NotFoundExceptionInterface`.
99
     *
100
     * @param string $id Identifier of the entry to look for.
101
     *
102
     * @return bool
103
     */
104
    public function has($id)
105
    {
106
        if (isset($this->values[$id])) {
107
            return true;
108
        }
109
        if ($this->container->has($id)) {
110
            return true;
111
        }
112
113
        if (is_a($id, AbstractObjectType::class, true)) {
114
            return true;
115
        }
116
117
        return false;
118
    }
119
120
    /**
121
     * Returns the authorization service.
122
     *
123
     * @return AuthorizationServiceInterface
124
     */
125
    public function getAuthorizationService(): AuthorizationServiceInterface
126
    {
127
        return $this->authorizationService;
128
    }
129
130
    /**
131
     * @return AuthenticationServiceInterface
132
     */
133
    public function getAuthenticationService(): AuthenticationServiceInterface
134
    {
135
        return $this->authenticationService;
136
    }
137
138
    /**
139
     * @return Reader
140
     */
141
    public function getAnnotationReader(): Reader
142
    {
143
        return $this->annotationReader;
144
    }
145
146
    /**
147
     * @return TypeMapperInterface
148
     */
149
    public function getTypeMapper(): TypeMapperInterface
150
    {
151
        return $this->typeMapper;
152
    }
153
154
    /**
155
     * @return HydratorInterface
156
     */
157
    public function getHydrator(): HydratorInterface
158
    {
159
        return $this->hydrator;
160
    }
161
}
162