Test Failed
Push — master ( 7bbbf4...34ff55 )
by Julien
04:57
created

Router::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 3
Bugs 2 Features 0
Metric Value
cc 1
eloc 1
c 3
b 2
f 0
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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\Bootstrap;
13
14
use Phalcon\Config\ConfigInterface;
15
16
/**
17
 * Zemit Router
18
 * {@inheritDoc}
19
 */
20
class Router extends \Zemit\Mvc\Router
21
{
22
    public array $defaults = [
23
        'namespace' => \Zemit\Modules\Frontend\Controller::class,
24
        'module' => 'frontend',
25
        'controller' => 'index',
26
        'action' => 'index',
27
    ];
28
    
29
    public array $notFound = [
30
        'controller' => 'error',
31
        'action' => 'notFound',
32
    ];
33
    
34
    /**
35
     * Router constructor.
36
     */
37 14
    public function __construct($defaultRoutes = true, ?ConfigInterface $config = null)
38
    {
39 14
        parent::__construct($defaultRoutes, $config);
40
    }
41
    
42 14
    public function baseRoutes(): void
43
    {
44 14
        $this->add('/', [
45 14
        ])->setName('default');
46
        
47 14
        $this->add('/:controller', [
48 14
            'controller' => 1,
49 14
        ])->setName('default-controller');
50
        
51 14
        $this->add('/:controller/:action/:params', [
52 14
            'controller' => 1,
53 14
            'action' => 2,
54 14
            'params' => 3,
55 14
        ])->setName('default-controller-action');
56
        
57 14
        $localeConfig = $this->getConfig()->get('locale')->toArray();
58
        
59 14
        if (!empty($localeConfig['allowed'])) {
60 14
            $localeRegex = '{locale:(' . implode('|', $localeConfig['allowed']) . ')}';
61
            
62 14
            $this->add('/' . $localeRegex, [
63 14
                'locale' => 1,
64 14
            ])->setName('locale');
65
            
66 14
            $this->add('/' . $localeRegex . '/:controller', [
67 14
                'locale' => 1,
68 14
                'controller' => 2,
69 14
            ])->setName('locale-controller');
70
            
71 14
            $this->add('/' . $localeRegex . '/:controller/:action/:params', [
72 14
                'locale' => 1,
73 14
                'controller' => 2,
74 14
                'action' => 3,
75 14
                'params' => 4,
76 14
            ])->setName('locale-controller-action');
77
        }
78
        
79 14
        foreach ($localeConfig['allowed'] as $locale) {
80 14
            $localeRegex = $locale;
81
            
82 14
            $this->add('/' . $localeRegex, [
83 14
                'locale' => $locale,
84 14
            ])->setName($locale);
85
            
86 14
            $this->add('/' . $localeRegex . '/:controller', [
87 14
                'locale' => $locale,
88 14
                'controller' => 1,
89 14
            ])->setName($locale . '-controller');
90
            
91 14
            $this->add('/' . $localeRegex . '/:controller/:action/:params', [
92 14
                'locale' => $locale,
93 14
                'controller' => 1,
94 14
                'action' => 2,
95 14
                'params' => 3,
96 14
            ])->setName($locale . '-controller-action');
97
        }
98
    }
99
}
100