ControllerName   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 1
dl 0
loc 70
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 2
A generate() 0 7 2
A getActionSuffix() 0 6 2
A getControllerName() 0 8 2
A generateControllerHierarchy() 0 10 2
B getPrefix() 0 17 5
1
<?php
2
3
namespace Knp\RadBundle\Routing\Conventional\Generator;
4
5
use Knp\RadBundle\Routing\Conventional\Config;
6
use Knp\RadBundle\Routing\Conventional\Generator;
7
8
class ControllerName implements Generator
9
{
10
    private $map = array(
11
        'create' => 'new',
12
        'update' => 'edit',
13
    );
14
15
    public function __construct(array $map = null)
16
    {
17
        $this->map = $map ?: $this->map;
18
    }
19
20
    public function generate(Config $config)
21
    {
22
        $controller = $this->getControllerName($config);
23
        $name = isset($this->map[$config->name]) ? $this->map[$config->name] : $config->name;
24
25
        return sprintf('%s:%s%s', $controller, $name, $this->getActionSuffix($config));
26
    }
27
28
    private function getActionSuffix(Config $config)
29
    {
30
        if ($config->getController()) {
31
            return 'Action';
32
        }
33
    }
34
35
    private function getControllerName(Config $config)
36
    {
37
        if ($config->getController()) {
38
            return $config->getController();
39
        }
40
41
        return $this->generateControllerHierarchy($config);
42
    }
43
44
    private function generateControllerHierarchy(Config $config)
45
    {
46
        $parts = array();
47
        if ($config->parent) {
48
            $parts[] = $this->generateControllerHierarchy($config->parent);
49
        }
50
        $parts[] = $this->getPrefix($config);
51
52
        return implode('/', array_filter($parts));
53
    }
54
55
    /**
56
     * prefix is the name (by default). This name can be of form: "App:Something"
57
     *
58
     * @return string
59
     **/
60
    private function getPrefix(Config $config)
61
    {
62
        $parts = explode(':', $config->name);
63
64
        if (1 === count($parts)) {
65
            return;
66
        }
67
68
        if ($config->parent && 2 === count($parts)) {
69
            array_shift($parts);
70
            return implode('/', $parts);
71
        }
72
73
        if (!$config->parent) {
74
            return implode(':', $parts);
75
        }
76
    }
77
}
78