Passed
Push — master ( 61ebc4...c33ad8 )
by Pavel
17:39
created

EntityRouteLoader   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 50
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A load() 0 15 3
A supports() 0 4 1
A addRoute() 0 19 2
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
    /** @var  Route[] */
12
    private $routes = [];
13
14
    private $loaded = false;
15
16
    /** {@inheritdoc} */
17
    public function load($resource, $type = null)
18
    {
19
        if ($this->loaded) {
20
            throw new \LogicException('Already loaded');
21
        }
22
23
        $collection = new RouteCollection();
24
        foreach ($this->routes as $name => $route) {
25
            $collection->add($name, $route);
26
        }
27
28
        $this->loaded = true;
29
30
        return $collection;
31
    }
32
33
    /** {@inheritdoc} */
34
    public function supports($resource, $type = null)
35
    {
36
        return 'cruds' === $type;
37
    }
38
39
    public function addRoute($name, $path, $controller,array $methods)
40
    {
41
        if ($this->loaded) {
42
            throw new \LogicException('Already loaded');
43
        }
44
45
        $this->routes[$name] = new Route(
46
            $path,
47
            [
48
                '_controller' => $controller,
49
            ],
50
            [],
51
            [],
52
            '',
53
            [],
54
            $methods,
55
            ''
56
        );
57
    }
58
}
59