Test Failed
Push — master ( 503dc7...d178d4 )
by Alexey
04:55
created

Material::getHref()   B

Complexity

Conditions 11
Paths 32

Size

Total Lines 16
Code Lines 11

Duplication

Lines 8
Ratio 50 %

Importance

Changes 0
Metric Value
cc 11
eloc 11
nc 32
nop 0
dl 8
loc 16
rs 7.1162
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * Material
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 Materials;
13
14
class Material extends \Model {
15
16
    public static $objectName = 'Материал';
17
    public static $categoryModel = 'Materials\Category';
18
    public static $labels = [
19
        'name' => 'Заголовок',
20
        'category_id' => 'Раздел',
21
        'preview' => 'Краткое превью',
22
        'text' => 'Текст страницы',
23
        'alias' => 'Алиас страницы',
24
        'template' => 'Шаблон сайта',
25
        'viewer' => 'Тип страницы',
26
        'image_file_id' => 'Фото материала',
27
        'description' => 'Описание для поисковиков',
28
        'keywords' => 'Ключевые слова',
29
        'user_id' => 'Создатель',
30
        'date_create' => 'Дата создания',
31
        'date_publish' => 'Дата публикации',
32
        'tag_list' => 'Теги',
33
        'default' => 'По умолчанию'
34
    ];
35
    public static $dataManagers = [
36
        'manager' => [
37
            'cols' => [
38
                'name',
39
                'alias',
40
                'default',
41
                'category_id',
42
                'tag_list',
43
                'date_publish',
44
                'date_create'
45
            ],
46
            'sortable' => [
47
                'name',
48
                'alias',
49
                'category_id',
50
                'date_create'
51
            ],
52
            'filters' => [
53
                'name',
54
                'preview',
55
                'text',
56
                'default',
57
                'alias',
58
                'template',
59
                'viewer',
60
                'description',
61
                'keywords',
62
                'user_id',
63
                'date_create'
64
            ],
65
            'categorys' => [
66
                'model' => 'Materials\Category',
67
            ],
68
            'sortMode' => true
69
        ]
70
    ];
71
    public static $cols = [
72
        'name' => ['type' => 'text'],
73
        'alias' => ['type' => 'text'],
74
        'preview' => ['type' => 'html'],
75
        'text' => ['type' => 'html'],
76
        'keywords' => ['type' => 'text'],
77
        'description' => ['type' => 'text'],
78
        'category_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'category'],
79
        'user_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'user'],
80
        'template' => ['type' => 'select', 'source' => 'method', 'method' => 'templatesList', 'module' => 'Materials'],
81
        'viewer' => ['type' => 'select', 'source' => 'method', 'method' => 'viewsList', 'module' => 'Materials'],
82
        'default' => ['type' => 'bool'],
83
        'hidden' => ['type' => 'bool'],
84
        'image_file_id' => ['type' => 'image'],
85
        'link' => ['type' => 'dataManager', 'relation' => 'links'],
86
        'tree_path' => ['type' => 'text'],
87
        'weight' => ['type' => 'number'],
88
        'tag_list' => ['type' => 'text'],
89
        'date_publish' => ['type' => 'dateTime', 'null' => true, 'emptyValue' => null],
90
        'date_create' => ['type' => 'dateTime'],
91
    ];
92
    public static $forms = [
93
        'manager' => [
94
            'options' => [
95
                'access' => [
96
                    'groups' => [
97
                        3
98
                    ]
99
                ]
100
            ],
101
            'map' => [
102
                ['name', 'category_id'],
103
                ['alias', 'image_file_id', 'date_publish'],
104
                ['template', 'viewer', 'default'],
105
                ['keywords', 'description'],
106
                ['tag_list'],
107
                ['preview'],
108
                ['text'],
109
                ['link'],
110
            ]
111
        ]
112
    ];
113
114 View Code Duplication
    public static function relations() {
115
        return [
116
            'category' => [
117
                'model' => '\Materials\Category',
118
                'col' => 'category_id'
119
            ],
120
            'image' => [
121
                'model' => '\Files\File',
122
                'col' => 'image_file_id'
123
            ],
124
            'user' => [
125
                'model' => '\Users\User',
126
                'col' => 'user_id'
127
            ],
128
            'links' => [
129
                'type' => 'many',
130
                'model' => '\Materials\Material\Link',
131
                'col' => 'material_id'
132
            ]
133
        ];
134
    }
135
136
    public function getHref() {
137
        $href = !empty(\App::$primary->config['defaultModule']) && \App::$primary->config['defaultModule'] == 'Materials' ? '' : '/materials';
138
        $treePath = array_filter(explode('/', $this->tree_path));
139 View Code Duplication
        if ($treePath) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
140
            $categorys = Category::getList(['where' => ['id', implode(',', $treePath), 'IN']]);
141
            foreach ($categorys as $category) {
142
                if ($category->alias) {
143
                    $href .= "/{$category->alias}";
144
                }
145
            }
146
        }
147
        if ((!$href || $href == '/materials') && !$this->alias) {
148
            return ($href ? $href : '/materials') . '/view/' . $this->pk();
149
        }
150
        return $href . "/" . ($this->alias ? $this->alias : $this->pk());
151
    }
152
153 View Code Duplication
    public function resolveTemplate() {
154
        if ($this->template !== 'inherit') {
155
            return $this->template;
156
        } elseif ($this->template == 'inherit' && $this->category) {
157
            return $this->category->resolveTemplate(true);
158
        } else {
159
            return 'current';
160
        }
161
    }
162
163
    public function resolveViewer() {
164
        if ($this->viewer !== 'inherit') {
165
            return $this->viewer;
166
        } elseif ($this->viewer == 'inherit' && $this->category) {
167
            return $this->category->resolveViewer(true);
168
        } else {
169
            return 'default';
170
        }
171
    }
172
173
    public function beforeSave() {
174
        if ($this->default) {
175
            Material::update(['default', 0]);
176
        }
177
    }
178
}