Test Failed
Push — master ( c28bb2...68d0c2 )
by Julien
11:50
created

ModuleRoute::initialize()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 64
Code Lines 49

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 50
CRAP Score 6

Importance

Changes 3
Bugs 0 Features 1
Metric Value
cc 6
eloc 49
c 3
b 0
f 1
nc 12
nop 0
dl 0
loc 64
ccs 50
cts 50
cp 1
crap 6
rs 8.4905

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
/**
4
 * This file is part of the Zemit Framework.
5
 *
6
 * (c) Zemit Team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE.txt
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Zemit\Mvc\Router;
13
14
use Phalcon\Mvc\Router\Group as RouterGroup;
15
use Zemit\Utils\Slug;
16
17
class ModuleRoute extends RouterGroup
18
{
19
    public array $locale;
20
    
21 14
    public function __construct($paths = null, array $locale = [], ?string $hostname = null)
22
    {
23 14
        $this->locale = $locale;
24 14
        if (isset($hostname)) {
25
            $this->setHostname($hostname);
26
        }
27 14
        parent::__construct($paths);
28
    }
29
    
30 14
    public function initialize(): void
31
    {
32 14
        $hostname = $this->getHostname();
33 14
        $path = $this->getPaths();
34 14
        $module = $path['module'];
35 14
        $routePrefix = $hostname ? '' : '/' . $module;
36 14
        $namePrefix = $hostname ? Slug::generate($hostname) : $module;
37
        
38 14
        $this->add($routePrefix ?: '/', [
39 14
        ])->setName($namePrefix);
40
        
41 14
        $this->add($routePrefix . '/:controller', [
42 14
            'controller' => 1,
43 14
        ])->setName($namePrefix . '-controller');
44
        
45 14
        $this->add($routePrefix . '/:controller/:action/:params', [
46 14
            'controller' => 1,
47 14
            'action' => 2,
48 14
            'params' => 3,
49 14
        ])->setName($namePrefix . '-controller-action');
50
        
51 14
        if (!empty($this->locale)) {
52 14
            $localeRegex = '{locale:(' . implode('|', $this->locale) . ')}';
53
            
54 14
            $routePrefix = '/' . $localeRegex . '/' . $module;
55 14
            $namePrefix = 'locale-' . $module;
56
            
57 14
            $this->add($routePrefix, [
58 14
                'locale' => 1,
59 14
            ])->setName($namePrefix);
60
            
61 14
            $this->add($routePrefix . '/:controller', [
62 14
                'locale' => 1,
63 14
                'controller' => 2,
64 14
            ])->setName($namePrefix . '-controller');
65
            
66 14
            $this->add($routePrefix . '/:controller/:action/:params', [
67 14
                'locale' => 1,
68 14
                'controller' => 2,
69 14
                'action' => 3,
70 14
                'params' => 4,
71 14
            ])->setName($namePrefix . '-controller-action');
72
            
73 14
            foreach ($this->locale as $locale) {
74 14
                $localeRegex = $locale;
75
                
76 14
                $routePrefix = '/' . $localeRegex . '/' . $module;
77 14
                $namePrefix = $locale . '-' . $module;
78
                
79 14
                $this->add($routePrefix, [
80 14
                    'locale' => $locale,
81 14
                ])->setName($namePrefix);
82
                
83 14
                $this->add($routePrefix . '/:controller', [
84 14
                    'locale' => $locale,
85 14
                    'controller' => 1,
86 14
                ])->setName($namePrefix . '-controller');
87
                
88 14
                $this->add($routePrefix . '/:controller/:action/:params', [
89 14
                    'locale' => $locale,
90 14
                    'controller' => 1,
91 14
                    'action' => 2,
92 14
                    'params' => 3,
93 14
                ])->setName($namePrefix . '-controller-action');
94
            }
95
        }
96
    }
97
}
98