Passed
Push — v5 ( e348bf...8a8f01 )
by Alexey
06:35
created

MaterialsController::viewAction()   D

Complexity

Conditions 10
Paths 168

Size

Total Lines 42
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 32
nc 168
nop 0
dl 0
loc 42
rs 4.606
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
 * Materials app controller
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
class MaterialsController extends Controller {
0 ignored issues
show
Bug introduced by
The type Controller was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
13
    public function indexAction() {
14
        $args = func_get_args();
15
        $category = null;
16
        $material = null;
17
        $path = trim(implode('/', $args));
18
        if (is_numeric($path)) {
19
            $material = Materials\Material::get([['id', (int) $path], ['date_publish', null, 'IS NOT']]);
20
        }
21
        if (!$material && $args) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $args of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
22
            foreach ($args as $key => $alias) {
23
                $nextCategory = Materials\Category::get([['parent_id', $category ? $category->id : 0], ['alias', $alias]]);
24
                if (!$nextCategory) {
25
                    break;
26
                }
27
                $category = $nextCategory;
28
            }
29
        }
30
        if (!$material && $path) {
31
            if ($category) {
32
                $where = [
33
                    ['tree_path', $category->tree_path . $category->id . '/%', 'LIKE'],
34
                    ['alias', $args[count($args) - 1]],
35
                    ['date_publish', null, 'IS NOT']
36
                ];
37
            } else {
38
                $where = [['alias', $path], ['date_publish', null, 'IS NOT']];
39
            }
40
            $material = Materials\Material::get($where);
41
            if (!$material) {
42
                if ($category) {
43
                    $where = [
44
                        ['category_id', $category->id],
45
                        ['id', (int) $args[count($args) - 1]],
46
                        ['date_publish', null, 'IS NOT']
47
                    ];
48
                } else {
49
                    $where = [['alias', $path], ['date_publish', null, 'IS NOT']];
50
                }
51
                $material = Materials\Material::get($where);
52
            }
53
            if (!$material) {
54
                $category = Materials\Category::get($path, 'alias');
55
                if ($category) {
56
                    $this->categoryAction($category->id);
57
                }
58
            }
59
        } elseif (!$material) {
60
            $material = Materials\Material::get(1, 'default');
61
        }
62
        if ($material) {
63
            $this->viewAction($material->id);
64
        } elseif (!$category && !$material) {
65
            Tools::header('404');
0 ignored issues
show
Bug introduced by
The type Tools was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
66
            $this->view->page([
67
                'content' => '404',
68
                'data' => ['text' => 'Такой страницы не найдено']
69
            ]);
70
        }
71
    }
72
73
    public function categoryAction() {
74
        $args = func_get_args();
75
        $path = trim(implode('/', $args));
76
        $category = null;
77
        if (is_numeric($path)) {
78
            $category = Materials\Category::get((int) $path);
79
        }
80
        if (!$category) {
81
            foreach ($args as $alias) {
82
                $category = Materials\Category::get([['parent_id', $category ? $category->id : 0], ['alias', $alias]]);
83
                if (!$category) {
84
                    break;
85
                }
86
            }
87
        }
88
        if (!$category) {
89
            $category = Materials\Category::get($path, 'alias');
90
        }
91
        if (!$category) {
92
            Tools::header('404');
93
            $this->view->page([
94
                'content' => '404',
95
                'data' => ['text' => 'Такой страницы не найдено']
96
            ]);
97
        } else {
98
            $this->view->setTitle($category->name);
99
100
            $pages = new Ui\Pages($_GET, ['count' => Materials\Material::getCount(['where' => [['tree_path', $category->tree_path . $category->id . '/%', 'LIKE'], ['date_publish', null, 'IS NOT']]]), 'limit' => 10]);
101
            $materials = Materials\Material::getList(['where' => [['tree_path', $category->tree_path . $category->id . '/%', 'LIKE'], ['date_publish', null, 'IS NOT']], 'order' => ['date_create', 'desc'], 'start' => $pages->params['start'], 'limit' => $pages->params['limit']]);
102
103
            $this->view->page(['page' => $category->resolveTemplate(), 'content' => $category->resolveViewer(), 'data' => compact('materials', 'pages', 'category')]);
104
        }
105
    }
106
107
    public function viewAction() {
108
        $args = func_get_args();
109
        $alias = trim(implode('/', $args));
110
        $material = false;
111
        if ($alias) {
112
            if (is_numeric($alias)) {
113
                $material = Materials\Material::get([['id', (int) $alias], ['date_publish', null, 'IS NOT']]);
114
            }
115
            if (!$material) {
116
                $material = Materials\Material::get([['alias', $alias], ['date_publish', null, 'IS NOT']]);
117
                if (!$material) {
118
                    Tools::header('404');
119
                    $this->view->page([
120
                        'content' => '404',
121
                        'data' => ['text' => 'Такой страницы не найдено']
122
                    ]);
123
                }
124
            }
125
        }
126
        if ($material->keywords) {
127
            $this->view->addMetaTag(['name' => 'keywords', 'content' => $material->keywords]);
128
        }
129
        if ($material->description) {
130
            $this->view->addMetaTag(['name' => 'description', 'content' => $material->description]);
131
        }
132
        $this->view->addMetaTag(['property' => 'og:title', 'content' => $material->name]);
133
        $this->view->addMetaTag(['property' => 'og:url', 'content' => 'http://' . idn_to_utf8(INJI_DOMAIN_NAME) . '/' . $material->alias]);
134
        if ($material->description) {
135
            $this->view->addMetaTag(['property' => 'og:description', 'content' => 'http://' . idn_to_utf8(INJI_DOMAIN_NAME) . '/' . $material->description]);
136
        }
137
        if ($material->image) {
138
            $this->view->addMetaTag(['property' => 'og:image', 'content' => 'http://' . idn_to_utf8(INJI_DOMAIN_NAME) . $material->image->path]);
139
        } elseif ($logo = Files\File::get('site_logo', 'code')) {
140
            $this->view->addMetaTag(['property' => 'og:image', 'content' => 'http://' . idn_to_utf8(INJI_DOMAIN_NAME) . $logo->path]);
141
        }
142
        $this->view->setTitle($material->name);
143
        $bread = [];
144
        $bread[] = ['text' => $material->name, 'href' => '/' . $material->alias];
145
        $this->view->page([
146
            'page' => $material->resolveTemplate(),
147
            'content' => $material->resolveViewer(),
148
            'data' => compact('material', 'bread'),
149
        ]);
150
    }
151
152
    function detailSearchAction() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
153
        $this->view->setTitle('Поиск материалов');
154
        $result = [];
155
        if (!empty($_GET['search']) && is_string($_GET['search'])) {
156
            $result = $this->module->search($_GET['search']);
157
        }
158
        $this->view->page(['data' => ['result' => $result]]);
159
    }
160
}