Registry::getAuthorizationService()   A
last analyzed

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\Tdbm\GraphQL\Registry;
5
6
use Psr\Container\ContainerExceptionInterface;
7
use Psr\Container\ContainerInterface;
8
use Psr\Container\NotFoundExceptionInterface;
9
use TheCodingMachine\GraphQL\Controllers\Security\AuthorizationServiceInterface;
10
use Youshido\GraphQL\Type\Object\AbstractObjectType;
11
12
/**
13
 * The role of the registry is to provide access to all GraphQL types.
14
 * 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.
15
 *
16
 * @deprecated Use TheCodingMachine\GraphQL\Controllers\Registry instead
17
 */
18
class Registry implements ContainerInterface
19
{
20
21
    /**
22
     * @var ContainerInterface
23
     */
24
    private $container;
25
26
    /**
27
     * @var AbstractObjectType[]
28
     */
29
    private $values = [];
30
    /**
31
     * @var null|AuthorizationServiceInterface
32
     */
33
    private $authorizationService;
34
35
    /**
36
     * @param ContainerInterface $container The proxied container.
37
     * @param AuthorizationServiceInterface|null $authorizationService
38
     */
39
    public function __construct(ContainerInterface $container, AuthorizationServiceInterface $authorizationService = null)
40
    {
41
        $this->container = $container;
42
        $this->authorizationService = $authorizationService;
43
    }
44
45
    /**
46
     * Finds an entry of the container by its identifier and returns it.
47
     *
48
     * @param string $id Identifier of the entry to look for.
49
     *
50
     * @throws NotFoundExceptionInterface  No entry was found for **this** identifier.
51
     * @throws ContainerExceptionInterface Error while retrieving the entry.
52
     *
53
     * @return mixed Entry.
54
     */
55
    public function get($id)
56
    {
57
        if (isset($this->values[$id])) {
58
            return $this->values[$id];
59
        }
60
        if ($this->container->has($id)) {
61
            return $this->container->get($id);
62
        }
63
64
        if (is_a($id, AbstractObjectType::class, true)) {
65
            $this->values[$id] = new $id($this);
66
            return $this->values[$id];
67
        }
68
69
        throw NotFoundException::notFound($id);
70
    }
71
72
    /**
73
     * Returns true if the container can return an entry for the given identifier.
74
     * Returns false otherwise.
75
     *
76
     * `has($id)` returning true does not mean that `get($id)` will not throw an exception.
77
     * It does however mean that `get($id)` will not throw a `NotFoundExceptionInterface`.
78
     *
79
     * @param string $id Identifier of the entry to look for.
80
     *
81
     * @return bool
82
     */
83
    public function has($id)
84
    {
85
        if (isset($this->values[$id])) {
86
            return true;
87
        }
88
        if ($this->container->has($id)) {
89
            return true;
90
        }
91
92
        if (is_a($id, AbstractObjectType::class, true)) {
93
            return true;
94
        }
95
96
        return false;
97
    }
98
99
    /**
100
     * Returns the authorization service.
101
     *
102
     * @return AuthorizationServiceInterface|null
103
     */
104
    public function getAuthorizationService(): ?AuthorizationServiceInterface
105
    {
106
        return $this->authorizationService;
107
    }
108
}
109