Completed
Branch v2.0.0 (addc15)
by Alexander
03:27
created

Router   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 77
Duplicated Lines 31.17 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 5
dl 24
loc 77
c 0
b 0
f 0
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getRouter() 0 4 1
A init() 0 18 1
A frontIndex() 0 11 1
A dashboardIndex() 12 12 1
A dashboardAuth() 12 12 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * @author Alexander Torosh <[email protected]>
4
 */
5
6
namespace Web;
7
8
use Phalcon\Di\AbstractInjectionAware;
9
use Phalcon\Di\DiInterface;
10
use Phalcon\Events\Manager;
11
use Phalcon\Mvc\Router as PhalconRouter;
12
use Phalcon\Mvc\Router\Group;
13
14
class Router extends AbstractInjectionAware
15
{
16
    // @var $router Router
17
    private $router;
18
19
    public function __construct(DiInterface $container, Manager $eventsManager)
20
    {
21
        $this->setDI($container);
22
        $this->init();
23
        $this->router->setEventsManager($eventsManager);
24
    }
25
26
    /**
27
     * @return mixed
28
     */
29
    public function getRouter(): PhalconRouter
30
    {
31
        return $this->router;
32
    }
33
34
    private function init()
35
    {
36
        $router = new PhalconRouter();
37
        $router
38
            ->setDefaultModule('front')
39
            ->setDefaultController('index')
40
            ->setDefaultAction('index')
41
        ;
42
43
        // Frontend Router Groups
44
        $router->mount($this->frontIndex());
45
46
        // Backend Router Groups
47
        $router->mount($this->dashboardIndex());
48
        $router->mount($this->dashboardAuth());
49
50
        $this->router = $router;
51
    }
52
53
    private function frontIndex(): Group
54
    {
55
        $group = new Group([
56
            'controller' => 'index',
57
        ]);
58
        $group->setPrefix('/');
59
60
        $group->add('', ['action' => 'index'])->setName('homepage');
61
62
        return $group;
63
    }
64
65 View Code Duplication
    private function dashboardIndex(): Group
66
    {
67
        $group = new Group([
68
            'module' => 'dashboard',
69
            'controller' => 'index',
70
        ]);
71
        $group->setPrefix('/dashboard');
72
73
        $group->addGet('', ['action' => 'index'])->setName('dashboardIndex');
74
75
        return $group;
76
    }
77
78 View Code Duplication
    private function dashboardAuth(): Group
79
    {
80
        $group = new Group([
81
            'module' => 'dashboard',
82
            'controller' => 'auth',
83
        ]);
84
        $group->setPrefix('/dashboard/auth');
85
86
        $group->addGet('/login', ['action' => 'login'])->setName('dashboardLogin');
87
88
        return $group;
89
    }
90
}
91