Completed
Push — master ( 055f1b...f72a4b )
by Pavel
52s
created

EndpointRouteLoader   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 90%

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 18
cts 20
cp 0.9
rs 10

3 Methods

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