Conditions | 53 |
Paths | > 20000 |
Total Lines | 162 |
Code Lines | 122 |
Lines | 16 |
Ratio | 9.88 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
19 | public function indexAction() { |
||
20 | $deliverys = \Ecommerce\Delivery::getList(['where' => ['disabled', 0], 'order' => ['weight', 'ASC']]); |
||
21 | $cart = $this->ecommerce->getCurCart(false); |
||
22 | if ($cart && !empty($_POST)) { |
||
23 | $error = false; |
||
24 | $user = Users\User::$cur; |
||
25 | View Code Duplication | if (!Users\User::$cur->id) { |
|
|
|||
26 | $user_id = $this->Users->registration($_POST, true); |
||
27 | if (!$user_id) { |
||
28 | $error = true; |
||
29 | } else { |
||
30 | $user = Users\User::get($user_id); |
||
31 | } |
||
32 | } |
||
33 | $ids = []; |
||
34 | if (!empty($_POST['cartItems'])) { |
||
35 | foreach ($_POST['cartItems'] as $cartItemId => $cartItemCont) { |
||
36 | $cartItem = \Ecommerce\Cart\Item::get((int) $cartItemId); |
||
37 | if (!$cartItem) { |
||
38 | continue; |
||
39 | } |
||
40 | if ($cartItem->cart_id != $cart->id) { |
||
41 | continue; |
||
42 | } |
||
43 | $count = (float) $cartItemCont; |
||
44 | if ($count < 0.001) { |
||
45 | $count = 1; |
||
46 | } |
||
47 | $cartItem->count = $count; |
||
48 | $cartItem->save(); |
||
49 | $ids[] = $cartItemId; |
||
50 | } |
||
51 | } |
||
52 | foreach ($cart->cartItems as $cartItem) { |
||
53 | if (!in_array($cartItem->id, $ids)) { |
||
54 | $cartItem->delete(); |
||
55 | } |
||
56 | } |
||
57 | $cart = Ecommerce\Cart::get($cart->id); |
||
58 | if (!$cart->cartItems) { |
||
59 | $error = true; |
||
60 | } |
||
61 | if (empty($this->module->config['sell_over_warehouse'])) { |
||
62 | foreach ($cart->cartItems as $cartitem) { |
||
63 | $warecount = $cartitem->price->offer->warehouseCount($cart->id); |
||
64 | if ($cartitem->count > $warecount) { |
||
65 | $error = true; |
||
66 | Msg::add('Вы заказали <b>' . $cartitem->item->name . '</b> больше чем есть на складе. на складе: <b>' . $warecount . '</b>', 'danger'); |
||
67 | } |
||
68 | } |
||
69 | } |
||
70 | $this->module->parseFields($_POST['userAdds']['fields'], $cart); |
||
71 | if ($deliverys && !$cart->delivery_id && (empty($_POST['delivery']) || empty($deliverys[$_POST['delivery']]))) { |
||
72 | $error = 1; |
||
73 | Msg::add('Выберите способ доставки', 'danger'); |
||
74 | } elseif ($deliverys && !empty($_POST['delivery']) && !empty($deliverys[$_POST['delivery']])) { |
||
75 | $cart->delivery_id = $_POST['delivery']; |
||
76 | } |
||
77 | if ($cart->delivery) { |
||
78 | foreach ($deliverys[$cart->delivery_id]->fields as $field) { |
||
79 | View Code Duplication | if (empty($_POST['deliveryFields'][$field->id]) && $field->required) { |
|
80 | $error = 1; |
||
81 | Msg::add('Вы не указали: ' . $field->name, 'danger'); |
||
82 | } |
||
83 | } |
||
84 | $this->module->parseDeliveryFields($_POST['deliveryFields'], $cart, $cart->delivery->fields); |
||
85 | } |
||
86 | |||
87 | $payTypes = $cart->availablePayTypes(); |
||
88 | $payType = false; |
||
89 | if ($payTypes && (empty($_POST['payType']) || empty($payTypes[$_POST['payType']]) || ($cart->paytype_id && !isset($payTypes[$cart->paytype_id])))) { |
||
90 | $error = 1; |
||
91 | Msg::add('Выберите способ оплаты', 'danger'); |
||
92 | } elseif ($payTypes && !empty($payTypes[$_POST['payType']])) { |
||
93 | $payType = $payTypes[$_POST['payType']]; |
||
94 | $cart->paytype_id = $payType->id; |
||
95 | } |
||
96 | foreach (\Ecommerce\UserAdds\Field::getList() as $field) { |
||
97 | View Code Duplication | if (empty($_POST['userAdds']['fields'][$field->id]) && $field->required) { |
|
98 | $error = 1; |
||
99 | Msg::add('Вы не указали: ' . $field->name, 'danger'); |
||
100 | } |
||
101 | } |
||
102 | if (!empty($_POST['discounts']['card_item_id'])) { |
||
103 | $userCard = \Ecommerce\Card\Item::get((int) $_POST['discounts']['card_item_id']); |
||
104 | if (!$userCard) { |
||
105 | $error = true; |
||
106 | Msg::add('Такой карты не существует', 'danger'); |
||
107 | } elseif ($userCard->user_id != $user->id) { |
||
108 | $error = true; |
||
109 | Msg::add('Это не ваша карта', 'danger'); |
||
110 | } else { |
||
111 | $cart->card_item_id = $userCard->id; |
||
112 | } |
||
113 | } |
||
114 | |||
115 | $cart->save(); |
||
116 | if (!$error && !empty($_POST['action']) && $_POST['action'] = 'order') { |
||
117 | $cart->user_id = $user->user_id; |
||
118 | $cart->cart_status_id = 2; |
||
119 | $cart->comment = !empty($_POST['comment']) ? htmlspecialchars($_POST['comment']) : ''; |
||
120 | $cart->date_status = date('Y-m-d H:i:s'); |
||
121 | $cart->complete_data = date('Y-m-d H:i:s'); |
||
122 | $cart->warehouse_block = 1; |
||
123 | $cart->save(); |
||
124 | |||
125 | $cart = \Ecommerce\Cart::get($cart->id); |
||
126 | foreach ($cart->cartItems as $cartItem) { |
||
127 | $cartItem->discount = $cartItem->discount(); |
||
128 | $cartItem->final_price = $cartItem->price->price - $cartItem->discount; |
||
129 | $cartItem->save(); |
||
130 | } |
||
131 | $cart = \Ecommerce\Cart::get($cart->id); |
||
132 | |||
133 | $orderInfo = $cart->buildOrderInfo(); |
||
134 | $domain = App::$cur->getDomain(true); |
||
135 | $domainRaw = App::$cur->getDomain(); |
||
136 | $title = 'Новый заказ в интернет магазине на сайте ' . $domain; |
||
137 | |||
138 | if ($user && !empty($user->mail)) { |
||
139 | $text = '<p><b><a href = "http://' . App::$cur->getDomain() . '/ecommerce/cart/orderDetail/' . ($cart->id) . '">Посмотреть на сайте</a></b></p>' . $orderInfo; |
||
140 | \Tools::sendMail('noreply@' . $domainRaw, $cart->user->mail, $title, $text); |
||
141 | } |
||
142 | if (!empty(\App::$cur->ecommerce->config['notify_mail'])) { |
||
143 | $text = '<p><b><a href = "http://' . App::$cur->getDomain() . '/admin/Ecommerce/view/Cart/' . ($cart->id) . '">Открыть заказ в админ панеле</a></b></p>' . $orderInfo; |
||
144 | \Tools::sendMail('noreply@' . $domainRaw, \App::$cur->ecommerce->config['notify_mail'], $title, $text); |
||
145 | } |
||
146 | |||
147 | if ($this->notifications) { |
||
148 | $notification = new Notifications\Notification(); |
||
149 | $notification->name = 'Новый заказ в интернет магазине на сайте ' . idn_to_utf8(INJI_DOMAIN_NAME); |
||
150 | $notification->text = 'Перейдите в админ панель чтобы просмотреть новый заказ'; |
||
151 | $notification->chanel_id = $this->notifications->getChanel('Ecommerce-orders')->id; |
||
152 | $notification->save(); |
||
153 | } |
||
154 | $handlers = $this->ecommerce->getSnippets('payTypeHandler'); |
||
155 | $redirect = ['/ecommerce/cart/success']; |
||
156 | if ($payType && !empty($handlers[$payType->handler]['handler'])) { |
||
157 | $newRedirect = $handlers[$payType->handler]['handler']($cart); |
||
158 | if (!empty($newRedirect)) { |
||
159 | $redirect = $newRedirect; |
||
160 | } |
||
161 | } |
||
162 | unset($_SESSION['cart']['cart_id']); |
||
163 | call_user_func_array(['Tools', 'redirect'], $redirect); |
||
164 | } |
||
165 | |||
166 | } elseif ($cart) { |
||
167 | $payTypes = $cart->availablePayTypes(); |
||
168 | } |
||
169 | $this->view->setTitle('Корзина'); |
||
170 | $bread = []; |
||
171 | $bread[] = [ |
||
172 | 'text' => 'Каталог', |
||
173 | 'href' => '/ecommerce' |
||
174 | ]; |
||
175 | $bread[] = [ |
||
176 | 'text' => 'Корзина', |
||
177 | 'href' => '/ecommerce/cart' |
||
178 | ]; |
||
179 | $this->view->page(['data' => compact('cart', 'items', 'deliverys', 'payTypes', 'packItem', 'bread')]); |
||
180 | } |
||
181 | |||
383 |
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.