ControllerProductSpecial::index()   F
last analyzed

Complexity

Conditions 33
Paths > 20000

Size

Total Lines 274
Code Lines 173

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 33
eloc 173
c 0
b 0
f 0
nc 77856768
nop 0
dl 0
loc 274
rs 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
2
3
/* 	Divine CMS - Open source CMS for widespread use.
4
    Copyright (c) 2019 Mykola Burakov ([email protected])
5
6
    See SOURCE.txt for other and additional information.
7
8
    This file is part of Divine CMS.
9
10
    This program is free software: you can redistribute it and/or modify
11
    it under the terms of the GNU General Public License as published by
12
    the Free Software Foundation, either version 3 of the License, or
13
    (at your option) any later version.
14
15
    This program is distributed in the hope that it will be useful,
16
    but WITHOUT ANY WARRANTY; without even the implied warranty of
17
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
    GNU General Public License for more details.
19
20
    You should have received a copy of the GNU General Public License
21
    along with this program. If not, see <http://www.gnu.org/licenses/>. */
22
23
class ControllerProductSpecial extends \Divine\Engine\Core\Controller
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
24
{
25
    public function index()
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 0 found
Loading history...
26
    {
27
        $this->load->language('product/special');
28
29
        $this->load->model('catalog/product');
30
31
        
32
33
        if (isset($this->request->get['sort'])) {
34
            $sort = $this->request->get['sort'];
35
            $this->document->setRobots('noindex,follow');
36
        } else {
37
            $sort = 'p.sort_order';
38
        }
39
40
        if (isset($this->request->get['order'])) {
41
            $order = $this->request->get['order'];
42
        } else {
43
            $order = 'ASC';
44
        }
45
46
        if (isset($this->request->get['page'])) {
47
            $page = $this->request->get['page'];
48
            $this->document->setRobots('noindex,follow');
49
        } else {
50
            $page = 1;
51
        }
52
53
        if (isset($this->request->get['limit'])) {
54
            $limit = ((int)$this->request->get['limit'] > 100 ? 100 : (int)$this->request->get['limit']);
0 ignored issues
show
Unused Code introduced by
The assignment to $limit is dead and can be removed.
Loading history...
55
            $this->document->setRobots('noindex,follow');
56
        } else {
57
            $limit = $this->config->get('config_limit_store');
58
        }
59
60
        if ($this->config->get('seomanager_meta_title_special')) {
61
            $this->document->setTitle($this->config->get('seomanager_meta_title_special'));
62
        } else {
63
            $this->document->setTitle($this->language->get('heading_title'));
64
        }
65
66
        if ($this->config->get('seomanager_html_h1_special')) {
67
            $data['heading_title'] = $this->config->get('seomanager_html_h1_special');
0 ignored issues
show
Comprehensibility Best Practice introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.
Loading history...
68
        } else {
69
            $data['heading_title'] = $this->language->get('heading_title');
70
        }
71
72
        if ($this->config->get('seomanager_meta_description_special')) {
73
            $this->document->setDescription($this->config->get('seomanager_meta_description_special'));
74
        }
75
76
        $data['description'] = html_entity_decode(($this->config->get('seomanager_description_special')), ENT_QUOTES, 'UTF-8');
77
78
        $data['breadcrumbs'] = array();
79
80
        $data['breadcrumbs'][] = array(
81
            'text' => $this->language->get('text_home'),
82
            'href' => $this->url->link('common/home')
83
        );
84
85
        $url = '';
86
87
        if (isset($this->request->get['sort'])) {
88
            $url .= '&sort=' . $this->request->get['sort'];
89
        }
90
91
        if (isset($this->request->get['order'])) {
92
            $url .= '&order=' . $this->request->get['order'];
93
        }
94
95
        if (isset($this->request->get['page'])) {
96
            $url .= '&page=' . $this->request->get['page'];
97
        }
98
99
        if (isset($this->request->get['limit'])) {
100
            $url .= '&limit=' . $this->request->get['limit'];
101
        }
102
103
        $data['breadcrumbs'][] = array(
104
            'text' => $this->language->get('heading_title'),
105
            'href' => $this->url->link('product/special', $url)
106
        );
107
108
        $data['text_empty'] = $this->language->get('text_empty');
109
        $data['text_quantity'] = $this->language->get('text_quantity');
110
        $data['text_manufacturer'] = $this->language->get('text_manufacturer');
111
        $data['text_model'] = $this->language->get('text_model');
112
        $data['text_price'] = $this->language->get('text_price');
113
        $data['text_points'] = $this->language->get('text_points');
114
        $data['text_sort'] = $this->language->get('text_sort');
115
        $data['text_limit'] = $this->language->get('text_limit');
116
        $data['text_go_back'] = $this->language->get('text_go_back');
117
118
        $data['button_buy_it'] = $this->language->get('button_buy_it');
119
        $data['button_list'] = $this->language->get('button_list');
120
        $data['button_grid'] = $this->language->get('button_grid');
121
        $data['button_continue'] = $this->language->get('button_continue');
122
123
        $data['products'] = array();
124
125
        $filter_data = array(
126
            'sort'  => $sort,
127
            'order' => $order,
128
            'start' => ($page - 1) * $limit,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $limit does not seem to be defined for all execution paths leading up to this point.
Loading history...
129
            'limit' => $limit
130
        );
131
132
        $product_total = $this->model_catalog_product->getTotalProductSpecials();
133
134
        $results = $this->model_catalog_product->getProductSpecials($filter_data);
135
136
        foreach ($results as $result) {
137
            if ($result['image']) {
138
                $image = '/public_html/assets/images/' . $result['image'];
139
            } else {
140
                $image = '/public_html/assets/images/no_image.png';
141
            }
142
143
            if ($this->customer->isLogged() || !$this->config->get('config_customer_price')) {
144
                $price = $this->currency->format($result['price'], $this->session->data['currency']);
145
            } else {
146
                $price = false;
147
            }
148
149
            if ((float)$result['special']) {
150
                $special = $this->currency->format($result['special'], $this->session->data['currency']);
151
            } else {
152
                $special = false;
153
            }
154
155
            if ($result['description_mini']) {
156
                $description = \voku\helper\UTF8::substr(strip_tags(html_entity_decode($result['description_mini'], ENT_QUOTES, 'UTF-8')), 0);
157
            } else {
158
                $description = \voku\helper\UTF8::substr(strip_tags(html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8')), 0, $this->config->get($this->config->get('config_theme') . '_product_description_length')) . '..';
0 ignored issues
show
Bug introduced by
Are you sure voku\helper\UTF8::substr...t_description_length')) of type false|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

158
                $description = /** @scrutinizer ignore-type */ \voku\helper\UTF8::substr(strip_tags(html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8')), 0, $this->config->get($this->config->get('config_theme') . '_product_description_length')) . '..';
Loading history...
159
            }
160
161
            $stickers = $this->getStickers($result['product_id']);
162
163
            $data['products'][] = array(
164
                'product_id'  => $result['product_id'],
165
                'thumb'       => $image,
166
                'name'        => $result['name'],
167
                'description' => $description,
168
                'price'       => $price,
169
                'special'     => $special,
170
                'sticker'     => $stickers,
171
                'minimum'     => $result['minimum'] > 0 ? $result['minimum'] : 1,
172
                'href'        => $this->url->link('product/product', 'product_id=' . $result['product_id'] . $url)
173
            );
174
        }
175
176
        $url = '';
177
178
        if (isset($this->request->get['limit'])) {
179
            $url .= '&limit=' . $this->request->get['limit'];
180
        }
181
182
        $data['sorts'] = array();
183
184
        $data['sorts'][] = array(
185
            'text'  => $this->language->get('text_default'),
186
            'value' => 'p.sort_order-ASC',
187
            'href'  => $this->url->link('product/special', 'sort=p.sort_order&order=ASC' . $url)
188
        );
189
190
        $data['sorts'][] = array(
191
            'text'  => $this->language->get('text_name_asc'),
192
            'value' => 'pd.name-ASC',
193
            'href'  => $this->url->link('product/special', 'sort=pd.name&order=ASC' . $url)
194
        );
195
196
        $data['sorts'][] = array(
197
            'text'  => $this->language->get('text_name_desc'),
198
            'value' => 'pd.name-DESC',
199
            'href'  => $this->url->link('product/special', 'sort=pd.name&order=DESC' . $url)
200
        );
201
202
        $data['sorts'][] = array(
203
            'text'  => $this->language->get('text_price_asc'),
204
            'value' => 'ps.price-ASC',
205
            'href'  => $this->url->link('product/special', 'sort=ps.price&order=ASC' . $url)
206
        );
207
208
        $data['sorts'][] = array(
209
            'text'  => $this->language->get('text_price_desc'),
210
            'value' => 'ps.price-DESC',
211
            'href'  => $this->url->link('product/special', 'sort=ps.price&order=DESC' . $url)
212
        );
213
214
        $data['sorts'][] = array(
215
            'text'  => $this->language->get('text_model_asc'),
216
            'value' => 'p.model-ASC',
217
            'href'  => $this->url->link('product/special', 'sort=p.model&order=ASC' . $url)
218
        );
219
220
        $data['sorts'][] = array(
221
            'text'  => $this->language->get('text_model_desc'),
222
            'value' => 'p.model-DESC',
223
            'href'  => $this->url->link('product/special', 'sort=p.model&order=DESC' . $url)
224
        );
225
226
        $url = '';
227
228
        if (isset($this->request->get['sort'])) {
229
            $url .= '&sort=' . $this->request->get['sort'];
230
        }
231
232
        if (isset($this->request->get['order'])) {
233
            $url .= '&order=' . $this->request->get['order'];
234
        }
235
236
        $data['limits'] = array();
237
238
        $limits = array_unique(array($this->config->get('config_limit_store'), 25, 50, 75, 100));
239
240
        sort($limits);
241
242
        foreach ($limits as $value) {
243
            $data['limits'][] = array(
244
                'text'  => $value,
245
                'value' => $value,
246
                'href'  => $this->url->link('product/special', $url . '&limit=' . $value)
247
            );
248
        }
249
250
        $url = '';
251
252
        if (isset($this->request->get['sort'])) {
253
            $url .= '&sort=' . $this->request->get['sort'];
254
        }
255
256
        if (isset($this->request->get['order'])) {
257
            $url .= '&order=' . $this->request->get['order'];
258
        }
259
260
        if (isset($this->request->get['limit'])) {
261
            $url .= '&limit=' . $this->request->get['limit'];
262
        }
263
264
        $pagination = new \Divine\Engine\Library\Pagination();
265
        $pagination->total = $product_total;
266
        $pagination->page = $page;
267
        $pagination->limit = $limit;
268
        $pagination->url = $this->url->link('product/special', $url . '&page={page}');
269
270
        $data['pagination'] = $pagination->render();
271
272
        $data['results'] = sprintf($this->language->get('text_pagination'), ($product_total) ? (($page - 1) * $limit) + 1 : 0, ((($page - 1) * $limit) > ($product_total - $limit)) ? $product_total : ((($page - 1) * $limit) + $limit), $product_total, ceil($product_total / $limit));
273
274
        // http://googlewebmastercentral.blogspot.com/2011/09/pagination-with-relnext-and-relprev.html
275
        if ($page == 1) {
276
            $this->document->addLink($this->url->link('product/special', '', true), 'canonical');
277
        } elseif ($page == 2) {
278
            $this->document->addLink($this->url->link('product/special', '', true), 'prev');
279
        } else {
280
            $this->document->addLink($this->url->link('product/special', 'page=' . ($page - 1), true), 'prev');
281
        }
282
283
        if ($limit && ceil($product_total / $limit) > $page) {
284
            $this->document->addLink($this->url->link('product/special', 'page=' . ($page + 1), true), 'next');
285
        }
286
287
        $data['sort'] = $sort;
288
        $data['order'] = $order;
289
        $data['limit'] = $limit;
290
291
        $data['continue'] = $this->url->link('common/home');
292
293
        $data['content_top'] = $this->load->controller('common/content_top');
294
        $data['content_bottom'] = $this->load->controller('common/content_bottom');
295
        $data['footer'] = $this->load->controller('common/footer');
296
        $data['header'] = $this->load->controller('common/header');
297
298
        $this->response->setOutput($this->load->view('product/special', $data));
299
    }
300
301
    private function getStickers($product_id)
302
    {
303
        $stickers = $this->model_catalog_product->getProductStickerbyProductId($product_id);
304
305
        if (!$stickers) {
306
            return;
307
        }
308
309
        $data['stickers'] = array();
0 ignored issues
show
Comprehensibility Best Practice introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.
Loading history...
310
311
        foreach ($stickers as $sticker) {
312
            $data['stickers'][] = array(
313
                'position' => $sticker['position'],
314
                'image' => '/public_html/assets/images/' . $sticker['image']
315
            );
316
        }
317
318
        return $this->load->view('product/stickers', $data);
319
    }
320
}
321