Completed
Push — master ( 8717df...fdbe7f )
by Alexey
05:44
created

Category   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 119
Duplicated Lines 16.81 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 11
lcom 0
cbo 2
dl 20
loc 119
rs 10
c 2
b 1
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A indexes() 0 17 1
B relations() 0 32 1
A resolveTemplate() 10 10 4
B resolveViewer() 10 10 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
2
3
/**
4
 * Item Category
5
 *
6
 * @author Alexey Krupskiy <[email protected]>
7
 * @link http://inji.ru/
8
 * @copyright 2015 Alexey Krupskiy
9
 * @license https://github.com/injitools/cms-Inji/blob/master/LICENSE
10
 */
11
12
namespace Ecommerce;
13
14
class Category extends \Model
15
{
16
    public static $objectName = 'Категория магазина';
17
    public static $treeCategory = 'Ecommerce\Item';
18
    public static $cols = [
19
        //Основные параметры
20
        'parent_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'parent'],
21
        'name' => ['type' => 'text'],
22
        'alias' => ['type' => 'text'],
23
        'viewer' => ['type' => 'select', 'source' => 'method', 'method' => 'viewsCategoryList', 'module' => 'Ecommerce'],
24
        'template' => ['type' => 'select', 'source' => 'method', 'method' => 'templatesCategoryList', 'module' => 'Ecommerce'],
25
        'description' => ['type' => 'html'],
26
        'image_file_id' => ['type' => 'image'],
27
        'options_inherit' => ['type' => 'bool'],
28
        //Системные
29
        'imported' => ['type' => 'bool'],
30
        'weight' => ['type' => 'number'],
31
        'user_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'user'],
32
        'tree_path' => ['type' => 'text'],
33
        'date_create' => ['type' => 'dateTime'],
34
        //Менеджеры
35
        'options' => ['type' => 'dynamicList', 'relation' => 'options'],
36
    ];
37
    public static $labels = [
38
        'name' => 'Название',
39
        'alias' => 'Алиас',
40
        'parent_id' => 'Родитель',
41
        'image_file_id' => 'Изображение',
42
        'description' => 'Описание',
43
        'options_inherit' => 'Наследовать набор свойств',
44
        'options' => 'Свойства товаров',
45
    ];
46
    public static $forms = [
47
        'manager' => [
48
            'map' => [
49
                ['name', 'alias'],
50
                ['parent_id', 'image_file_id'],
51
                ['viewer', 'template'],
52
                //['options_inherit'],
53
                //['options'],
54
                ['description']
55
            ]
56
        ]
57
    ];
58
59
    public static function indexes()
60
    {
61
        return [
62
            'ecommerce_category_category_parent_id' => [
63
                'type' => 'INDEX',
64
                'cols' => [
65
                    'category_parent_id',
66
                ]
67
            ],
68
            'ecommerce_category_category_tree_path' => [
69
                'type' => 'INDEX',
70
                'cols' => [
71
                    'category_tree_path(255)'
72
                ]
73
            ],
74
        ];
75
    }
76
77
    public static function relations()
78
    {
79
        return [
80
            'items' => [
81
                'type' => 'many',
82
                'model' => 'Ecommerce\Item',
83
                'col' => 'category_id',
84
            ],
85
            'parent' => [
86
                'model' => 'Ecommerce\Category',
87
                'col' => 'parent_id'
88
            ],
89
            'options' => [
90
                'type' => 'relModel',
91
                'model' => 'Ecommerce\Item\Option',
92
                'relModel' => 'Ecommerce\Item\Option\Relation',
93
            ],
94
            'image' => [
95
                'model' => 'Files\File',
96
                'col' => 'image_file_id'
97
            ],
98
            'user' => [
99
                'model' => 'Users\User',
100
                'col' => 'user_id'
101
            ],
102
            'catalogs' => [
103
                'type' => 'many',
104
                'model' => 'Ecommerce\Category',
105
                'col' => 'parent_id',
106
            ]
107
        ];
108
    }
109
110 View Code Duplication
    public function resolveTemplate()
111
    {
112
        if ($this->template !== 'inherit') {
113
            return $this->template;
114
        } elseif ($this->template == 'inherit' && $this->category) {
115
            return $this->category->resolveTemplate(true);
116
        } else {
117
            return 'current';
118
        }
119
    }
120
121 View Code Duplication
    public function resolveViewer()
122
    {
123
        if ($this->viewer !== 'inherit') {
124
            return $this->viewer;
125
        } elseif ($this->viewer == 'inherit' && $this->category) {
126
            return $this->category->resolveViewer(true);
127
        } else {
128
            return (!empty(\App::$cur->ecommerce->config['defaultCategoryView']) ? \App::$cur->ecommerce->config['defaultCategoryView'] : 'itemList');
129
        }
130
    }
131
132
}
133