Complex classes like Cart 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Cart, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class Cart extends \Model { |
||
15 | |||
16 | public static $objectName = 'Корзины'; |
||
17 | |||
18 | public static function indexes() { |
||
41 | |||
42 | public static function relations() { |
||
101 | |||
102 | public function beforeDelete() { |
||
116 | |||
117 | public static $labels = [ |
||
118 | 'user_id' => 'Пользователь', |
||
119 | 'cart_status_id' => 'Статус', |
||
120 | 'delivery_id' => 'Доставка', |
||
121 | 'comment' => 'Комментарий', |
||
122 | 'bonus_used' => 'Выгодные рубли', |
||
123 | 'complete_data' => 'Время заказа', |
||
124 | 'info' => 'Информация', |
||
125 | 'items' => 'Товары', |
||
126 | 'paytype_id' => 'Способ оплаты', |
||
127 | 'payed' => 'Оплачен', |
||
128 | 'exported' => 'Выгружено', |
||
129 | 'warehouse_block' => 'Блокировка товаров', |
||
130 | 'extra' => 'Доп.', |
||
131 | 'card_item_id' => 'Дисконтная карта', |
||
132 | 'info' => 'Информация', |
||
133 | 'contacts' => 'Информация', |
||
134 | 'pay' => 'Счета', |
||
135 | 'sums' => 'Суммы', |
||
136 | 'deliveryInfo' => 'Для доставки', |
||
137 | 'discount' => 'Скидки', |
||
138 | ]; |
||
139 | public static $cols = [ |
||
140 | //Основные параметры |
||
141 | 'user_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'user'], |
||
142 | 'cart_status_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'status'], |
||
143 | 'delivery_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'delivery'], |
||
144 | 'paytype_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'payType'], |
||
145 | 'card_item_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'card'], |
||
146 | 'warehouse_block' => ['type' => 'bool'], |
||
147 | 'payed' => ['type' => 'bool'], |
||
148 | 'comment' => ['type' => 'textarea'], |
||
149 | //Системные |
||
150 | 'exported' => ['type' => 'bool'], |
||
151 | 'complete_data' => ['type' => 'dateTime', 'null' => true, 'emptyValue' => null], |
||
152 | 'date_status' => ['type' => 'dateTime', 'null' => true, 'emptyValue' => null], |
||
153 | 'date_last_activ' => ['type' => 'dateTime', 'null' => true, 'emptyValue' => null], |
||
154 | 'date_create' => ['type' => 'dateTime'], |
||
155 | //Виджеты |
||
156 | 'sums' => [ |
||
157 | 'type' => 'void', |
||
158 | 'view' => [ |
||
159 | 'type' => 'widget', |
||
160 | 'widget' => 'Ecommerce\adminSums', |
||
161 | ], |
||
162 | ], |
||
163 | 'contacts' => [ |
||
164 | 'type' => 'void', |
||
165 | 'view' => [ |
||
166 | 'type' => 'widget', |
||
167 | 'widget' => 'Ecommerce\admin/contacts', |
||
168 | ], |
||
169 | ], |
||
170 | //Менеджеры |
||
171 | 'extra' => ['type' => 'dataManager', 'relation' => 'extras'], |
||
172 | 'pay' => ['type' => 'dataManager', 'relation' => 'pays'], |
||
173 | 'items' => ['type' => 'dataManager', 'relation' => 'cartItems'], |
||
174 | 'info' => ['type' => 'dataManager', 'relation' => 'infos'], |
||
175 | 'deliveryInfo' => ['type' => 'dataManager', 'relation' => 'deliveryInfos'], |
||
176 | 'discount' => ['type' => 'dataManager', 'relation' => 'discounts'], |
||
177 | ]; |
||
178 | public static $dataManagers = [ |
||
179 | 'manager' => [ |
||
180 | 'cols' => [ |
||
181 | 'contacts', |
||
182 | 'items', |
||
183 | 'extra', |
||
184 | 'discount', |
||
185 | 'sums', |
||
186 | 'cart_status_id', |
||
187 | 'delivery_id', |
||
188 | 'deliveryInfo', |
||
189 | 'payed', |
||
190 | 'pay', |
||
191 | 'complete_data', |
||
192 | ], |
||
193 | 'sortable' => [ |
||
194 | 'cart_status_id', |
||
195 | 'delivery_id', |
||
196 | 'payed', |
||
197 | 'complete_data', |
||
198 | ], |
||
199 | 'filters' => [ |
||
200 | 'cart_status_id', |
||
201 | 'delivery_id', |
||
202 | 'payed', |
||
203 | 'complete_data', |
||
204 | ], |
||
205 | 'preSort' => [ |
||
206 | 'complete_data' => 'desc' |
||
207 | ], |
||
208 | 'actions' => [ |
||
209 | 'Ecommerce\CloseCartBtn', 'Open', 'Edit', 'Delete' |
||
210 | ] |
||
211 | ] |
||
212 | ]; |
||
213 | |||
214 | public static function itemName($item) { |
||
217 | |||
218 | public static $forms = [ |
||
219 | 'manager' => [ |
||
220 | 'inputs' => [ |
||
221 | 'userSearch' => [ |
||
222 | 'type' => 'search', |
||
223 | 'source' => 'relation', |
||
224 | 'relation' => 'user', |
||
225 | 'label' => 'Покупатель', |
||
226 | 'cols' => [ |
||
227 | 'info:first_name', |
||
228 | 'info:last_name', |
||
229 | 'info:middle_name', |
||
230 | 'mail' |
||
231 | ], |
||
232 | 'col' => 'user_id', |
||
233 | 'required' => true, |
||
234 | 'showCol' => [ |
||
235 | 'type' => 'staticMethod', |
||
236 | 'class' => 'Ecommerce\Cart', |
||
237 | 'method' => 'itemName', |
||
238 | ], |
||
239 | ], |
||
240 | 'cardSearch' => [ |
||
241 | 'type' => 'search', |
||
242 | 'source' => 'relation', |
||
243 | 'relation' => 'card', |
||
244 | 'label' => 'Дисконтная карта', |
||
245 | 'cols' => [ |
||
246 | 'code', |
||
247 | 'user:info:first_name', |
||
248 | 'user:info:last_name', |
||
249 | 'user:info:middle_name', |
||
250 | 'user:mail' |
||
251 | ], |
||
252 | 'col' => 'card_item_id', |
||
253 | ], |
||
254 | ], |
||
255 | 'map' => [ |
||
256 | ['userSearch', 'cart_status_id'], |
||
257 | ['paytype_id', 'delivery_id'], |
||
258 | ['cardSearch', 'comment'], |
||
259 | ['warehouse_block', 'complete_data'], |
||
260 | ['payed'], |
||
261 | ['items'], |
||
262 | ['extra'], |
||
263 | ['pay'], |
||
264 | ['info'], |
||
265 | ['deliveryInfo'] |
||
266 | ] |
||
267 | ], |
||
268 | ]; |
||
269 | |||
270 | public function checkStage() { |
||
271 | $sum = $this->itemsSum(); |
||
272 | $stages = Cart\Stage::getList(['order' => ['sum', 'asc']]); |
||
273 | $groups = []; |
||
274 | foreach ($stages as $stage) { |
||
275 | if ($sum->greater(new \Money\Sums([$stage->currency_id => $stage->sum])) || $sum->equal(new \Money\Sums([$stage->currency_id => $stage->sum]))) { |
||
276 | $groups[$stage->group] = $stage; |
||
277 | } |
||
278 | } |
||
279 | $discounts = Cart\Discount::getList(['where' => ['cart_id', $this->id]]); |
||
280 | foreach ($discounts as $discount) { |
||
281 | if (!isset($groups[$discount->group]) && $discount->auto) { |
||
282 | $discount->delete(); |
||
283 | } |
||
284 | if (isset($groups[$discount->group]) && $groups[$discount->group]->type == 'discount') { |
||
285 | $discount->discount_id = $groups[$discount->group]->value; |
||
286 | $discount->save(); |
||
287 | unset($groups[$discount->group]); |
||
288 | } |
||
289 | } |
||
290 | foreach ($groups as $group) { |
||
291 | if ($group && $group->type == 'discount') { |
||
292 | $rel = $this->addRelation('discounts', $group->value); |
||
293 | $rel->auto = true; |
||
294 | $rel->group = 'discount'; |
||
295 | $rel->save(); |
||
296 | } |
||
297 | } |
||
298 | } |
||
299 | |||
300 | public function needDelivery() { |
||
308 | |||
309 | public function deliverySum() { |
||
332 | |||
333 | public function hasDiscount() { |
||
336 | |||
337 | public function discountSum() { |
||
344 | |||
345 | public function finalSum() { |
||
351 | |||
352 | public function itemsSum() { |
||
363 | |||
364 | public function addItem($offer_price_id, $count = 1, $final_price = 0) { |
||
384 | |||
385 | public function calc($save = true) { |
||
390 | |||
391 | } |
||
392 |