Completed
Push — master ( 58bdf2...4883e1 )
by Pavel
03:39
created

EndpointRouteLoader   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 45
ccs 20
cts 20
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A supports() 0 4 1
A load() 0 21 3
A addEndpoint() 0 8 2
1
<?php
2
3
namespace Bankiru\Api\Rpc\Http\Routing;
4
5
use Symfony\Component\Config\Loader\Loader;
6
use Symfony\Component\Routing\Route;
7
use Symfony\Component\Routing\RouteCollection;
8
9
final class EndpointRouteLoader extends Loader
10
{
11
    /** @var bool */
12
    private $loaded = false;
13
    /** @var array */
14
    private $endpoints = [];
15
16
    /** {@inheritdoc} */
17 2
    public function load($resource, $type = null)
18
    {
19 2
        if ($this->loaded) {
20 1
            throw new \LogicException('Endpoint loader is already loaded');
21
        }
22
23 2
        $collection = new RouteCollection();
24 2
        foreach ($this->endpoints as $name => $endpoint) {
25
            // prepare a new route
26 2
            $path     = $endpoint['path'];
27 2
            $defaults = $endpoint['defaults'];
28 2
            $route    = new Route($path, $defaults);
29 2
            $route->setMethods('POST');
30
31 2
            $collection->add($name, $route);
32 2
        }
33
34 2
        $this->loaded = true;
35
36 2
        return $collection;
37
    }
38
39
    /** {@inheritdoc} */
40 2
    public function supports($resource, $type = null)
41
    {
42 2
        return 'endpoint' === $type;
43
    }
44
45 2
    public function addEndpoint($name, array $endpoint)
46
    {
47 2
        if ($this->loaded) {
48 1
            throw new \LogicException('Endpoint loader is already configured');
49
        }
50
51 2
        $this->endpoints[$name] = $endpoint;
52 2
    }
53
}
54