EntityRouteLoader::getRoutes()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 4
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 6
1
<?php
2
3
namespace ScayTrase\Api\Cruds\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 EntityRouteLoader extends Loader
10
{
11
    const RESOURCE_TYPE = 'cruds_mount';
12
    /** @var  Route[][] */
13
    private $routes = [];
14
    /** @var bool[] */
15
    private $loaded = [];
16
17
    /** {@inheritdoc} */
18 2
    public function load($mount, $type = null)
19
    {
20 2
        $this->assertLoaded($mount);
21
22 2
        $collection = new RouteCollection();
23 2
        if (!array_key_exists($mount, $this->routes)) {
24
            return $collection;
25
        }
26
27 2
        foreach ($this->routes[$mount] as $name => $route) {
28 2
            $collection->add($name, $route);
29
        }
30
31 2
        $this->loaded[$mount] = true;
32
33 2
        return $collection;
34
    }
35
36
    /** {@inheritdoc} */
37 2
    public function supports($mount, $type = null): bool
38
    {
39 2
        return self::RESOURCE_TYPE === $type;
40
    }
41
42
    /**
43
     * @param string $mount
44
     * @param string $name
45
     * @param string $path
46
     * @param string $controller
47
     * @param array  $methods
48
     * @param array  $options
49
     *
50
     * @throws \LogicException
51
     */
52 2
    public function addRoute($mount, $name, $path, $controller, array $methods, array $options = [])
53
    {
54 2
        $this->assertLoaded($mount);
55
56 2
        $this->routes[$mount][$name] = CrudsRouteFactory::create($path, $controller, $methods, $options);
57 2
    }
58
59
    /**
60
     * @param string $mount
61
     *
62
     * @return Route[]
63
     *
64
     * @throws \OutOfBoundsException if mount does not exist
65
     */
66
    public function getRoutes($mount): array
67
    {
68
        if (!array_key_exists($mount, $this->routes)) {
69
            throw new \OutOfBoundsException('Mount does not exist');
70
        }
71
72
        return $this->routes[$mount];
73
    }
74
75
    /**
76
     * @return string[]
77
     */
78
    public function getMounts(): array
79
    {
80
        return array_keys($this->routes);
81
    }
82
83
    /**
84
     * @param $resource
85
     *
86
     * @throws \LogicException
87
     */
88 2
    private function assertLoaded($resource)
89
    {
90 2
        if (array_key_exists($resource, $this->loaded) && $this->loaded[$resource]) {
91
            throw new \LogicException('Already loaded');
92
        }
93 2
    }
94
}
95