Completed
Pull Request — master (#90)
by Arnaud
02:34
created

RouteNameGenerator   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 1
dl 0
loc 42
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B generate() 0 28 3
1
<?php
2
3
namespace LAG\AdminBundle\Routing;
4
5
use Exception;
6
use LAG\AdminBundle\Admin\Configuration\AdminConfiguration;
7
8
class RouteNameGenerator
9
{
10
    /**
11
     * Generate an admin route name using the pattern in the configuration.
12
     *
13
     * @param string             $actionName
14
     * @param string             $adminName
15
     * @param AdminConfiguration $configuration
16
     *
17
     * @return string
18
     *
19
     * @throws Exception
20
     */
21
    public function generate($actionName, $adminName, AdminConfiguration $configuration)
22
    {
23
        if (!$configuration->isResolved()) {
24
            throw new Exception('The configuration should be resolved before using it');
25
        }
26
        
27
        if (!array_key_exists($actionName, $configuration->getParameter('actions'))) {
28
            throw new Exception(
29
                sprintf('Invalid action name %s for admin %s (available action are: %s)',
30
                    $actionName,
31
                    $adminName,
32
                    implode(', ', array_keys($configuration->getParameter('actions'))))
33
            );
34
        }
35
        // generate the route name using the configured pattern
36
        $routeName = str_replace(
37
            '{admin}',
38
            strtolower($adminName),
39
            $configuration->getParameter('routing_name_pattern')
40
        );
41
        $routeName = str_replace(
42
            '{action}',
43
            $actionName,
44
            $routeName
45
        );
46
    
47
        return $routeName;
48
    }
49
}
50