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

EndpointRouteLoader::addEndpoint()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 2
crap 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