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

EndpointRouteLoader::supports()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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