Completed
Push — master ( 839f22...1b3a7e )
by Pavel
04:14
created

EntityRouteLoader::getRoutes()   A

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
class EntityRouteLoader extends Loader
10
{
11
    const RESOURCE_TYPE = 'cruds_mount';
12
    /** @var  CrudsRoute[][] */
13
    private $routes = [];
14
    /** @var bool[] */
15
    private $loaded = [];
16
17
    /** {@inheritdoc} */
18 12
    public function load($mount, $type = null)
19
    {
20 12
        $this->assertLoaded($mount);
21
22 12
        $collection = new RouteCollection();
23 12
        if (!array_key_exists($mount, $this->routes)) {
24
            throw new \LogicException(sprintf('No routes configured for %s CRUDS mount point', $mount));
25
        }
26
27 12
        foreach ($this->routes[$mount] as $name => $route) {
28 12
            $collection->add($name, $route);
29 12
        }
30
31 12
        $this->loaded = true;
0 ignored issues
show
Documentation Bug introduced by
It seems like true of type boolean is incompatible with the declared type array<integer,boolean> of property $loaded.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
32
33 12
        return $collection;
34
    }
35
36
    /** {@inheritdoc} */
37 12
    public function supports($mount, $type = null)
38
    {
39 12
        return self::RESOURCE_TYPE === $type;
40
    }
41
42 12
    public function addRoute($mount, $name, $path, $controller, array $methods, array $options = [])
43
    {
44 12
        $this->assertLoaded($mount);
45
46 12
        $this->routes[$mount][$name] = CrudsRoute::create($path, $controller, $methods, $options);
47 12
    }
48
49
    /**
50
     * @param $resource
51
     */
52 12
    private function assertLoaded($resource)
53
    {
54 12
        if (array_key_exists($resource, $this->loaded) && $this->loaded[$resource]) {
55
            throw new \LogicException('Already loaded');
56
        }
57 12
    }
58
59
    /**
60
     * @param string $mount
61
     *
62
     * @return CrudsRoute[]
63
     */
64
    public function getRoutes($mount)
65
    {
66
        if (!array_key_exists($mount, $this->routes)){
67
            throw new \OutOfBoundsException('Mount does not exist');
68
        }
69
70
        return $this->routes[$mount];
71
    }
72
73
    /**
74
     * @return string[]
75
     */
76
    public function getMounts()
77
    {
78
        return array_keys($this->routes);
79
    }
80
}
81