Completed
Push — master ( 32f082...893399 )
by Dmitry
12:23
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 Psalm\Type\Atomic\TLiteralClassString;
8
use Psr\Container\ContainerInterface;
9
10
class EndpointRepository
11
{
12
    private ContainerInterface $container;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
13
14
    private BuilderFactory $builderFactory;
15
16
    /**
17
     * @var array<string, string|TLiteralClassString>
18
     */
19
    private array $endpoints = [];
20
21
    public function __construct(array $endpoints, ContainerInterface $container, BuilderFactory $builderFactory)
22
    {
23
        foreach ($endpoints as $name => $handler) {
24
            $this->addEndpoint($name, $handler);
25
        }
26
        $this->container = $container;
27
        $this->builderFactory = $builderFactory;
28
    }
29
30
    public function has(string $name): bool
31
    {
32
        return isset($this->endpoints[$name]);
33
    }
34
35
    /**
36
     * @param string $name
37
     * @param string $handlerClassName
38
     * @psalm-param TLiteralClassString $handlerClassName
39
     * @return $this
40
     */
41
    public function addEndpoint(string $name, string $handlerClassName): self
42
    {
43
        $this->endpoints[$name] = $handlerClassName;
44
45
        return $this;
46
    }
47
48
    public function getByName(string $name): Endpoint
49
    {
50
        if (!$this->has($name)) {
51
            throw new ConfigurationException(sprintf('Endpoint %s does not exist', $name));
52
        }
53
54
        return $this->container->get($this->endpoints[$name])($this->builderFactory);
55
    }
56
57
    /**
58
     * @psalm-return list<string>
59
     */
60
    public function getEndpointNames(): array
61
    {
62
        return array_keys($this->endpoints);
63
    }
64
}
65