Completed
Push — development ( a150a5...f82eb6 )
by Andrij
17:01
created

Mod_seo   C

Complexity

Total Complexity 58

Size/Duplication

Total Lines 382
Duplicated Lines 37.96 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 9
Bugs 6 Features 0
Metric Value
c 9
b 6
f 0
dl 145
loc 382
rs 6.3005
wmc 58
lcom 1
cbo 9

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
C _buildBrandMeta() 15 44 8
A setMetaTags() 0 4 1
A _buildBrandsMeta() 0 12 2
C _buildCategoryMeta() 30 77 16
C _buildPageCategoryMeta() 43 43 8
C _buildPageMeta() 42 42 8
B _buildProductsMeta() 15 56 9
A _buildSearchMeta() 0 19 2
A _deinstall() 0 4 1
A _install() 0 4 1
A autoload() 0 13 1

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 Mod_seo 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 Mod_seo, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
use CMSFactory\Events;
4
use CMSFactory\MetaManipulator\MetaManipulator;
5
use CMSFactory\MetaManipulator\MetaStorage;
6
use CMSFactory\MetaManipulator\PageCategoryMetaManipulator;
7
use CMSFactory\MetaManipulator\PageMetaManipulator;
8
use CMSFactory\MetaManipulator\ShopBrandMetaManipulator;
9
use CMSFactory\MetaManipulator\ShopCategoryMetaManipulator;
10
use CMSFactory\MetaManipulator\ShopProductMetaManipulator;
11
12
(defined('BASEPATH')) OR exit('No direct script access allowed');
13
14
/**
15
 *
16
 * @property Seoexpert_model $seoexpert_model
17
 * @property Seoexpert_model_products $seoexpert_model_products
18
 * @author <[email protected]>
19
 * @copyright ImageCMS (c) 2014
20
 */
21
class Mod_seo 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
    public function __construct() {
25
26
        parent::__construct();
27
        $this->load->model('seoexpert_model');
28
        $this->load->model('seoexpert_model_products');
29
        $this->load->helper('translit');
30
31
        $lang = new MY_Lang();
32
        $lang->load('mod_seo');
33
    }
34
35
    /**
36
     * Build Meta for Shop Brand
37
     * @param array $arg
38
     * @return false|null
39
     */
40
    public static function _buildBrandMeta($arg) {
41
42
        $local = MY_Controller::getCurrentLocale();
43
        /* @var $model SBrands */
44
        $model = $arg['model'];
45
        $settings = CI::$APP->seoexpert_model->getSettings($local);
46
47
        CI::$APP->core->setLastModified($model->getUpdated());
48
49
        if ($settings['useBrandPattern'] != 1) {
50
            return FALSE;
51
        }
52
53 View Code Duplication
        if ($settings['useBrandPatternForEmptyMeta'] == 1 && trim($model->getMetaTitle()) != '') {
54
            $templateTitle = trim($model->getMetaTitle());
55
        } else {
56
            $templateTitle = $settings['brandTemplate'];
57
        }
58
59 View Code Duplication
        if ($settings['useBrandPatternForEmptyMeta'] == 1 && trim($model->getMetaDescription()) != '') {
60
            $templateDesc = trim($model->getMetaDescription());
61
        } else {
62
            $templateDesc = $settings['brandTemplateDesc'];
63
        }
64
65 View Code Duplication
        if ($settings['useBrandPatternForEmptyMeta'] == 1 && trim($model->getMetaKeywords()) != '') {
66
            $templateKey = trim($model->getMetaKeywords());
67
        } else {
68
            $templateKey = $settings['brandTemplateKey'];
69
        }
70
71
        $metaStorage = new MetaStorage();
72
        $metaStorage->setTitleTemplate($templateTitle);
73
        $metaStorage->setDescriptionTemplate($templateDesc);
74
        $metaStorage->setKeywordsTemplate($templateKey);
75
76
        $metaManipulator = new ShopBrandMetaManipulator($model, $metaStorage);
77
        $metaManipulator->setDescLength($settings['brandTemplateDescCount']);
78
        $metaManipulator->setPageNumber($settings['brandPaginationTemplate']);
79
        $meta = $metaManipulator->render();
80
81
        // Set meta tags
82
        self::setMetaTags($meta['metaTitle'], $meta['metaKeywords'], $meta['metaDescription']);
83
    }
84
85
    /**
86
     * @param string $template
87
     * @param string $templateKey
88
     * @param string $templateDesc
89
     */
90
    private static function setMetaTags($template, $templateKey, $templateDesc) {
91
92
        CI::$APP->core->set_meta_tags($template, $templateKey, $templateDesc);
93
    }
94
95
    /**
96
     * Build Meta for Shop Brands page
97
     * @return false|null
98
     */
99
    public static function _buildBrandsMeta() {
100
101
        $local = MY_Controller::getCurrentLocale();
102
        $settings = CI::$APP->seoexpert_model->getSettings($local);
103
104
        if ($settings['useBrandsListPattern'] != 1) {
105
            return FALSE;
106
        }
107
108
        // Set meta tags
109
        self::setMetaTags($settings['brandsListTemplate'], $settings['brandsListTemplateKey'], $settings['brandsListTemplateDesc']);
110
    }
111
112
    /**
113
     * Build Meta for Shop Category
114
     * @param Category $categoryObj
115
     * @return false|null
116
     */
117
    public static function _buildCategoryMeta($categoryObj) {
118
119
        CI::$APP->load->helper('array_helper');
120
121
        $local = MY_Controller::getCurrentLocale();
122
        /* @var $model SCategory */
123
        $model = $categoryObj->data['category'];
124
125
        CI::$APP->core->setLastModified($model->getUpdated());
126
127
        $settings = ShopCore::$ci->seoexpert_model->getSettings($local);
128
129
        if ($model->getParentId() > 0) {
130
            if ($settings['usesubcategoryPattern'] != 1) {
131
                return FALSE;
132
            }
133 View Code Duplication
            if ($settings['usesubcategoryPatternForEmptyMeta'] == 1 && trim($model->getMetaTitle()) != '') {
134
                $templateTitle = trim($model->getMetaTitle());
135
            } else {
136
                $templateTitle = $settings['subcategoryTemplate'];
137
            }
138
139 View Code Duplication
            if ($settings['usesubcategoryPatternForEmptyMeta'] == 1 && trim($model->getMetaDesc()) != '') {
140
                $templateDesc = trim($model->getMetaDesc());
141
            } else {
142
                $templateDesc = $settings['subcategoryTemplateDesc'];
143
            }
144
145 View Code Duplication
            if ($settings['usesubcategoryPatternForEmptyMeta'] == 1 && trim($model->getMetaKeywords()) != '') {
146
                $templateKey = trim($model->getMetaKeywords());
147
            } else {
148
                $templateKey = $settings['subcategoryTemplateKey'];
149
            }
150
            $brandsCount = $settings['subcategoryTemplateBrandsCount'];
151
            $paginationTemplate = $settings['subcategoryTemplatePaginationTemplate'];
152
            $descCount = $settings['subcategoryTemplateDescCount'];
153
        } else {
154
            if ($settings['useCategoryPattern'] != 1) {
155
                return FALSE;
156
            }
157
158 View Code Duplication
            if ($settings['useCategoryPatternForEmptyMeta'] == 1 && trim($model->getMetaTitle()) != '') {
159
                $templateTitle = trim($model->getMetaTitle());
160
            } else {
161
                $templateTitle = $settings['categoryTemplate'];
162
            }
163
164 View Code Duplication
            if ($settings['useCategoryPatternForEmptyMeta'] == 1 && trim($model->getMetaDesc()) != '') {
165
                $templateDesc = trim($model->getMetaDesc());
166
            } else {
167
                $templateDesc = $settings['categoryTemplateDesc'];
168
            }
169
170 View Code Duplication
            if ($settings['useCategoryPatternForEmptyMeta'] == 1 && trim($model->getMetaKeywords()) != '') {
171
                $templateKey = trim($model->getMetaKeywords());
172
            } else {
173
                $templateKey = $settings['categoryTemplateKey'];
174
            }
175
            $brandsCount = $settings['categoryTemplateBrandsCount'];
176
            $paginationTemplate = $settings['categoryTemplatePaginationTemplate'];
177
            $descCount = $settings['categoryTemplateDescCount'];
178
        }
179
180
        $metaStorage = new MetaStorage();
181
        $metaStorage->setTitleTemplate($templateTitle);
182
        $metaStorage->setDescriptionTemplate($templateDesc);
183
        $metaStorage->setKeywordsTemplate($templateKey);
184
185
        $metaManipulator = new ShopCategoryMetaManipulator($model, $metaStorage);
186
        $metaManipulator->setPageNumber($paginationTemplate);
187
        $metaManipulator->setBrandsCount($brandsCount);
188
        $metaManipulator->setDescLength($descCount);
189
        $meta = $metaManipulator->render();
190
191
        // Set meta tags
192
        self::setMetaTags($meta['metaTitle'], $meta['metaKeywords'], $meta['metaDescription']);
193
    }
194
195
    /**
196
     * Change page category meta tags
197
     *
198
     * @param array $model
199
     * @return false|null
200
     */
201 View Code Duplication
    public static function _buildPageCategoryMeta($model) {
202
203
        $local = MY_Controller::getCurrentLocale();
204
205
        CI::$APP->core->setLastModified($model['updated']);
206
207
        $settings = CI::$APP->seoexpert_model->getSettings($local);
208
209
        if ($settings['usePageCategoryPattern'] != 1) {
210
            return FALSE;
211
        }
212
213
        // Use for Empty meta
214
        if ($settings['usePageCategoryPatternForEmptyMeta'] == 1 && trim($model['title']) != '') {
215
            $templateTitle = trim($model['title']);
216
        } else {
217
            $templateTitle = $settings['pageCategoryTemplateTitle'];
218
        }
219
220
        if ($settings['usePageCategoryPatternForEmptyMeta'] == 1 && trim($model['description']) != '') {
221
            $templateDesc = trim($model['description']);
222
        } else {
223
            $templateDesc = $settings['pageCategoryTemplateDesc'];
224
        }
225
226
        if ($settings['usePageCategoryPatternForEmptyMeta'] == 1 && trim($model['keywords']) != '') {
227
            $templateKey = trim($model['keywords']);
228
        } else {
229
            $templateKey = $settings['pageCategoryTemplateKey'];
230
        }
231
232
        $metaStorage = new MetaStorage();
233
        $metaStorage->setTitleTemplate($templateTitle);
234
        $metaStorage->setDescriptionTemplate($templateDesc);
235
        $metaStorage->setKeywordsTemplate($templateKey);
236
237
        $metaManipulator = new PageCategoryMetaManipulator($model, $metaStorage);
238
        $metaManipulator->setPageNumber($settings['pageCategoryTemplatePaginationTemplate']);
239
        $metaManipulator->setDescLength($settings['pageCategoryTemplateDescCount']);
240
        $meta = $metaManipulator->render();
241
242
        self::setMetaTags($meta['metaTitle'], $meta['metaKeywords'], $meta['metaDescription']);
243
    }
244
245
    /**
246
     * Change simple page meta tags
247
     *
248
     * @param array $model
249
     * @return false|null
250
     */
251 View Code Duplication
    public static function _buildPageMeta($model) {
252
253
        CI::$APP->core->setLastModified($model['updated']);
254
255
        $local = MY_Controller::getCurrentLocale();
256
        $settings = CI::$APP->seoexpert_model->getSettings($local);
257
258
        if ($settings['usePagePattern'] != 1) {
259
            return FALSE;
260
        }
261
262
        // Use for Empty meta
263
        if ($settings['usePagePatternForEmptyMeta'] == 1 && trim($model['meta_title']) != '') {
264
            $templateTitle = trim($model['meta_title']);
265
        } else {
266
            $templateTitle = $settings['pageTemplateTitle'];
267
        }
268
269
        if ($settings['usePagePatternForEmptyMeta'] == 1 && trim($model['description']) != '') {
270
            $templateDesc = trim($model['description']);
271
        } else {
272
            $templateDesc = $settings['pageTemplateDesc'];
273
        }
274
275
        if ($settings['usePagePatternForEmptyMeta'] == 1 && trim($model['keywords']) != '') {
276
            $templateKey = trim($model['keywords']);
277
        } else {
278
            $templateKey = $settings['pageTemplateKey'];
279
        }
280
281
        $metaStorage = new MetaStorage();
282
        $metaStorage->setTitleTemplate($templateTitle);
283
        $metaStorage->setDescriptionTemplate($templateDesc);
284
        $metaStorage->setKeywordsTemplate($templateKey);
285
286
        $metaManipulator = new PageMetaManipulator($model, $metaStorage);
287
        $metaManipulator->setDescLength($settings['pageTemplateDescCount']);
288
        $meta = $metaManipulator->render();
289
290
        // Set meta tags
291
        self::setMetaTags($meta['metaTitle'], $meta['metaKeywords'], $meta['metaDescription']);
292
    }
293
294
    /**
295
     * Build Meta tags for Shop Product
296
     * @param array $arg
297
     * @return false|null
298
     */
299
    public static function _buildProductsMeta($arg) {
300
301
        /* @var $model SProducts */
302
        $model = $arg['model'];
303
        $local = MY_Controller::getCurrentLocale();
304
305
        CI::$APP->core->setLastModified($model->getUpdated());
306
307
        // Get categories ids which has unique settings
308
        $uniqueCategories = CI::$APP->seoexpert_model_products->getCategoriesArray();
309
        // Check is common categories or unique
310
        if (in_array($model->getCategoryId(), $uniqueCategories)) {
311
            $settings = CI::$APP->seoexpert_model_products->getProductCategory($model->getCategoryId(), $local);
312
            $settings = $settings['settings'];
313
        } else {
314
            $settings = CI::$APP->seoexpert_model->getSettings($local);
315
        }
316
317
        // Is active
318
        if ($settings['useProductPattern'] != 1) {
319
            return FALSE;
320
        }
321
322
        // Use for Empty meta
323 View Code Duplication
        if ($settings['useProductPatternForEmptyMeta'] == 1 && trim($model->getMetaTitle()) != '') {
324
            $templateTitle = trim($model->getMetaTitle());
325
        } else {
326
            $templateTitle = $settings['productTemplate'];
327
        }
328
329 View Code Duplication
        if ($settings['useProductPatternForEmptyMeta'] == 1 && trim($model->getMetaDescription()) != '') {
330
            $templateDesc = trim($model->getMetaDescription());
331
        } else {
332
            $templateDesc = $settings['productTemplateDesc'];
333
        }
334
335 View Code Duplication
        if ($settings['useProductPatternForEmptyMeta'] == 1 && trim($model->getMetaKeywords()) != '') {
336
            $templateKey = trim($model->getMetaKeywords());
337
        } else {
338
            $templateKey = $settings['productTemplateKey'];
339
        }
340
341
        $descCount = $settings['productTemplateDescCount'];
342
343
        $metaStorage = new MetaStorage();
344
        $metaStorage->setTitleTemplate($templateTitle);
345
        $metaStorage->setDescriptionTemplate($templateDesc);
346
        $metaStorage->setKeywordsTemplate($templateKey);
347
348
        $metaManipulator = new ShopProductMetaManipulator($model, $metaStorage);
349
        $metaManipulator->setDescLength($descCount);
350
        $meta = $metaManipulator->render();
351
352
        // Set meta tags
353
        self::setMetaTags($meta['metaTitle'], $meta['metaKeywords'], $meta['metaDescription']);
354
    }
355
356
    /**
357
     * @return bool
0 ignored issues
show
Documentation introduced by
Should the return type not be false|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
358
     */
359
    public static function _buildSearchMeta() {
360
361
        $local = MY_Controller::getCurrentLocale();
362
        $settings = CI::$APP->seoexpert_model->getSettings($local);
363
364
        if ($settings['useSearchPattern'] != 1) {
365
            return FALSE;
366
        }
367
368
        $metaStorage = new MetaStorage();
369
        $metaStorage->setTitleTemplate($settings['searchTemplate']);
370
        $metaStorage->setDescriptionTemplate($settings['searchTemplateDesc']);
371
        $metaStorage->setKeywordsTemplate($settings['searchTemplateKey']);
372
373
        $metaManipulator = new MetaManipulator([], $metaStorage);
374
        $meta = $metaManipulator->render();
375
376
        self::setMetaTags($meta['metaTitle'], $meta['metaKeywords'], $meta['metaDescription']);
377
    }
378
379
    public function _deinstall() {
380
381
        CI::$APP->seoexpert_model->deinstall();
382
    }
383
384
    public function _install() {
385
386
        CI::$APP->seoexpert_model->install();
387
    }
388
389
    public function autoload() {
390
391
        // Shop
392
        Events::create()->onSearchPageLoad()->setListener('_buildSearchMeta');
393
        Events::create()->onBrandPageLoad()->setListener('_buildBrandMeta');
394
        Events::create()->onBrandsPageLoad()->setListener('_buildBrandsMeta');
395
        Events::create()->onProductPageLoad()->setListener('_buildProductsMeta');
396
        Events::create()->onCategoryPageLoad()->setListener('_buildCategoryMeta');
397
398
        // Core
399
        Events::create()->onPageLoad()->setListener('_buildPageMeta');
400
        Events::create()->onPageCategoryLoad()->setListener('_buildPageCategoryMeta');
401
    }
402
}