| Total Complexity | 84 |
| Total Lines | 393 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| Bugs | 3 | Features | 0 |
Complex classes like ecommerceController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ecommerceController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class ecommerceController extends Controller { |
||
|
|
|||
| 12 | |||
| 13 | public function submitReviewAction() { |
||
| 14 | $result = new \Server\Result(); |
||
| 15 | if (!empty($_POST['review']['item_id']) && !empty($_POST['review']['name']) && !empty($_POST['review']['text'])) { |
||
| 16 | $item = Ecommerce\Item::get((int) $_POST['review']['item_id']); |
||
| 17 | if (!$item) { |
||
| 18 | $result->success = false; |
||
| 19 | $result->content = ['errorText' => 'Товар не найден']; |
||
| 20 | return $result->send(); |
||
| 21 | } |
||
| 22 | $review = new Ecommerce\Item\Review([ |
||
| 23 | 'item_id' => $item->id, |
||
| 24 | 'user_id' => \Users\User::$cur->id, |
||
| 25 | 'name' => htmlspecialchars($_POST['review']['name']), |
||
| 26 | 'rating' => (int) $_POST['review']['rating'], |
||
| 27 | 'text' => htmlspecialchars($_POST['review']['text']), |
||
| 28 | 'mail' => !empty($_POST['review']['email']) ? htmlspecialchars($_POST['review']['email']) : '', |
||
| 29 | 'file_id' => !empty($_FILES['review']['tmp_name']['file']) ? App::$cur->files->upload([ |
||
| 30 | 'tmp_name' => $_FILES['review']['tmp_name']['file'], |
||
| 31 | 'name' => $_FILES['review']['name']['file'] |
||
| 32 | ]) : 0 |
||
| 33 | ]); |
||
| 34 | $review->save(); |
||
| 35 | $result->successMsg = 'Отзыв успешно оставлен, он появится после модерации'; |
||
| 36 | return $result->send(); |
||
| 37 | } |
||
| 38 | $result->success = false; |
||
| 39 | $result->content = ['errorText' => 'Не все поля были заполнены']; |
||
| 40 | return $result->send(); |
||
| 41 | } |
||
| 42 | |||
| 43 | public function buyCardAction() { |
||
| 109 | } |
||
| 110 | |||
| 111 | public function autoCompleteAction() { |
||
| 112 | $return = Cache::get('itemsAutocomplete', [''], function () { |
||
| 113 | $items = $this->ecommerce->getItems(['array' => true]); |
||
| 114 | $return = []; |
||
| 115 | foreach ($items as $item) { |
||
| 116 | $return[] = ['name' => $item['item_name'], 'search' => $item['item_search_index']]; |
||
| 117 | } |
||
| 118 | return gzcompress(json_encode($return)); |
||
| 119 | }, 4 * 60 * 60); |
||
| 120 | echo gzuncompress($return); |
||
| 121 | } |
||
| 122 | |||
| 123 | public function indexAction() { |
||
| 124 | if (empty($this->module->config['catalogPresentPage'])) { |
||
| 125 | Tools::redirect('/ecommerce/itemList'); |
||
| 126 | } |
||
| 127 | $this->view->page(); |
||
| 128 | } |
||
| 129 | |||
| 130 | public function itemListAction($category_id = 0) { |
||
| 131 | //search |
||
| 132 | if (!empty($_GET['search'])) { |
||
| 133 | if (!empty($_GET['inCatalog'])) { |
||
| 134 | $category_id = (int) $_GET['inCatalog']; |
||
| 135 | } |
||
| 136 | $search = $_GET['search']; |
||
| 137 | } else { |
||
| 138 | $search = ''; |
||
| 139 | } |
||
| 140 | |||
| 141 | //sort |
||
| 142 | if (!empty($_GET['sort'])) { |
||
| 143 | $sort = $_GET['sort']; |
||
| 144 | } elseif (!empty($this->ecommerce->config['defaultSort'])) { |
||
| 145 | $sort = $this->ecommerce->config['defaultSort']; |
||
| 146 | } else { |
||
| 147 | $sort = ['name' => 'asc']; |
||
| 148 | } |
||
| 149 | |||
| 150 | //category |
||
| 151 | $category = null; |
||
| 152 | $categoryClass = 'Ecommerce\Category'; |
||
| 153 | if (!empty($this->module->config['catalogReplace'])) { |
||
| 154 | $categoryClass = 'Ecommerce\Catalog'; |
||
| 155 | } |
||
| 156 | if ($category_id) { |
||
| 157 | |||
| 158 | if (is_numeric($category_id)) { |
||
| 159 | $category = $categoryClass::get((int) $category_id); |
||
| 160 | } |
||
| 161 | if (!$category) { |
||
| 162 | $category = $categoryClass::get((int) $category_id, 'alias'); |
||
| 163 | } |
||
| 164 | if ($category) { |
||
| 165 | $category_id = $category->id; |
||
| 166 | } else { |
||
| 167 | $category_id = 0; |
||
| 168 | } |
||
| 169 | |||
| 170 | } else { |
||
| 171 | $category_id = 0; |
||
| 172 | } |
||
| 173 | if ($category) { |
||
| 174 | $category->views++; |
||
| 175 | $category->save(); |
||
| 176 | } |
||
| 177 | $active = $category_id; |
||
| 178 | if (!empty($_GET['categorys'])) { |
||
| 179 | $categorysList = $_GET['categorys']; |
||
| 180 | } elseif ($categoryClass === 'Ecommerce\Catalog' && $category) { |
||
| 181 | $categorysList = array_keys($category->categories(['key' => 'category_id'])); |
||
| 182 | } else { |
||
| 183 | $categorysList = $category_id; |
||
| 184 | } |
||
| 185 | |||
| 186 | //items pages |
||
| 187 | $pages = new \Ui\Pages($_GET, ['count' => $this->ecommerce->getItemsCount([ |
||
| 188 | 'parent' => $categorysList, |
||
| 189 | 'search' => trim($search), |
||
| 190 | 'filters' => !empty($_GET['filters']) ? $_GET['filters'] : [] |
||
| 191 | ]), |
||
| 192 | 'limit' => !empty($this->Ecommerce->config['default_limit']) ? $this->Ecommerce->config['default_limit'] : 18, |
||
| 193 | ]); |
||
| 194 | if (!empty(App::$cur->ecommerce->config['list_all']) && !empty($_GET['limit']) && $_GET['limit'] == 'all') { |
||
| 195 | $pages->params['start'] = 0; |
||
| 196 | $pages->params['limit'] = 0; |
||
| 197 | $pages->params['pages'] = 1; |
||
| 198 | } |
||
| 199 | |||
| 200 | //bread |
||
| 201 | $bread = []; |
||
| 202 | if (!$category || !$category->name) { |
||
| 203 | if (!empty($_GET['filters']['best'])) { |
||
| 204 | $bread[] = array('text' => 'Каталог', 'href' => '/ecommerce'); |
||
| 205 | $bread[] = array('text' => 'Рекомендумые товары'); |
||
| 206 | } else { |
||
| 207 | $bread[] = array('text' => 'Каталог'); |
||
| 208 | } |
||
| 209 | $this->view->setTitle('Каталог'); |
||
| 210 | } else { |
||
| 211 | $bread[] = array('text' => 'Каталог', 'href' => '/ecommerce'); |
||
| 212 | $categoryIds = array_values(array_filter(explode('/', $category->tree_path))); |
||
| 213 | foreach ($categoryIds as $id) { |
||
| 214 | $cat = Ecommerce\Category::get($id); |
||
| 215 | $bread[] = array('text' => $cat->name, 'href' => '/ecommerce/itemList/' . $cat->id); |
||
| 216 | } |
||
| 217 | $bread[] = array('text' => $category->name); |
||
| 218 | $this->view->setTitle($category->name); |
||
| 219 | } |
||
| 220 | |||
| 221 | //items |
||
| 222 | $items = $this->ecommerce->getItems([ |
||
| 223 | 'parent' => $categorysList, |
||
| 224 | 'start' => $pages->params['start'], |
||
| 225 | 'count' => $pages->params['limit'], |
||
| 226 | 'search' => trim($search), |
||
| 227 | 'sort' => $sort, |
||
| 228 | 'filters' => !empty($_GET['filters']) ? $_GET['filters'] : [] |
||
| 229 | ]); |
||
| 230 | |||
| 231 | //params |
||
| 232 | if (!empty(App::$cur->ecommerce->config['filtersByRel'])) { |
||
| 233 | $categorysList = is_array($categorysList) ? $categorysList : explode(',', $categorysList); |
||
| 234 | $categorysList = array_filter($categorysList); |
||
| 235 | $opts = []; |
||
| 236 | foreach ($categorysList as $categoryId) { |
||
| 237 | $cat = \Ecommerce\Category::get($categoryId); |
||
| 238 | if ($cat) { |
||
| 239 | $opts = array_merge($opts, array_keys($cat->options(['key' => 'item_option_id']))); |
||
| 240 | } |
||
| 241 | } |
||
| 242 | $opts = array_unique($opts); |
||
| 243 | if ($opts) { |
||
| 244 | $options = \Ecommerce\Item\Option::getList(['where' => [['item_option_searchable', 1], ['id', $opts, 'IN']], 'order' => ['weight', 'asc']]); |
||
| 245 | } else { |
||
| 246 | $options = []; |
||
| 247 | } |
||
| 248 | } elseif (empty(App::$cur->ecommerce->config['filtersInLast'])) { |
||
| 249 | $options = \Ecommerce\Item\Option::getList(['where' => ['item_option_searchable', 1], 'order' => ['weight', 'asc']]); |
||
| 250 | } else { |
||
| 251 | $params = $this->ecommerce->getItemsParams([ |
||
| 252 | 'parent' => $categorysList, |
||
| 253 | 'search' => trim($search), |
||
| 254 | 'filters' => !empty($_GET['filters']) ? $_GET['filters'] : [] |
||
| 255 | ]); |
||
| 256 | $ids = []; |
||
| 257 | foreach ($params as $param) { |
||
| 258 | $ids[] = $param->item_option_id; |
||
| 259 | } |
||
| 260 | if ($ids) { |
||
| 261 | $options = \Ecommerce\Item\Option::getList(['where' => ['id', $ids, 'IN'], 'order' => ['weight', 'asc']]); |
||
| 262 | } else { |
||
| 263 | $options = []; |
||
| 264 | } |
||
| 265 | } |
||
| 266 | |||
| 267 | |||
| 268 | //child categorys |
||
| 269 | if ($category) { |
||
| 270 | $categorys = $category->catalogs; |
||
| 271 | } else { |
||
| 272 | $categorys = \Ecommerce\Category::getList(['where' => ['parent_id', 0]]); |
||
| 273 | } |
||
| 274 | |||
| 275 | //view content |
||
| 276 | $this->view->page([ |
||
| 277 | 'page' => $category ? $category->resolveTemplate() : (!empty(App::$cur->ecommerce->config['defaultCategoryTemplate']) ? App::$cur->ecommerce->config['defaultCategoryTemplate'] : 'current'), |
||
| 278 | 'content' => $category ? $category->resolveViewer() : (!empty(App::$cur->ecommerce->config['defaultCategoryView']) ? App::$cur->ecommerce->config['defaultCategoryView'] : 'itemList'), |
||
| 279 | 'data' => compact('active', 'category', 'sort', 'search', 'pages', 'items', 'categorys', 'bread', 'options')]); |
||
| 280 | } |
||
| 281 | |||
| 282 | public function favoritesAction() { |
||
| 283 | $count = $this->module->getFavoriteCount(); |
||
| 284 | //items pages |
||
| 285 | $pages = new \Ui\Pages($_GET, [ |
||
| 286 | 'count' => $count, |
||
| 287 | 'limit' => !empty($this->Ecommerce->config['default_limit']) ? $this->Ecommerce->config['default_limit'] : 18, |
||
| 288 | ]); |
||
| 289 | if (Users\User::$cur->id) { |
||
| 290 | $favs = \Ecommerce\Favorite::getList(['where' => ['user_id', Users\User::$cur->id], 'key' => 'item_id', 'start' => $pages->params['start'], 'limit' => $pages->params['limit']]); |
||
| 291 | $ids = array_keys($favs); |
||
| 292 | } else { |
||
| 293 | $favs = !empty($_COOKIE['ecommerce_favitems']) ? json_decode($_COOKIE['ecommerce_favitems'], true) : []; |
||
| 294 | $ids = array_slice($favs, $pages->params['start'], $pages->params['limit']); |
||
| 295 | } |
||
| 296 | if ($ids) { |
||
| 297 | //items |
||
| 298 | $items = \Ecommerce\Item::getList(['where' => ['id', $ids, 'IN']]); |
||
| 299 | } |
||
| 300 | |||
| 301 | $bread = []; |
||
| 302 | $bread[] = ['text' => 'Каталог', 'href' => '/ecommerce/itemList/']; |
||
| 303 | $bread[] = ['text' => 'Избранное']; |
||
| 304 | $this->view->setTitle('Избранное'); |
||
| 305 | $this->view->page(['data' => compact('pages', 'items', 'bread')]); |
||
| 306 | } |
||
| 307 | |||
| 308 | public function viewAction($id = '', $quick = 0) { |
||
| 362 | } |
||
| 363 | } |
||
| 364 | |||
| 365 | public function toggleFavAction($itemId) { |
||
| 404 | } |
||
| 405 | } |
||
| 406 | } |
||
| 407 | } |
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths