Completed
Push — development ( 67765c...7029e6 )
by Andrij
18:12
created

FrontController   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 170
Duplicated Lines 3.53 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
dl 6
loc 170
rs 10
c 0
b 0
f 0
wmc 26
lcom 1
cbo 8

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
C display() 0 39 8
B main() 3 23 6
A page() 0 4 1
A category() 0 3 1
A shopCategory() 0 3 1
A product() 3 9 3
B module() 0 31 5

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 namespace core\src;
2
3
use CI;
4
use CMSFactory\Events;
5
use core\models\Route;
6
use core\src\Controller\CategoryController;
7
use core\src\Controller\PageController;
8
use core\src\Exception\PageNotFoundException;
9
10
class FrontController
11
{
0 ignored issues
show
introduced by
Opening brace of a class must be on the same line as the definition
Loading history...
12
13
    /**
14
     * @var CI
15
     */
16
    private $ci;
17
18
    /**
19
     * @var CoreModel
20
     */
21
    private $frontModel;
22
23
    /**
24
     * @var Router
25
     */
26
    private $router;
27
28
    /**
29
     * FrontController constructor.
30
     * @param CI $ci
31
     * @param CoreModel $frontModel
32
     * @param Router $router
33
     */
34
    public function __construct(CI $ci, CoreModel $frontModel, Router $router) {
35
        $this->ci = $ci;
36
        $this->frontModel = $frontModel;
37
        $this->router = $router;
38
    }
39
40
    /**
41
     * @param string $url
42
     * @throws PageNotFoundException
0 ignored issues
show
introduced by
Comment missing or not on the next line for @throws tag in function comment
Loading history...
43
     */
44
    public function display($url) {
45
46
        $route = $this->router->findRoute($url);
47
48
        if (!$route) {
49
            throw new PageNotFoundException();
50
        }
51
52
        $this->ci->core->core_data['data_type'] = $route->getType();
53
        $id = $route->getEntityId();
54
        $this->ci->core->core_data['id'] = $id;
55
56
        CoreFactory::getConfiguration()->setCurrentEntityType($route->getType());
57
        CoreFactory::getConfiguration()->setCurrentEntityId($id);
58
59
        switch ($route->getType()) {
60
            case Route::TYPE_MAIN:
61
                $this->main();
62
                break;
0 ignored issues
show
Coding Style introduced by
Case breaking statements must be followed by a single blank line
Loading history...
63
            case Route::TYPE_PRODUCT:
64
                $this->product($id);
65
                break;
0 ignored issues
show
Coding Style introduced by
Case breaking statements must be followed by a single blank line
Loading history...
66
            case Route::TYPE_SHOP_CATEGORY:
67
                $this->shopCategory($id);
68
                break;
0 ignored issues
show
Coding Style introduced by
Case breaking statements must be followed by a single blank line
Loading history...
69
            case Route::TYPE_CATEGORY:
70
                $this->category($id);
71
                break;
0 ignored issues
show
Coding Style introduced by
Case breaking statements must be followed by a single blank line
Loading history...
72
            case Route::TYPE_PAGE:
73
                $this->page($id);
74
                break;
0 ignored issues
show
Coding Style introduced by
Case breaking statements must be followed by a single blank line
Loading history...
75
            case Route::TYPE_MODULE:
76
                $this->module($route->getUrl());
77
                break;
0 ignored issues
show
Coding Style introduced by
Case breaking statements must be followed by a single blank line
Loading history...
78
            default:
79
                throw new PageNotFoundException();
80
81
        }
82
    }
83
84
    private function main() {
85
86 View Code Duplication
        if ($this->ci->input->get() || strstr($this->ci->input->server('REQUEST_URI'), '?')) {
87
            $this->ci->template->registerCanonical(site_url());
88
        }
89
90
        Events::create()->registerEvent(NULL, 'Core:_mainPage')->runFactory();
91
92
        $settings = CoreFactory::getConfiguration()->getSettings();
93
94
        switch ($settings['main_type']) {
95
            case 'page':
96
                Events::create()->registerEvent(NULL, 'Core:_mainPage')->runFactory();
97
                $this->page($settings['main_page_id']);
98
                break;
0 ignored issues
show
Coding Style introduced by
Case breaking statements must be followed by a single blank line
Loading history...
99
            case 'category':
100
                $this->category($settings['main_page_cat']);
101
                break;
0 ignored issues
show
Coding Style introduced by
Case breaking statements must be followed by a single blank line
Loading history...
102
            case 'module':
103
                $this->module($settings['main_page_module']);
104
                break;
105
        }
106
    }
107
108
    /**
109
     * @param int $id
110
     */
111
    private function page($id) {
112
113
        (new PageController($this->ci, $this->frontModel))->index($id);
114
    }
115
116
    /**
117
     * @param int $id
118
     */
119
    private function category($id) {
120
        (new CategoryController($this->ci, $this->frontModel))->index($id);
121
    }
122
123
    /**
124
     * @param int $id
125
     */
126
    private function shopCategory($id) {
127
        $this->module('shop/category/index/' . $id);
128
    }
129
130
    /**
131
     * @param int $id
132
     */
133
    private function product($id) {
134
135 View Code Duplication
        if ($this->ci->input->get() || strstr($this->ci->input->server('REQUEST_URI'), '?')) {
136
            $this->ci->template->registerCanonical(site_url($this->ci->uri->uri_string()));
137
        }
138
139
        $this->module('shop/product/index/' . $id);
140
141
    }
142
143
    /**
144
     * @param string $url
145
     * @throws PageNotFoundException
0 ignored issues
show
introduced by
Comment missing or not on the next line for @throws tag in function comment
Loading history...
146
     */
147
    private function module($url) {
148
149
        $moduleSegments = explode('/', $url);
150
151
        $moduleName = array_shift($moduleSegments);
152
        $moduleMethod = array_shift($moduleSegments);
153
        if ($moduleMethod == FALSE) {
154
            $moduleMethod = 'index';
155
        }
156
157
        $file = getModulePath($moduleName) . $moduleMethod . EXT;
158
        $this->core_data['module'] = $moduleName;
0 ignored issues
show
Bug introduced by
The property core_data does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
159
160
        if (file_exists($file)) {
161
            $moduleAction = array_shift($moduleSegments) ?: 'index';
162
            $moduleFullPath = $moduleName . '/' . $moduleMethod;
163
            $module = $this->ci->load->module($moduleName . '/' . $moduleMethod);
164
165
        } else {
166
            $moduleAction = $moduleMethod;
167
            $moduleFullPath = $moduleName . '/' . $moduleName;
168
            $module = $this->ci->load->module($moduleName);
169
        }
170
171
        $args = $moduleSegments;
172
        if (method_exists($module, $moduleAction)) {
173
            echo \modules::run($moduleFullPath . '/' . $moduleAction, $args);
174
        } else {
175
            throw new PageNotFoundException();
176
        }
177
    }
178
179
}