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

Core   B

Complexity

Total Complexity 54

Size/Duplication

Total Lines 326
Duplicated Lines 6.75 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
dl 22
loc 326
rs 7.0642
c 0
b 0
f 0
wmc 54
lcom 1
cbo 8

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A index() 0 3 1
B grab_variables() 0 22 6
A error() 0 11 1
A error_404() 0 8 1
F set_meta_tags() 0 67 23
A _makeDescription() 0 8 3
A _makeKeywords() 0 10 3
B robots() 22 36 5
C setLastModified() 0 22 7
B adminAutoload() 0 42 3

How to fix   Duplicated Code    Complexity   

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like Core often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Core, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
use CMSFactory\DependencyInjection\DependencyInjectionProvider;
4
use CMSFactory\Events;
5
use core\src\CoreFactory;
6
7
if (!defined('BASEPATH')) {
8
    exit('No direct script access allowed');
9
}
10
11
/**
12
 *
13
 * Image CMS
14
 *
15
 * core.php
0 ignored issues
show
introduced by
Doc comment long description must start with a capital letter
Loading history...
16
 * @property Cms_base $cms_base
17
 * @property Lib_category $lib_category
18
 * @property Cfcm $cfcm
19
 * @property Lib_seo lib_seo
20
 */
21
class Core extends MY_Controller
22
{
0 ignored issues
show
introduced by
Opening brace of a class must be on the same line as the definition
Loading history...
23
24
    /**
25
     * @var array
26
     */
27
    public $cat_content;
28
29
    /**
30
     * @var int
31
     */
32
    public $page_content;
33
34
    /**
35
     * @var array
36
     */
37
    public $core_data = ['data_type' => null];
38
39
    /**
40
     * @var array
41
     */
42
    public $settings = [];
43
44
    /**
45
     * @var array
46
     */
47
    public $def_lang = []; // Modules array
48
49
    /**
50
     * @var array
51
     */
52
    public $langs = [];
53
54
    public function __construct() {
55
56
        parent::__construct();
57
        Modules::$registry['core'] = $this;
58
        $lang = new MY_Lang();
59
        $lang->load('core');
60
        $this->settings = $this->cms_base->get_settings();
61
        $this->langs = CoreFactory::getModel()->getLanguages();
0 ignored issues
show
Documentation Bug introduced by
It seems like \core\src\CoreFactory::getModel()->getLanguages() of type * is incompatible with the declared type array of property $langs.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
62
        $this->def_lang = [CoreFactory::getModel()->getDefaultLanguage()];
63
64
    }
65
66
    public function index() {
67
        (new \core\src\Kernel($this, CI::$APP))->run();
68
    }
69
70
    /**
71
     * Used in other modules
72
     * todo: move(and simplify) or remove
73
     * @param $n
0 ignored issues
show
introduced by
Missing parameter type
Loading history...
74
     * @return array
75
     */
76
    public function grab_variables($n) {
77
78
        $args = [];
79
80
        foreach ($this->uri->uri_to_assoc($n) as $k => $v) {
81
            if (isset($k)) {
82
                array_push($args, $k);
83
            }
84
            if (isset($v)) {
85
                array_push($args, $v);
86
            }
87
        }
88
89
        $count = count($args);
90
        for ($i = 0, $cnt = $count; $i < $cnt; $i++) {
91
            if ($args[$i] === FALSE) {
92
                unset($args[$i]);
93
            }
94
        }
95
96
        return $args;
97
    }
98
99
    /**
100
     * Display error template end exit
101
     * @param string $text
102
     * @param bool $back
103
     */
104
    public function error($text, $back = TRUE) {
105
106
        $this->template->add_array(
107
            [
108
             'content' => $this->template->read('error', ['error_text' => $text, 'back_button' => $back]),
109
            ]
110
        );
111
112
        $this->template->show();
113
        exit;
114
    }
115
116
    /**
117
     * Page not found
118
     * Show 404 error
119
     */
120
    public function error_404() {
121
122
        header('HTTP/1.1 404 Not Found');
123
        $this->set_meta_tags(lang('Page not found', 'core'));
124
        $this->template->assign('error_text', lang('Page not found.', 'core'));
125
        $this->template->show('404');
126
        exit;
127
    }
128
129
    /**
130
     * Set meta tags for pages
131
     * @param string $title
132
     * @param string $keywords
133
     * @param string $description
134
     * @param string $page_number
135
     * @param int $showsitename
136
     * @param string $category
137
     */
138
    public function set_meta_tags($title = '', $keywords = '', $description = '', $page_number = '', $showsitename = 0, $category = '') {
139
140
        if ($this->core_data['data_type'] == 'main') {
141
            $this->template->add_array(
142
                [
143
                 'site_title'       => empty($this->settings['site_title']) ? $title : $this->settings['site_title'],
144
                 'site_description' => empty($this->settings['site_description']) ? $description : $this->settings['site_description'],
145
                 'site_keywords'    => empty($this->settings['site_keywords']) ? $keywords : $this->settings['site_keywords'],
146
                ]
147
            );
148
        } else {
149
            if (($page_number > 1) && ($page_number != '')) {
150
                $title = lang('Page', 'core') . ' №' . $page_number . ' - ' . $title;
151
            }
152
153
            if ($description != '') {
154
                if ($page_number > 1 && $page_number != '') {
155
                    $description = "$page_number - $description {$this->settings['delimiter']} {$this->settings['site_short_title']}";
156
                } else {
157
                    $description = "$description {$this->settings['delimiter']} {$this->settings['site_short_title']}";
158
                }
159
            }
160
161
            if ($this->settings['add_site_name_to_cat']) {
162
                if ($category != '') {
163
                    $title .= ' - ' . $category;
164
                }
165
            }
166
167
            if ($this->core_data['data_type'] == 'page' AND $this->page_content['category'] != 0 AND $this->settings['add_site_name_to_cat']) {
168
                $title .= ' ' . $this->settings['delimiter'] . ' ' . $this->cat_content['name'];
169
            }
170
171
            if (is_array($title)) {
172
                $n_title = '';
173
                foreach ($title as $k => $v) {
174
                    $n_title .= $v;
175
176
                    if ($k < count($title) - 1) {
177
                        $n_title .= ' ' . $this->settings['delimiter'] . ' ';
178
                    }
179
                }
180
                $title = $n_title;
181
            }
182
183
            if ($this->settings['add_site_name'] == 1 && $showsitename != 1) {
184
                $title .= ' ' . $this->settings['delimiter'] . ' ' . $this->settings['site_short_title'];
185
            }
186
187
            if ($this->settings['create_description'] == 'empty') {
188
                $description = '';
189
            }
190
            if ($this->settings['create_keywords'] == 'empty') {
191
                $keywords = '';
192
            }
193
194
            $page_number = $page_number ?: (int) $this->pagination->cur_page;
195
            $this->template->add_array(
196
                [
197
                 'site_title'       => $title,
198
                 'site_description' => htmlspecialchars($description),
199
                 'site_keywords'    => htmlspecialchars($keywords),
200
                 'page_number'      => $page_number,
201
                ]
202
            );
203
        }
204
    }
205
206
    /**
207
     *
208
     * @param string $description
209
     * @param null|string $text
210
     * @return string
211
     */
212
    public function _makeDescription($description, $text = null) {
213
214
        if ($this->settings['create_description'] == 'auto' && !$description) {
215
            $description = $this->lib_seo->get_description($text);
216
        }
217
218
        return $description;
219
    }
220
221
    /**
222
     *
223
     * @param string $keywords
224
     * @param string $text
225
     * @return string
226
     */
227
    public function _makeKeywords($keywords, $text) {
228
229
        if ($this->settings['create_keywords'] == 'auto' && !$keywords) {
230
            $keywords = $this->lib_seo->get_keywords($text, TRUE);
231
232
            $keywords = implode(', ', array_keys($keywords));
233
        }
234
235
        return $keywords;
236
    }
237
238
    public function robots() {
239
240
        $robotsSettings = $this->db->select('robots_settings,robots_settings_status,robots_status')->get('settings');
241
        if ($robotsSettings) {
242
            $robotsSettings = $robotsSettings->row();
243
        }
244
245
        header('Content-type: text/plain');
246
        if ($robotsSettings->robots_status == '1') {
247
            if ($robotsSettings->robots_settings_status == '1') {
248
                if (trim($robotsSettings->robots_settings)) {
249
                    echo $robotsSettings->robots_settings;
250
                    exit;
251 View Code Duplication
                } else {
252
                    header('Content-type: text/plain');
253
                    echo "User-agent: * \r\nDisallow: /";
254
                    echo "\r\nHost: " . $this->input->server('HTTP_HOST');
255
                    echo "\r\nSitemap: " . site_url('sitemap.xml');
256
                    exit;
257
                }
258 View Code Duplication
            } else {
259
260
                header('Content-type: text/plain');
261
                echo "User-agent: * \r\nDisallow: ";
262
                echo "\r\nHost: " . $this->input->server('HTTP_HOST');
263
                echo "\r\nSitemap: " . site_url('sitemap.xml');
264
                exit;
265
            }
266 View Code Duplication
        } else {
267
            header('Content-type: text/plain');
268
            echo "User-agent: * \r\nDisallow: /";
269
            echo "\r\nHost: " . $this->input->server('HTTP_HOST');
270
            echo "\r\nSitemap: " . site_url('sitemap.xml');
271
            exit;
272
        }
273
    }
274
275
    /**
276
     *
277
     * @param int $LastModified_unix
278
     * @return void
0 ignored issues
show
introduced by
If there is no return value for a function, there must not be a @return tag.
Loading history...
279
     */
280
    public function setLastModified($LastModified_unix) {
281
282
        if ($LastModified_unix < time() - 60 * 60 * 24 * 4 or $LastModified_unix > time()) {
283
            if (in_array(date('D', time()), ['Mon', 'Tue', 'Wen'])) {
284
                $LastModified_unix = strtotime('last sunday', time());
285
            } else {
286
                $LastModified_unix = strtotime('last thursday', time());
287
            }
288
        }
289
290
        $LastModified = date('D, d M Y H:i:s \G\M\T', $LastModified_unix);
291
        $IfModifiedSince = false;
292
293
        if ($this->input->server('HTTP_IF_MODIFIED_SINCE')) {
294
            $IfModifiedSince = strtotime(substr($this->input->server('HTTP_IF_MODIFIED_SINCE'), 5));
295
        }
296
        if ($IfModifiedSince && $IfModifiedSince >= $LastModified_unix) {
297
            header($this->input->server('SERVER_PROTOCOL') . ' 304 Not Modified');
298
            return;
299
        }
300
        header('Last-Modified: ' . $LastModified);
301
    }
302
303
    public static function adminAutoload() {
304
        $subscriber = new \core\src\RouteSubscriber();
305
306
        foreach ($subscriber->getHandlers() as $eventName => $callback) {
307
308
            Events::create()->on($eventName)->setListener([$subscriber, $callback]);
309
310
        }
311
312
        $events = [
313
                   'ShopAdminCategories:create',
314
                   'ShopAdminCategories:edit',
315
                   'ShopAdminCategories:delete',
316
                   'ShopAdminCategories:fastCreate',
317
                   'ShopAdminCategories:ajaxChangeShowInSite',
318
319
                   'ShopAdminProducts:create',
320
                   'ShopAdminProducts:edit',
321
                   'ShopAdminProducts:delete',
322
                   'ShopAdminProducts:fastProdCreate',
323
                   'ShopAdminProducts:ajaxChangeActive',
324
                   'ShopAdminProducts:ajaxChangeStatus',
325
326
                   'ShopAdminProperties:fastCreate',
327
                   'ShopAdminProperties:create',
328
                   'ShopAdminProperties::delete',
329
                   'ShopAdminProperties:edit',
330
331
                  ];
332
333
        foreach ($events as $event) {
334
335
            Events::create()->on($event)->setListener(
336
                function () {
337
338
                    MY_Controller::dropCache();
339
                }
340
            );
341
342
        }
343
344
    }
345
346
}
347
348
/* End of file core.php */