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

EndpointRouteLoader::addEndpoint()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

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