Passed
Pull Request — master (#300)
by Arnaud
14:15 queued 08:05
created

RoutingLoader   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 29
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A supports() 0 3 1
A load() 0 13 3
A __construct() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LAG\AdminBundle\Routing\Loader;
6
7
use LAG\AdminBundle\Admin\Resource\Registry\ResourceRegistryInterface;
8
use RuntimeException;
9
use Symfony\Component\Config\Loader\Loader;
10
use Symfony\Component\Routing\RouteCollection;
11
12
class RoutingLoader extends Loader
13
{
14
    private bool $loaded = false;
15
16
    public function __construct(
17
        private ResourceRegistryInterface $resourceRegistry,
18
        private ResourceLoaderInterface $resourceLoader,
19
    ) {
20
        parent::__construct();
21
    }
22
23
    public function load($resource, string $type = null): RouteCollection
24
    {
25
        if ($this->loaded) {
26
            throw new RuntimeException('Do not add the Admin routing loader "extra" twice');
27
        }
28
        $routes = new RouteCollection();
29
        $resources = $this->resourceRegistry->all();
30
31
        foreach ($resources as $resource) {
0 ignored issues
show
introduced by
$resource is overwriting one of the parameters of this function.
Loading history...
32
            $this->resourceLoader->loadRoutes($resource, $routes);
33
        }
34
35
        return $routes;
36
    }
37
38
    public function supports($resource, string $type = null): bool
39
    {
40
        return 'extra' === $type;
41
    }
42
}
43