Completed
Push — master ( 2b45da...60f6b8 )
by Arnaud
16s queued 12s
created

RoutingLoader   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Test Coverage

Coverage 17.5%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 43
c 3
b 0
f 0
dl 0
loc 130
ccs 7
cts 40
cp 0.175
rs 10
wmc 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setResolver() 0 2 1
A getResolver() 0 2 1
A load() 0 37 5
A supports() 0 3 1
A __construct() 0 10 1
A generateRouteName() 0 15 1
1
<?php
2
3
namespace LAG\AdminBundle\Routing;
4
5
use Exception;
6
use LAG\AdminBundle\Configuration\ApplicationConfiguration;
7
use LAG\AdminBundle\Configuration\ApplicationConfigurationStorage;
8
use LAG\AdminBundle\Controller\HomeAction;
9
use LAG\AdminBundle\Factory\ConfigurationFactory;
10
use LAG\AdminBundle\Resource\Registry\ResourceRegistryInterface;
11
use RuntimeException;
12
use Symfony\Component\Config\Loader\LoaderInterface;
13
use Symfony\Component\Config\Loader\LoaderResolverInterface;
14
use Symfony\Component\Routing\Route;
15
use Symfony\Component\Routing\RouteCollection;
16
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
17
18
/**
19
 * RoutingLoader.
20
 *
21
 * Creates routing for configured entities
22
 */
23
class RoutingLoader implements LoaderInterface
24
{
25
    /**
26
     * @var bool
27
     */
28
    private $loaded = false;
29
30
    /**
31
     * @var EventDispatcherInterface
32
     */
33
    private $eventDispatcher;
34
35
    /**
36
     * @var ApplicationConfiguration
37
     */
38
    private $applicationConfiguration;
39
40
    /**
41
     * @var ConfigurationFactory
42
     */
43
    private $configurationFactory;
44
45
    /**
46
     * @var ResourceRegistryInterface
47
     */
48
    private $registry;
49
50
    /**
51
     * RoutingLoader constructor.
52
     */
53
    public function __construct(
54
        ResourceRegistryInterface $registry,
55
        EventDispatcherInterface $eventDispatcher,
56
        ApplicationConfigurationStorage $applicationConfigurationStorage,
57
        ConfigurationFactory $configurationFactory
58
    ) {
59
        $this->eventDispatcher = $eventDispatcher;
60
        $this->applicationConfiguration = $applicationConfigurationStorage->getConfiguration();
61
        $this->configurationFactory = $configurationFactory;
62
        $this->registry = $registry;
63
    }
64
65
    /**
66
     * @return string
67
     */
68 4
    public static function generateRouteName(string $adminName, string $actionName, string $routingPattern)
69
    {
70
        // generate the route name using the configured pattern
71 4
        $routeName = str_replace(
72 4
            '{admin}',
73 4
            strtolower($adminName),
74
            $routingPattern
75
        );
76 4
        $routeName = str_replace(
77 4
            '{action}',
78
            $actionName,
79
            $routeName
80
        );
81
82 4
        return $routeName;
83
    }
84
85
    /**
86
     * Load the Admin's route.
87
     *
88
     * @param mixed  $routingResource
89
     * @param string $type
90
     *
91
     * @return RouteCollection
92
     *
93
     * @throws Exception
94
     */
95
    public function load($routingResource, $type = null)
96
    {
97
        if ($this->loaded) {
98
            throw new RuntimeException('Do not add the Admin "extra" loader twice');
99
        }
100
        $routes = new RouteCollection();
101
102
        foreach ($this->registry->all() as $name => $resource) {
103
            $configuration = $this
104
                ->configurationFactory
105
                ->createAdminConfiguration(
106
                    $resource->getName(),
107
                    $resource->getConfiguration(),
108
                    $this->applicationConfiguration
109
                )
110
            ;
111
112
            foreach ($configuration->getParameter('actions') as $actionName => $actionData) {
113
                $actionConfiguration = $this
114
                    ->configurationFactory
115
                    ->createActionConfiguration($actionName, $actionData, $name, $configuration)
116
                ;
117
                $route = new Route(
118
                    $actionConfiguration->getParameter('route_path'),
119
                    $actionConfiguration->getParameter('route_defaults'),
120
                    $actionConfiguration->getParameter('route_requirements')
121
                );
122
                $routes->add($actionConfiguration->getParameter('route'), $route);
123
            }
124
125
            if ($this->applicationConfiguration->getParameter('enable_homepage')) {
126
                $route = new Route('/', ['_controller' => HomeAction::class], []);
127
                $routes->add('lag.admin.homepage', $route);
128
            }
129
        }
130
131
        return $routes;
132
    }
133
134
    /**
135
     * Return true for the extra resource.
136
     *
137
     * @param mixed $resource
138
     * @param string $type
139
     *
140
     * @return bool
141
     */
142
    public function supports($resource, $type = null)
143
    {
144
        return 'extra' === $type;
145
    }
146
147
    public function getResolver()
148
    {
149
    }
150
151
    public function setResolver(LoaderResolverInterface $resolver)
152
    {
153
    }
154
}
155