| Total Complexity | 86 |
| Total Lines | 363 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like CartController 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 CartController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class CartController extends Controller { |
||
|
|
|||
| 18 | |||
| 19 | public function indexAction() { |
||
| 180 | } |
||
| 181 | |||
| 182 | public function orderDetailAction($id = 0) { |
||
| 183 | $cart = Ecommerce\Cart::get((int) $id); |
||
| 184 | if ($cart->user_id != Users\User::$cur->id) { |
||
| 185 | $this->url->redirect('/', 'Это не ваша корзина'); |
||
| 186 | } |
||
| 187 | $bread = []; |
||
| 188 | $bread[] = [ |
||
| 189 | 'text' => 'Каталог', |
||
| 190 | 'href' => '/ecommerce' |
||
| 191 | ]; |
||
| 192 | $bread[] = [ |
||
| 193 | 'text' => 'Корзина', |
||
| 194 | 'href' => '/ecommerce/cart' |
||
| 195 | ]; |
||
| 196 | $bread[] = [ |
||
| 197 | 'text' => 'Заказ: №' . $cart->id, |
||
| 198 | 'href' => '/ecommerce/cart/orderDetail/' . $cart->id |
||
| 199 | ]; |
||
| 200 | $this->view->setTitle('Заказ №' . $cart->id); |
||
| 201 | $this->view->page(['data' => compact('cart', 'bread')]); |
||
| 202 | } |
||
| 203 | |||
| 204 | public function continueAction($id = 0) { |
||
| 205 | $cart = \Ecommerce\Cart::get((int) $id); |
||
| 206 | if ($cart->user_id != Users\User::$cur->id) { |
||
| 207 | Tools::redirect('/', 'Это не ваша корзина'); |
||
| 208 | } |
||
| 209 | if ($cart->cart_status_id > 1) { |
||
| 210 | Tools::redirect('/', 'Корзина уже оформлена'); |
||
| 211 | } |
||
| 212 | $_SESSION['cart']['cart_id'] = $cart->id; |
||
| 213 | Tools::redirect('/ecommerce/cart'); |
||
| 214 | } |
||
| 215 | |||
| 216 | public function deleteAction($id = 0) { |
||
| 217 | $cart = \Ecommerce\Cart::get((int) $id); |
||
| 218 | if ($cart->user_id != Users\User::$cur->id) { |
||
| 219 | Tools::redirect('/', 'Это не ваша корзина'); |
||
| 220 | } |
||
| 221 | if ($cart->cart_status_id > 1) { |
||
| 222 | Tools::redirect('/', 'Корзина уже оформлена'); |
||
| 223 | } |
||
| 224 | if (!empty($_SESSION['cart']['cart_id']) && $_SESSION['cart']['cart_id'] == $cart->id) { |
||
| 225 | unset($_SESSION['cart']['cart_id']); |
||
| 226 | } |
||
| 227 | $cart->delete(); |
||
| 228 | Tools::redirect('/users/cabinet/ecommerceOrdersHistory', 'Корзина была удалена', 'success'); |
||
| 229 | } |
||
| 230 | |||
| 231 | public function refillAction($id = 0) { |
||
| 232 | $cart = \Ecommerce\Cart::get((int) $id); |
||
| 233 | if ($cart->user_id != Users\User::$cur->id) { |
||
| 234 | Tools::redirect('/', 'Это не ваша корзина'); |
||
| 235 | } |
||
| 236 | if (!empty($_SESSION['cart']['cart_id'])) { |
||
| 237 | unset($_SESSION['cart']['cart_id']); |
||
| 238 | } |
||
| 239 | $newCart = $this->ecommerce->getCurCart(); |
||
| 240 | foreach ($cart->cartItems as $cartitem) { |
||
| 241 | $newCart->addItem($cartitem->item_offer_price_id, $cartitem->count); |
||
| 242 | } |
||
| 243 | |||
| 244 | $newCart->save(); |
||
| 245 | |||
| 246 | Tools::redirect('/ecommerce/cart/'); |
||
| 247 | } |
||
| 248 | |||
| 249 | public function successAction() { |
||
| 250 | $bread = []; |
||
| 251 | $bread[] = [ |
||
| 252 | 'text' => 'Каталог', |
||
| 253 | 'href' => '/ecommerce' |
||
| 254 | ]; |
||
| 255 | $bread[] = [ |
||
| 256 | 'text' => 'Корзина', |
||
| 257 | 'href' => '/ecommerce/cart' |
||
| 258 | ]; |
||
| 259 | $bread[] = [ |
||
| 260 | 'text' => 'Заказ принят', |
||
| 261 | 'href' => '/ecommerce/cart/success' |
||
| 262 | ]; |
||
| 263 | $this->view->setTitle('Заказ принят'); |
||
| 264 | $this->view->page(['data' => compact('bread')]); |
||
| 265 | } |
||
| 266 | |||
| 267 | public function addAction() { |
||
| 268 | $result = new Server\Result(); |
||
| 269 | if (empty($_GET['itemOfferPriceId'])) { |
||
| 270 | $result->success = false; |
||
| 271 | $result->content = 'Произошла непредвиденная ошибка при добавлении товара'; |
||
| 272 | $result->send(); |
||
| 273 | } |
||
| 274 | |||
| 275 | $price = \Ecommerce\Item\Offer\Price::get((int) $_GET['itemOfferPriceId']); |
||
| 276 | if (!$price) { |
||
| 277 | $result->success = false; |
||
| 278 | $result->content = 'Такой цены не найдено'; |
||
| 279 | $result->send(); |
||
| 280 | } |
||
| 281 | |||
| 282 | $offer = $price->offer; |
||
| 283 | if (!$offer) { |
||
| 284 | $result->success = false; |
||
| 285 | $result->content = 'Такого предложения не существует'; |
||
| 286 | $result->send(); |
||
| 287 | } |
||
| 288 | |||
| 289 | $item = $price->offer->item; |
||
| 290 | |||
| 291 | if (!$item) { |
||
| 292 | $result->success = false; |
||
| 293 | $result->content = 'Такого товара не существует'; |
||
| 294 | $result->send(); |
||
| 295 | } |
||
| 296 | |||
| 297 | $cart = $this->ecommerce->getCurCart(); |
||
| 298 | /** |
||
| 299 | * @var \Ecommerce\Cart\Item[] $cartItems |
||
| 300 | */ |
||
| 301 | $cartItems =[]; |
||
| 302 | foreach ($cart->cartItems as $cartItem){ |
||
| 303 | $cartItems[$cartItem->price->item_offer_id] = $cartItem; |
||
| 304 | } |
||
| 305 | if (!empty($this->ecommerce->config['cartAddToggle']) && isset($cartItems[$offer->id])) { |
||
| 306 | $cartItems[$offer->id]->delete(); |
||
| 307 | $cart = $this->ecommerce->getCurCart(); |
||
| 308 | $cart->date_last_activ = date('Y-m-d H:i:s'); |
||
| 309 | $item->sales--; |
||
| 310 | $cart->calc(true); |
||
| 311 | $result->successMsg = '<a href="/ecommerce/view/' . $item->id . '">' . $item->name() . ($price->offer->name() && $price->offer->name() != $item->name() ? ' (' . $price->offer->name() . ')' : '') . '</a> удален <a href="/ecommerce/cart">из корзины покупок</a>!'; |
||
| 312 | $result->content = ['result' => 'toggleDelete']; |
||
| 313 | return $result->send(); |
||
| 314 | } |
||
| 315 | |||
| 316 | if (empty($_GET['count'])) { |
||
| 317 | $count = 1; |
||
| 318 | } else { |
||
| 319 | $count = (float) $_GET['count']; |
||
| 320 | } |
||
| 321 | |||
| 322 | if (empty($this->module->config['sell_over_warehouse']) && $price->offer->warehouseCount() < $count) { |
||
| 323 | $result->success = false; |
||
| 324 | $result->content = 'На складе недостаточно товара! Доступно: ' . $price->offer->warehouseCount(); |
||
| 325 | $result->send(); |
||
| 326 | } |
||
| 327 | $price = $price->offer->getPrice($cart); |
||
| 328 | if (!isset($cartItems[$offer->id])) { |
||
| 329 | $cart->addItem($price->id, $count); |
||
| 330 | $result->content = ['result' => 'addNew']; |
||
| 331 | } else { |
||
| 332 | $cartItems[$offer->id]->count += $count; |
||
| 333 | $cartItems[$offer->id]->item_offer_price_id = $price->id; |
||
| 334 | $cartItems[$offer->id]->save(); |
||
| 335 | $result->content = ['result' => 'addCount']; |
||
| 336 | } |
||
| 337 | $cart->date_last_activ = date('Y-m-d H:i:s'); |
||
| 338 | $cart->calc(true); |
||
| 339 | |||
| 340 | $item->sales++; |
||
| 341 | $item->save(); |
||
| 342 | |||
| 343 | $result->successMsg = '<a href="/ecommerce/view/' . $item->id . '">' . $price->name() . '</a> добавлен <a href="/ecommerce/cart">в корзину покупок</a>!'; |
||
| 344 | $result->send(); |
||
| 345 | } |
||
| 346 | |||
| 347 | public function deleteItemAction() { |
||
| 371 | } |
||
| 372 | |||
| 373 | public function getcartAction() { |
||
| 380 | } |
||
| 381 | |||
| 382 | } |
||
| 383 |
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