Test Failed
Push — master ( a316f7...12ed59 )
by Julien
11:39
created

ModuleRoute::initialize()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 63
Code Lines 48

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 48
c 2
b 0
f 1
dl 0
loc 63
ccs 0
cts 26
cp 0
rs 9.1344
cc 3
nc 4
nop 0
crap 12

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * This file is part of the Zemit Framework.
4
 *
5
 * (c) Zemit Team <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE.txt
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Zemit\Mvc\Router;
12
13
use Phalcon\Mvc\Router\Group as RouterGroup;
14
15
/**
16
 * Class ModuleRoute
17
 *
18
 * @author Julien Turbide <[email protected]>
19
 * @copyright Zemit Team <[email protected]>
20
 *
21
 * @since 1.0
22
 * @version 1.0
23
 *
24
 * @package Zemit\Mvc\Router
25
 */
26
class ModuleRoute extends RouterGroup
27
{
28
    public array $locale;
29
    public bool $params;
30
    
31
    /**
32
     * ModuleRoute constructor.
33
     * The module routing is segmented in order to give more control over
34
     * the route for specific modules
35
     *
36
     * @param mixed $paths
37
     * @param array $locale
38
     * @param bool $params
39
     */
40
    public function __construct($paths = null, array $locale = [], bool $params = true)
41
    {
42
        $this->params = $params;
43
        $this->locale = $locale;
44
        parent::__construct($paths);
45
    }
46
    
47
    public function initialize()
48
    {
49
        $path = $this->getPaths();
50
        $module = $path['module'];
51
        
52
        $routePrefix = '/' . $module;
53
        $namePrefix = $module;
54
        
55
        $this->add($routePrefix, [
56
        ])->setName($namePrefix);
57
        
58
        $this->add($routePrefix . '/:controller', [
59
            'controller' => 1,
60
        ])->setName($namePrefix . '-controller');
61
        
62
        $this->add($routePrefix . '/:controller/:action/:params', [
63
            'controller' => 1,
64
            'action' => 2,
65
            'params' => 3,
66
        ])->setName($namePrefix . '-controller-action');
67
        
68
        if (!empty($this->locale)) {
69
            $localeRegex = '{locale:(' . implode('|', $this->locale) . ')}';
70
            $routePrefix = '/' . $localeRegex . '/' . $module;
71
            $namePrefix = 'locale-' . $module;
72
            
73
            $this->add($routePrefix, [
74
                'locale' => 1,
75
            ])->setName($namePrefix);
76
            
77
            $this->add($routePrefix . '/:controller', [
78
                'locale' => 1,
79
                'controller' => 2,
80
            ])->setName($namePrefix . '-controller');
81
            
82
            $this->add($routePrefix . '/:controller/:action/:params', [
83
                'locale' => 1,
84
                'controller' => 2,
85
                'action' => 3,
86
                'params' => 4,
87
            ])->setName($namePrefix . '-controller-action');
88
        }
89
        
90
        foreach ($this->locale as $locale) {
91
            $localeRegex = $locale;
92
            $routePrefix = '/' . $localeRegex . '/' . $module;
93
            $namePrefix = $locale . '-' . $module;
94
            
95
            $this->add($routePrefix, [
96
                'locale' => $locale,
97
            ])->setName($namePrefix);
98
            
99
            $this->add($routePrefix . '/:controller', [
100
                'locale' => $locale,
101
                'controller' => 1,
102
            ])->setName($namePrefix . '-controller');
103
            
104
            $this->add($routePrefix . '/:controller/:action/:params', [
105
                'locale' => $locale,
106
                'controller' => 1,
107
                'action' => 2,
108
                'params' => 3,
109
            ])->setName($namePrefix . '-controller-action');
110
        }
111
    }
112
}
113