Test Failed
Push — master ( 7f4446...d7feff )
by Alexey
11:49 queued 07:23
created

OptionsParser::order()   C

Complexity

Conditions 12
Paths 18

Size

Total Lines 40
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 32
nc 18
nop 2
dl 0
loc 40
rs 5.1612
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
 * Helper for module methods
5
 *
6
 * @author Alexey Krupskiy <[email protected]>
7
 * @link http://inji.ru/
8
 * @copyright 2016 Alexey Krupskiy
9
 * @license https://github.com/injitools/cms-Inji/blob/master/LICENSE
10
 */
11
12
namespace Ecommerce;
13
14
class OptionsParser extends \Object {
15
16
    public static function parse($options = []) {
17
        $selectOptions = self::getDefault($options);
18
19
        //parse order options
20
        self::order($options, $selectOptions);
21
22
        //joined offers
23
        $selectOptions['join'][] = [Item\Offer::table(), Item::index() . ' = ' . Item\Offer::colPrefix() . Item::index(), 'inner'];
24
25
        //joined prices, check price configs
26
        $selectOptions['join'][] = [Item\Offer\Price::table(),
27
            Item\Offer::index() . ' = ' . Item\Offer\Price::colPrefix() . Item\Offer::index() .
28
            (empty(\App::$cur->Ecommerce->config['show_zero_price']) ? ' and ' . Item\Offer\Price::colPrefix() . 'price>0' : ''),
29
            empty(\App::$cur->Ecommerce->config['show_without_price']) ? 'inner' : 'left'];
30
31
        //join item types
32
        $selectOptions['join'][] = [
33
            Item\Offer\Price\Type::table(), Item\Offer\Price::colPrefix() . Item\Offer\Price\Type::index() . ' = ' . Item\Offer\Price\Type::index()
34
        ];
35
36
        //add filter for item type user roles by current user role
37
        $selectOptions['where'][] = [
38
            [Item\Offer\Price\Type::index(), null, 'is'],
39
            [
40
                [Item\Offer\Price\Type::colPrefix() . 'roles', '', '=', 'OR'],
41
                [Item\Offer\Price\Type::colPrefix() . 'roles', '%|' . \Users\User::$cur->role_id . '|%', 'LIKE', 'OR'],
42
            ],
43
        ];
44
45
        //add custom preset filters from config on where
46
        self::presetFilters($selectOptions);
47
48
        //parse filters
49
        self::filters($options, $selectOptions);
50
51
        //categories paths
52
        self::categories($options, $selectOptions);
53
54
        //search
55
        self::search($options, $selectOptions);
56
57
        self::warehouse($selectOptions);
58
59
        $selectOptions['group'] = Item::index();
60
        if(isset($options['array'])){
61
            $selectOptions['array'] = $options['array'];
62
        }
63
64
        return $selectOptions;
65
    }
66
67
    public static function getDefault(&$options) {
68
        $selectOptions = [
69
            'where' => !empty($options['where']) ? $options['where'] : [],
70
            'distinct' => false,
71
            'join' => [],
72
            'order' => [],
73
            'start' => isset($options['start']) ? (int) $options['start'] : 0,
74
            'key' => isset($options['key']) ? $options['key'] : null,
75
            'limit' => !empty($options['count']) ? (int) $options['count'] : 0,
76
        ];
77
78
        //only not deleted items
79
        $selectOptions['where'][] = ['deleted', 0];
80
81
        //check config of view items without images
82
        if (empty(\App::$cur->Ecommerce->config['view_empty_image'])) {
83
            $selectOptions['where'][] = ['image_file_id', 0, '!='];
84
        }
85
86
        return $selectOptions;
87
    }
88
89
    public static function order(&$options, &$selectOptions) {
90
        if (empty($options['sort']) || !is_array($options['sort'])) {
91
            return;
92
        }
93
        foreach ($options['sort'] as $col => $direction) {
94
            $direction = strtolower($direction) == 'desc' ? 'desc' : 'asc';
95
            switch ($col) {
96
                case 'price':
97
                    $selectOptions['order'][] = [Item\Offer\Price::colPrefix() . 'price', $direction];
98
                    break;
99
                case 'new':
100
                    $selectOptions['order'][] = ['date_create', $direction];
101
                    break;
102
                case 'isset':
103
                    $warehouseIds = self::getWarehouses();
104
                    $selectOptions['order'][] = [
105
                        '(
106
          (SELECT COALESCE(sum(`' . Item\Offer\Warehouse::colPrefix() . 'count`),0) 
107
            FROM ' . \App::$cur->db->table_prefix . Item\Offer\Warehouse::table() . ' iciw 
108
            WHERE iciw.' . Item\Offer\Warehouse::colPrefix() . Item\Offer::index() . ' = ' . Item\Offer::index() . '
109
                ' . ($warehouseIds ? ' AND iciw.' . Item\Offer\Warehouse::colPrefix() . Warehouse::index() . ' IN(' . implode(',', $warehouseIds) . ')' : '') . '
110
            )
111
          -
112
          (SELECT COALESCE(sum(' . Warehouse\Block::colPrefix() . 'count) ,0)
113
            FROM ' . \App::$cur->db->table_prefix . Warehouse\Block::table() . ' iewb
114
            inner JOIN ' . \App::$cur->db->table_prefix . Cart::table() . ' icc ON icc.' . Cart::index() . ' = iewb.' . Warehouse\Block::colPrefix() . Cart::index() . ' AND (
115
                (`' . Cart::colPrefix() . 'warehouse_block` = 1 and `' . Cart::colPrefix() . 'cart_status_id` in(2,3,6)) ||
116
                (`' . Cart::colPrefix() . Cart\Status::index() . '` in(0,1) and `' . Cart::colPrefix() . 'date_last_activ` >=subdate(now(),INTERVAL 30 MINUTE))
117
            )
118
            WHERE iewb.' . Warehouse\Block::colPrefix() . Item\Offer::index() . ' = ' . Item\Offer::index() . ')
119
          )', $direction
120
                    ];
121
                    break;
122
                case 'name':
123
                case 'sales':
124
                case 'weight':
125
                    $selectOptions['order'][] = [$col, $direction];
126
            }
127
        }
128
    }
129
130
    public static function presetFilters(&$selectOptions) {
131
        if (empty(\App::$cur->Ecommerce->config['view_filter'])) {
132
            return;
133
        }
134
        if (!empty(\App::$cur->Ecommerce->config['view_filter']['options'])) {
135
            foreach (\App::$cur->Ecommerce->config['view_filter']['options'] as $optionId => $optionValue) {
136
                $selectOptions['join'][] = [Item\Param::table(), Item::index() . ' = ' . 'option' . $optionId . '.' . Item\Param::colPrefix() . Item::index() . ' AND ' .
137
                    'option' . $optionId . '.' . Item\Param::colPrefix() . Item\Option::index() . ' = "' . (int) $optionId . '" AND ' .
138
                    'option' . $optionId . '.' . Item\Param::colPrefix() . 'value = "' . (int) $optionValue . '"',
139
                    'inner', 'option' . $optionId];
140
            }
141
        }
142
    }
143
144
    public static function filters(&$options, &$selectOptions) {
145
        if (empty($options['filters'])) {
146
            return;
147
        }
148
        foreach ($options['filters'] as $col => $filter) {
149
            switch ($col) {
150
                case 'best':
151
                    if ($filter) {
152
                        $selectOptions['where'][] = ['best', 1];
153
                    }
154
                    break;
155
                case 'badge':
156
                    $selectOptions['where'][] = ['item_badge_id', (int) $filter];
157
                    break;
158
                case 'price':
159
                    $colName = Item\Offer\Price::colPrefix() . 'price';
160 View Code Duplication
                    if (!empty($filter['min'])) {
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...
161
                        $selectOptions['where'][] = [$colName, (float) $filter['min'], '>='];
162
                    }
163 View Code Duplication
                    if (!empty($filter['max'])) {
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...
164
                        $selectOptions['where'][] = [$colName, (float) $filter['max'], '<='];
165
                    }
166
                    break;
167
                case 'options':
168
                case 'offerOptions':
169
                    if ($col == 'offerOptions') {
170
                        $paramPrefix = Item\Offer\Param::colPrefix();
171
                        $itemIndex = Item\Offer::index();
172
                        $optionIndex = Item\Offer\Option::index();
173
                        $table = Item\Offer\Param::table();
174
                    } else {
175
                        $paramPrefix = Item\Param::colPrefix();
176
                        $itemIndex = Item::index();
177
                        $optionIndex = Item\Option::index();
178
                        $table = Item\Param::table();
179
                    }
180
                    if ($filter) {
181
                        foreach ($filter as $optionId => $optionValue) {
182
                            $optionId = (int) $optionId;
183
                            if (is_array($optionValue)) {
184
                                $optionValueArr = [];
185
                                foreach ($optionValue as $val) {
186
                                    $optionValueArr[] = \App::$cur->db->connection->pdo->quote($val);
187
                                }
188
                                $qstr = 'IN (' . implode(',', $optionValueArr) . ')';
189
                            } else {
190
                                $qstr = '= ' . \App::$cur->db->connection->pdo->quote($optionValue);
191
                            }
192
                            $selectOptions['join'][] = [$table, $itemIndex . ' = ' . 'option' . $optionId . '.' . $paramPrefix . $itemIndex . ' AND ' .
193
                                'option' . $optionId . '.' . $paramPrefix . $optionIndex . ' = "' . (int) $optionId . '" AND ' .
194
                                'option' . $optionId . '.' . $paramPrefix . 'value ' . $qstr . '',
195
                                'inner', 'option' . $optionId];
196
                        }
197
                    }
198
            }
199
        }
200
    }
201
202
    public static function categories(&$options, &$selectOptions) {
203
        if (empty($options['parent'])) {
204
            return;
205
        }
206
        if (is_array($options['parent']) || strpos($options['parent'], ',') !== false) {
207
            $list = is_array($options['parent']) ? $options['parent'] : explode(',', $options['parent']);
208
            $first = true;
209
            $where = [];
210
            foreach ($list as $categoryId) {
211
                if (!$categoryId) {
212
                    continue;
213
                }
214
                $category = Category::get($categoryId);
215
                $where[] = ['tree_path', $category->tree_path . (int) $categoryId . '/%', 'LIKE', $first ? 'AND' : 'OR'];
216
                $first = false;
217
            }
218
            $selectOptions['where'][] = $where;
219
        } else {
220
            $category = Category::get($options['parent']);
221
            if ($category) {
222
                $selectOptions['where'][] = ['tree_path', $category->tree_path . (int) $options['parent'] . '/%', 'LIKE'];
223
            }
224
        }
225
    }
226
227
    public static function search(&$options, &$selectOptions) {
228
        if (empty($options['search'])) {
229
            return;
230
        }
231
        $searchStr = preg_replace('![^A-zА-я0-9 ]!iSu', ' ', $options['search']);
232
        $searchArr = [];
233
        foreach (explode(' ', $searchStr) as $part) {
234
            $part = trim($part);
235
            if ($part && strlen($part) > 2) {
236
                $searchArr[] = ['search_index', '%' . $part . '%', 'LIKE'];
237
            }
238
        }
239
        if (!empty($searchArr)) {
240
            $selectOptions['where'][] = $searchArr;
241
        }
242
    }
243
244
    public static function warehouse(&$selectOptions) {
245
        if (!empty(\App::$cur->Ecommerce->config['view_empty_warehouse'])) {
246
            return;
247
        }
248
        $warehouseIds = self::getWarehouses();
249
250
        $selectOptions['where'][] = [
251
            '(
252
          (SELECT COALESCE(sum(`' . Item\Offer\Warehouse::colPrefix() . 'count`),0) 
253
            FROM ' . \App::$cur->db->table_prefix . Item\Offer\Warehouse::table() . ' iciw 
254
            WHERE iciw.' . Item\Offer\Warehouse::colPrefix() . Item\Offer::index() . ' = ' . Item\Offer::index() . '
255
                ' . ($warehouseIds ? ' AND iciw.' . Item\Offer\Warehouse::colPrefix() . Warehouse::index() . ' IN(' . implode(',', $warehouseIds) . ')' : '') . '
256
            )
257
          -
258
          (SELECT COALESCE(sum(' . Warehouse\Block::colPrefix() . 'count) ,0)
259
            FROM ' . \App::$cur->db->table_prefix . Warehouse\Block::table() . ' iewb
260
            inner JOIN ' . \App::$cur->db->table_prefix . Cart::table() . ' icc ON icc.' . Cart::index() . ' = iewb.' . Warehouse\Block::colPrefix() . Cart::index() . ' AND (
261
                (`' . Cart::colPrefix() . 'warehouse_block` = 1 and `' . Cart::colPrefix() . 'cart_status_id` in(2,3,6)) ||
262
                (`' . Cart::colPrefix() . Cart\Status::index() . '` in(0,1) and `' . Cart::colPrefix() . 'date_last_activ` >=subdate(now(),INTERVAL 30 MINUTE))
263
            )
264
            WHERE iewb.' . Warehouse\Block::colPrefix() . Item\Offer::index() . ' = ' . Item\Offer::index() . ')
265
          )',
266
            0,
267
            '>'
268
        ];
269
    }
270
271
    public static function getWarehouses() {
272
        if (!\App::$cur->geography || !\Geography\City::$cur) {
273
            return [];
274
        }
275
        $warehouseIds = [];
276
        $warehouses = \Geography\City\Data::get([['code', 'warehouses'], ['city_id', \Geography\City::$cur->id]]);
277
        if ($warehouses && $warehouses->data) {
278
            foreach (explode(',', $warehouses->data) as $id) {
279
                $warehouseIds[$id] = $id;
280
            }
281
        }
282
        return $warehouseIds;
283
    }
284
}