Test Failed
Push — master ( 62e8aa...e74fb5 )
by Julien
04:23
created

ModuleRoute   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
eloc 54
c 3
b 0
f 1
dl 0
loc 85
ccs 0
cts 53
cp 0
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A initialize() 0 64 3
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
16
class ModuleRoute extends RouterGroup
17
{
18
    public array $locale;
19
    public bool $params;
20
    
21
    /**
22
     * ModuleRoute constructor.
23
     * The module routing is segmented in order to give more control over
24
     * the route for specific modules
25
     *
26
     * @param mixed $paths
27
     * @param array $locale
28
     * @param bool $params
29
     */
30
    public function __construct($paths = null, array $locale = [], bool $params = true)
31
    {
32
        $this->params = $params;
33
        $this->locale = $locale;
34
        parent::__construct($paths);
35
    }
36
    
37
    public function initialize(): void
38
    {
39
        $path = $this->getPaths();
40
        $module = $path['module'];
41
        $routePrefix = '/' . $module;
42
        $namePrefix = $module;
43
        
44
        $this->add($routePrefix, [
45
        ])->setName($namePrefix);
46
        
47
        $this->add($routePrefix . '/:controller', [
48
            'controller' => 1,
49
        ])->setName($namePrefix . '-controller');
50
        
51
        $this->add($routePrefix . '/:controller/:action/:params', [
52
            'controller' => 1,
53
            'action' => 2,
54
            'params' => 3,
55
        ])->setName($namePrefix . '-controller-action');
56
        
57
        if (!empty($this->locale)) {
58
            $localeRegex = '{locale:(' . implode('|', $this->locale) . ')}';
59
            
60
            $routePrefix = '/' . $localeRegex . '/' . $module;
61
            $namePrefix = 'locale-' . $module;
62
            
63
            $this->add($routePrefix, [
64
                'locale' => 1,
65
            ])->setName($namePrefix);
66
            
67
            $this->add($routePrefix . '/:controller', [
68
                'locale' => 1,
69
                'controller' => 2,
70
            ])->setName($namePrefix . '-controller');
71
            
72
            $this->add($routePrefix . '/:controller/:action/:params', [
73
                'locale' => 1,
74
                'controller' => 2,
75
                'action' => 3,
76
                'params' => 4,
77
            ])->setName($namePrefix . '-controller-action');
78
        }
79
        
80
        foreach ($this->locale as $locale) {
81
            $localeRegex = $locale;
82
            
83
            $routePrefix = '/' . $localeRegex . '/' . $module;
84
            $namePrefix = $locale . '-' . $module;
85
            
86
            $this->add($routePrefix, [
87
                'locale' => $locale,
88
            ])->setName($namePrefix);
89
            
90
            $this->add($routePrefix . '/:controller', [
91
                'locale' => $locale,
92
                'controller' => 1,
93
            ])->setName($namePrefix . '-controller');
94
            
95
            $this->add($routePrefix . '/:controller/:action/:params', [
96
                'locale' => $locale,
97
                'controller' => 1,
98
                'action' => 2,
99
                'params' => 3,
100
            ])->setName($namePrefix . '-controller-action');
101
        }
102
    }
103
}
104