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

CategoryController::index()   F

Complexity

Conditions 14
Paths 483

Size

Total Lines 106
Code Lines 65

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 14
eloc 65
nc 483
nop 1
dl 0
loc 106
rs 3.2528
c 0
b 0
f 0

How to fix   Long Method    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 namespace core\src\Controller;
2
3
use CMSFactory\Events;
4
use core\src\CoreFactory;
5
use core\src\Exception\PageNotFoundException;
6
7
class CategoryController extends Controller
8
{
0 ignored issues
show
introduced by
Opening brace of a class must be on the same line as the definition
Loading history...
9
10
    public function index($id) {
11
        $category = $this->model->getCategory($id);
12
13
        if (!$category) {
14
            throw new PageNotFoundException();
15
        }
16
17
        /** Register event 'Core:_displayCategory' */
18
        Events::create()->registerEvent($category, 'Core:_displayCategory')->runFactory();
19
20
        $paginationPage = $this->ci->input->get('per_page');
21
        if ($paginationPage) {
22
            $offset = $paginationPage;
23
            $segment = $this->ci->uri->total_segments();
24
        } else {
25
            $offset = 0;
26
            $segment = $this->ci->uri->total_segments() + 1;
27
        }
28
29
        $row_count = $category['per_page'];
30
        $pages = $this->model->getCategoryPages($category, $row_count, $offset);
31
32
        if (!$pages && $offset > 0) {
33
            throw new PageNotFoundException();
34
        }
35
36
        $category['total_pages'] = $this->model->countCategoryPages($category);
37
38
        if ($category['total_pages'] > $category['per_page']) {
39
            $this->ci->load->library('Pagination');
40
41
            $get = $this->ci->input->get();
42
            if (isset($get['per_page'])) {
43
                unset($get['per_page']);
44
            }
45
            $get = http_build_query($get);
46
47
            $get = $get ? '?' . $get : '';
48
49
            $categoryUrl = '/' . CoreFactory::getUrlParser()->getFullUrl(true, true, false) . $get;
50
            $paginationConfig['base_url'] = $categoryUrl;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$paginationConfig was never initialized. Although not strictly required by PHP, it is generally a good practice to add $paginationConfig = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
51
            $paginationConfig['first_url'] = $categoryUrl;
52
            $paginationConfig['first_link'] = '1';
53
            $paginationConfig['num_links'] = 3;
54
            $paginationConfig['total_rows'] = $category['total_pages'];
55
            $paginationConfig['per_page'] = $category['per_page'];
56
            $paginationConfig['uri_segment'] = $segment;
57
            $paginationConfig['page_query_string'] = false;
58
59
            include_once './templates/' . config_item('template') . '/paginations.php';
60
61
            $pagination = $this->ci->pagination;
62
63
            $pagination->initialize($paginationConfig);
64
            $this->ci->template->assign('pagination', $pagination->create_links());
65
        }
66
        // End pagination
67
68
        $template = $this->ci->template;
69
        $template->assign('category', $category);
70
71
        $countPages = count($pages);
72
73
        if ($category['tpl'] == '') {
74
            $categoryTemplate = 'category';
75
        } else {
76
            $categoryTemplate = $category['tpl'];
77
        }
78
79
        if ($countPages > 0) {
80
            if (!file_exists($template->template_dir . $categoryTemplate . '.tpl')) {
81
                show_error(lang("Can't locate category template file."));
82
            }
83
            $content = $template->read($categoryTemplate, ['pages' => $pages]);
84
85
        } else {
86
            $content = $template->read($categoryTemplate, ['no_pages' => lang('In the category has no pages.', 'core')]);
87
        }
88
89
        $category['title'] == NULL ? $category['title'] = $category['name'] : TRUE;
90
91
        $category['description'] = $this->ci->core->_makeDescription($category['description']);
92
93
        $category['keywords'] = $this->ci->core->_makeKeywords($category['keywords'], $category['short_desc']);
94
95
        // adding page number for pages with pagination (from second page)
96
        $currentPage = $pagination->cur_page;
97
        if ($currentPage > 1) {
98
            $title = $category['title'] . ' - ' . $currentPage;
99
            $description = $category['description'] . ' - ' . $currentPage;
100
101
            $this->ci->core->set_meta_tags($title, $category['keywords'], $description);
102
        } else {
103
            $this->ci->core->set_meta_tags($category['title'], $category['keywords'], $category['description']);
104
        }
105
106
        $this->ci->core->cat_content = $category;
107
108
        $template->assign('content', $content);
109
110
        Events::create()->registerEvent($category, 'pageCategory:load');
111
        Events::runFactory();
112
113
        $category['main_tpl'] ? $template->display($category['main_tpl']) : $template->show();
114
115
    }
116
117
}