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

ResourceLoader   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 32
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A loadRoutes() 0 24 3
A __construct() 0 4 1
1
<?php
2
3
namespace LAG\AdminBundle\Routing\Loader;
4
5
use LAG\AdminBundle\Admin\Resource\AdminResource;
6
use LAG\AdminBundle\Exception\ConfigurationException;
7
use LAG\AdminBundle\Exception\Exception;
8
use LAG\AdminBundle\Factory\Configuration\ActionConfigurationFactoryInterface;
9
use LAG\AdminBundle\Factory\Configuration\AdminConfigurationFactoryInterface;
10
use Symfony\Component\Routing\Route;
11
use Symfony\Component\Routing\RouteCollection;
12
13
class ResourceLoader implements ResourceLoaderInterface
14
{
15
    public function __construct(
16
        private AdminConfigurationFactoryInterface $adminConfigurationFactory,
17
        private ActionConfigurationFactoryInterface $actionConfigurationFactory,
18
    ) {
19
    }
20
21
    public function loadRoutes(AdminResource $resource, RouteCollection $routes): void
22
    {
23
        $configuration = $this
24
            ->adminConfigurationFactory
25
            ->create($resource->getName(), $resource->getConfiguration())
26
        ;
27
28
        foreach ($configuration->get('actions') as $name => $options) {
29
            try {
30
                $actionConfiguration = $this->actionConfigurationFactory->create(
31
                    $resource->getName(),
32
                    $name,
33
                    $options
34
                );
35
            } catch (Exception $exception) {
36
                throw new ConfigurationException('admin', $resource->getName(), $exception);
37
            }
38
            $route = new Route($actionConfiguration->getPath(), [
39
                '_controller' => $actionConfiguration->getController(),
40
                '_admin' => $actionConfiguration->getAdminName(),
41
                '_action' => $actionConfiguration->getName(),
42
            ], array_keys($actionConfiguration->getRouteParameters()));
43
44
            $routes->add($actionConfiguration->get('route'), $route);
45
        }
46
    }
47
}
48