Completed
Push — master ( 9a342a...a162ae )
by Andrii
13:06
created

EndpointRepository::has()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
4
namespace hiapi\Core\Endpoint;
5
6
use hiapi\exceptions\ConfigurationException;
7
use Psr\Container\ContainerInterface;
8
9
class EndpointRepository
10
{
11
    /**
12
     * @var ContainerInterface
13
     */
14
    private $container;
15
    /**
16
     * @var BuilderFactory
17
     */
18
    private $builderFactory;
19
20
    private $endpoints = [];
21
22
    public function __construct(array $endpoints, ContainerInterface $container, BuilderFactory $builderFactory)
23
    {
24
        foreach ($endpoints as $name => $handler) {
25
            $this->addEndpoint($name, $handler);
26
        }
27
        $this->container = $container;
28
        $this->builderFactory = $builderFactory;
29
    }
30
31
    public function has(string $name): bool
32
    {
33
        return isset($this->getEndpoints()[$name]);
34
    }
35
36
    public function addEndpoint(string $name, string $handlerClassName): self
37
    {
38
        $this->endpoints[$name] = $handlerClassName;
39
40
        return $this;
41
    }
42
43
    public function getByName(string $name): Endpoint
44
    {
45
        if (!$this->has($name)) {
46
            throw new ConfigurationException(sprintf('Endpoint %s does not exist', $name));
47
        }
48
49
        return $this->container->get($this->getEndpoints()[$name])($this->builderFactory);
50
    }
51
52
    private function getEndpoints(): iterable
53
    {
54
        return $this->endpoints;
55
    }
56
}
57