Completed
Push — master ( f7b321...a68cc8 )
by Pavel
04:26
created

EntityRouteLoader::assertLoaded()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3.1406

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 4
cp 0.75
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 3
nc 2
nop 1
crap 3.1406
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  Route[][] */
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