Failed Conditions
Pull Request — master (#1665)
by k-yamamura
74:32
created

ShoppingController   F

Complexity

Total Complexity 124

Size/Duplication

Total Lines 1435
Duplicated Lines 13.52 %

Coupling/Cohesion

Components 1
Dependencies 17

Test Coverage

Coverage 66.43%

Importance

Changes 0
Metric Value
dl 194
loc 1435
ccs 477
cts 718
cp 0.6643
rs 0.6314
c 0
b 0
f 0
wmc 124
lcom 1
cbo 17

17 Methods

Rating   Name   Duplication   Size   Complexity  
C index() 5 98 11
D confirm() 8 129 10
B complete() 0 26 2
D delivery() 0 101 9
B payment() 0 67 5
B shippingChange() 43 43 5
C shipping() 0 84 7
B shippingEditChange() 43 43 5
C shippingEdit() 7 101 11
C customer() 12 80 7
B login() 6 35 5
D nonmember() 5 128 9
B shippingMultipleChange() 43 43 5
F shippingMultiple() 22 214 26
A shippingMultipleEdit() 0 58 4
A shoppingError() 0 15 2
A customerValidation() 0 68 1

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like ShoppingController 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 ShoppingController, and based on these observations, apply Extract Interface, too.

1
<?php
2
/*
3
 * This file is part of EC-CUBE
4
 *
5
 * Copyright(c) 2000-2015 LOCKON CO.,LTD. All Rights Reserved.
6
 *
7
 * http://www.lockon.co.jp/
8
 *
9
 * This program is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU General Public License
11
 * as published by the Free Software Foundation; either version 2
12
 * of the License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
 */
23
24
25
namespace Eccube\Controller;
26
27
use Eccube\Application;
28
use Eccube\Common\Constant;
29
use Eccube\Entity\Customer;
30
use Eccube\Entity\CustomerAddress;
31
use Eccube\Entity\ShipmentItem;
32
use Eccube\Entity\Shipping;
33
use Eccube\Event\EccubeEvents;
34
use Eccube\Event\EventArgs;
35
use Eccube\Exception\CartException;
36
use Eccube\Exception\ShoppingException;
37
use Symfony\Component\HttpFoundation\Request;
38
use Symfony\Component\HttpFoundation\Response;
39
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
40
use Symfony\Component\Validator\Constraints as Assert;
41
42
class ShoppingController extends AbstractController
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
43
{
44
45
    /**
46
     * @var string 非会員用セッションキー
47
     */
48
    private $sessionKey = 'eccube.front.shopping.nonmember';
49
50
    /**
51
     * @var string 非会員用セッションキー
52
     */
53
    private $sessionCustomerAddressKey = 'eccube.front.shopping.nonmember.customeraddress';
54
55
    /**
56
     * @var string 複数配送警告メッセージ
57
     */
58
    private $sessionMultipleKey = 'eccube.front.shopping.multiple';
59
60
    /**
61
     * @var string 受注IDキー
62
     */
63
    private $sessionOrderKey = 'eccube.front.shopping.order.id';
64
65
    /**
66
     * 購入画面表示
67
     *
68
     * @param Application $app
69
     * @param Request $request
0 ignored issues
show
introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
70
     * @return \Symfony\Component\HttpFoundation\RedirectResponse|Response
71
     */
72 57
    public function index(Application $app, Request $request)
73
    {
74 57
        $cartService = $app['eccube.service.cart'];
75
76
        // カートチェック
77 57
        if (!$cartService->isLocked()) {
78 3
            log_info('カートが存在しません');
79
            // カートが存在しない、カートがロックされていない時はエラー
80 3
            return $app->redirect($app->url('cart'));
81
        }
82
83
        // カートチェック
84 54 View Code Duplication
        if (count($cartService->getCart()->getCartItems()) <= 0) {
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...
85 7
            log_info('カートに商品が入っていないためショッピングカート画面にリダイレクト');
86
            // カートが存在しない時はエラー
87 7
            return $app->redirect($app->url('cart'));
88
        }
89
90
        // 登録済みの受注情報を取得
91 50
        $Order = $app['eccube.service.shopping']->getOrder($app['config']['order_processing']);
92
93
        // 初回アクセス(受注情報がない)の場合は, 受注情報を作成
94 50
        if (is_null($Order)) {
95
            // 未ログインの場合, ログイン画面へリダイレクト.
96 50
            if (!$app->isGranted('IS_AUTHENTICATED_FULLY')) {
97
                // 非会員でも一度会員登録されていればショッピング画面へ遷移
98 12
                $Customer = $app['eccube.service.shopping']->getNonMember($this->sessionKey);
99
100 12
                if (is_null($Customer)) {
101
                    log_info('未ログインのためログイン画面にリダイレクト');
102 12
                    return $app->redirect($app->url('shopping_login'));
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
103
                }
104
            } else {
105 38
                $Customer = $app->user();
106
            }
107
108
            try {
109
                // 受注情報を作成
110 50
                $Order = $app['eccube.service.shopping']->createOrder($Customer);
111
            } catch (CartException $e) {
112
                log_error('初回受注情報作成エラー', array($e->getMessage()));
113
                $app->addRequestError($e->getMessage());
114
                return $app->redirect($app->url('cart'));
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
115
            }
116
117
            // セッション情報を削除
118 50
            $app['session']->remove($this->sessionOrderKey);
119 50
            $app['session']->remove($this->sessionMultipleKey);
120
        }
121
122
        // 受注関連情報を最新状態に更新
123 50
        $app['orm.em']->refresh($Order);
124
125
        // form作成
126 50
        $builder = $app['eccube.service.shopping']->getShippingFormBuilder($Order);
127
128 50
        $event = new EventArgs(
129
            array(
130 50
                'builder' => $builder,
131 50
                'Order' => $Order,
132
            ),
133
            $request
134
        );
135 50
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_SHOPPING_INDEX_INITIALIZE, $event);
136
137 50
        $form = $builder->getForm();
138
139 50
        if ($Order->getTotalPrice() < 0) {
140
            // 合計金額がマイナスの場合、エラー
141
            log_info('受注金額マイナスエラー', array($Order->getId()));
142
            $message = $app->trans('shopping.total.price', array('totalPrice' => number_format($Order->getTotalPrice())));
143
            $app->addError($message);
144
145
            return $app->redirect($app->url('shopping_error'));
146
        }
147
148
        // 複数配送の場合、エラーメッセージを一度だけ表示
149 50
        if (!$app['session']->has($this->sessionMultipleKey)) {
150 50
            if (count($Order->getShippings()) > 1) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
151
152
                $BaseInfo = $app['eccube.repository.base_info']->get();
153
154
                if (!$BaseInfo->getOptionMultipleShipping()) {
155
                    // 複数配送に設定されていないのに複数配送先ができればエラー
156
                    $app->addRequestError('cart.product.type.kind');
157
                    return $app->redirect($app->url('cart'));
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
158
                }
159
160
                $app->addError('shopping.multiple.delivery');
161
            }
162 50
            $app['session']->set($this->sessionMultipleKey, 'multiple');
163
        }
164
165 50
        return $app->render('Shopping/index.twig', array(
166 50
            'form' => $form->createView(),
167 50
            'Order' => $Order,
168
        ));
169
    }
170
171
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
introduced by
Doc comment for parameter "$request" missing
Loading history...
172
     * 購入処理
173
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
174 15
    public function confirm(Application $app, Request $request)
175
    {
176 15
        $cartService = $app['eccube.service.cart'];
177
178
        // カートチェック
179 15
        if (!$cartService->isLocked()) {
180
            // カートが存在しない、カートがロックされていない時はエラー
181
            log_info('カートが存在しません');
182
            return $app->redirect($app->url('cart'));
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
183
        }
184
185 15
        $Order = $app['eccube.service.shopping']->getOrder($app['config']['order_processing']);
186 15
        if (!$Order) {
187
            log_info('購入処理中の受注情報がないため購入エラー');
188
            $app->addError('front.shopping.order.error');
189
            return $app->redirect($app->url('shopping_error'));
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
190
        }
191
192 15
        if ('POST' !== $request->getMethod()) {
193
            return $app->redirect($app->url('cart'));
194
        }
195
196
        // form作成
197 15
        $builder = $app['eccube.service.shopping']->getShippingFormBuilder($Order);
198
199 15
        $event = new EventArgs(
200
            array(
201 15
                'builder' => $builder,
202 15
                'Order' => $Order,
203
            ),
204
            $request
205
        );
206 15
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_SHOPPING_CONFIRM_INITIALIZE, $event);
207
208 15
        $form = $builder->getForm();
209
210 15
        $form->handleRequest($request);
211
212 15
        if ($form->isSubmitted() && $form->isValid()) {
213 15
            $data = $form->getData();
214
215 15
            log_info('購入処理開始', array($Order->getId()));
216
217
            // トランザクション制御
218 15
            $em = $app['orm.em'];
219 15
            $em->getConnection()->beginTransaction();
220
            try {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
221
222
                // お問い合わせ、配送時間などのフォーム項目をセット
223 15
                $app['eccube.service.shopping']->setFormData($Order, $data);
224
                // 購入処理
225 15
                $app['eccube.service.shopping']->processPurchase($Order);
226
227 15
                $em->flush();
228 15
                $em->getConnection()->commit();
229
230 15
                log_info('購入処理完了', array($Order->getId()));
231
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
232
            } catch (ShoppingException $e) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
233
234
                log_error('購入エラー', array($e->getMessage()));
235
236
                $em->getConnection()->rollback();
237
238
                $app->log($e);
239
                $app->addError($e->getMessage());
240
241
                return $app->redirect($app->url('shopping_error'));
242
            } catch (\Exception $e) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
243
244
                log_error('予期しないエラー', array($e->getMessage()));
245
246
                $em->getConnection()->rollback();
247
248
                $app->log($e);
249
250
                $app->addError('front.shopping.system.error');
251
                return $app->redirect($app->url('shopping_error'));
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
252
            }
253
254
            // カート削除
255 15
            $app['eccube.service.cart']->clear()->save();
256
257 15
            $event = new EventArgs(
258
                array(
259 15
                    'form' => $form,
260 15
                    'Order' => $Order,
261
                ),
262
                $request
263
            );
264 15
            $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_SHOPPING_CONFIRM_PROCESSING, $event);
265
266 15 View Code Duplication
            if ($event->getResponse() !== null) {
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...
267
                log_info('イベントレスポンス返却', array($Order->getId()));
268
                return $event->getResponse();
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
269
            }
270
271
            // 受注IDをセッションにセット
272 15
            $app['session']->set($this->sessionOrderKey, $Order->getId());
273
274
            // メール送信
275 15
            $MailHistory = $app['eccube.service.shopping']->sendOrderMail($Order);
276
277 15
            $event = new EventArgs(
278
                array(
279 15
                    'form' => $form,
280 15
                    'Order' => $Order,
281 15
                    'MailHistory' => $MailHistory,
282
                ),
283
                $request
284
            );
285 15
            $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_SHOPPING_CONFIRM_COMPLETE, $event);
286
287 15 View Code Duplication
            if ($event->getResponse() !== null) {
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...
288
                log_info('イベントレスポンス返却', array($Order->getId()));
289
                return $event->getResponse();
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
290
            }
291
292
            // 完了画面表示
293 15
            return $app->redirect($app->url('shopping_complete'));
294
        }
295
296
        log_info('購入チェックエラー', array($Order->getId()));
297
298
        return $app->render('Shopping/index.twig', array(
299
            'form' => $form->createView(),
300
            'Order' => $Order,
301
        ));
302
    }
303
304
305
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
introduced by
Doc comment for parameter "$request" missing
Loading history...
306
     * 購入完了画面表示
307
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
308 8
    public function complete(Application $app, Request $request)
309
    {
310
        // 受注IDを取得
311 8
        $orderId = $app['session']->get($this->sessionOrderKey);
312
313 8
        $event = new EventArgs(
314
            array(
315 8
                'orderId' => $orderId,
316
            ),
317
            $request
318
        );
319 8
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_SHOPPING_COMPLETE_INITIALIZE, $event);
320
321 8
        if ($event->getResponse() !== null) {
322
            return $event->getResponse();
323
        }
324
325
        // 受注IDセッションを削除
326 8
        $app['session']->remove($this->sessionOrderKey);
327
328 8
        log_info('購入処理完了', array($orderId));
329
330 8
        return $app->render('Shopping/complete.twig', array(
331 8
            'orderId' => $orderId,
332
        ));
333
    }
334
335
336
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
introduced by
Doc comment for parameter "$request" missing
Loading history...
337
     * 配送業者選択処理
338
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
339 6
    public function delivery(Application $app, Request $request)
340
    {
341
        // カートチェック
342 6
        if (!$app['eccube.service.cart']->isLocked()) {
343
            // カートが存在しない、カートがロックされていない時はエラー
344
            log_info('カートが存在しません');
345
            return $app->redirect($app->url('cart'));
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
346
        }
347
348 6
        $Order = $app['eccube.service.shopping']->getOrder($app['config']['order_processing']);
349 6
        if (!$Order) {
350
            log_info('購入処理中の受注情報がないため購入エラー');
351
            $app->addError('front.shopping.order.error');
352
            return $app->redirect($app->url('shopping_error'));
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
353
        }
354
355 6
        if ('POST' !== $request->getMethod()) {
356
            return $app->redirect($app->url('shopping'));
357
        }
358
359 6
        $builder = $app['eccube.service.shopping']->getShippingFormBuilder($Order);
360
361 6
        $event = new EventArgs(
362
            array(
363 6
                'builder' => $builder,
364 6
                'Order' => $Order,
365
            ),
366
            $request
367
        );
368 6
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_SHOPPING_DELIVERY_INITIALIZE, $event);
369
370 6
        $form = $builder->getForm();
371
372 6
        $form->handleRequest($request);
373
374 6
        if ($form->isSubmitted() && $form->isValid()) {
375 2
            log_info('配送業者変更処理開始', array($Order->getId()));
376
377 2
            $data = $form->getData();
378
379 2
            $shippings = $data['shippings'];
380
381 2
            $productDeliveryFeeTotal = 0;
382 2
            $BaseInfo = $app['eccube.repository.base_info']->get();
383
384 2
            foreach ($shippings as $Shipping) {
385 2
                $Delivery = $Shipping->getDelivery();
386
387 2
                if ($Delivery) {
388 2
                    $deliveryFee = $app['eccube.repository.delivery_fee']->findOneBy(array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
389 2
                        'Delivery' => $Delivery,
390 2
                        'Pref' => $Shipping->getPref()
391
                    ));
392
393
                    // 商品ごとの配送料合計
394 2
                    if (!is_null($BaseInfo->getOptionProductDeliveryFee())) {
395 2
                        $productDeliveryFeeTotal += $app['eccube.service.shopping']->getProductDeliveryFee($Shipping);
396
                    }
397
398 2
                    $Shipping->setDeliveryFee($deliveryFee);
399 2
                    $Shipping->setShippingDeliveryFee($deliveryFee->getFee() + $productDeliveryFeeTotal);
400 2
                    $Shipping->setShippingDeliveryName($Delivery->getName());
401
                }
402
            }
403
404
            // 支払い情報をセット
405 2
            $payment = $data['payment'];
406 2
            $message = $data['message'];
407
408 2
            $Order->setPayment($payment);
409 2
            $Order->setPaymentMethod($payment->getMethod());
410 2
            $Order->setMessage($message);
411 2
            $Order->setCharge($payment->getCharge());
412
413 2
            $Order->setDeliveryFeeTotal($app['eccube.service.shopping']->getShippingDeliveryFeeTotal($shippings));
414
415
            // 合計金額の再計算
416 2
            $Order = $app['eccube.service.shopping']->getAmount($Order);
417
418
            // 受注関連情報を最新状態に更新
419 2
            $app['orm.em']->flush();
420
421 2
            $event = new EventArgs(
422
                array(
423 2
                    'form' => $form,
424 2
                    'Order' => $Order,
425
                ),
426
                $request
427
            );
428 2
            $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_SHOPPING_DELIVERY_COMPLETE, $event);
429
430 2
            log_info('配送業者変更処理完了', array($Order->getId()));
431 2
            return $app->redirect($app->url('shopping'));
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
432
        }
433
434 4
        log_info('配送業者変更入力チェックエラー', array($Order->getId()));
435 4
        return $app->render('Shopping/index.twig', array(
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
436 4
            'form' => $form->createView(),
437 4
            'Order' => $Order,
438
        ));
439
    }
440
441
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
introduced by
Doc comment for parameter "$request" missing
Loading history...
442
     * 支払い方法選択処理
443
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
444 9
    public function payment(Application $app, Request $request)
445
    {
446 9
        $Order = $app['eccube.service.shopping']->getOrder($app['config']['order_processing']);
447 9
        if (!$Order) {
448
            log_info('購入処理中の受注情報がないため購入エラー');
449
            $app->addError('front.shopping.order.error');
450
            return $app->redirect($app->url('shopping_error'));
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
451
        }
452
453 9
        if ('POST' !== $request->getMethod()) {
454
            return $app->redirect($app->url('shopping'));
455
        }
456
457 9
        $builder = $app['eccube.service.shopping']->getShippingFormBuilder($Order);
458
459 9
        $event = new EventArgs(
460
            array(
461 9
                'builder' => $builder,
462 9
                'Order' => $Order,
463
            ),
464
            $request
465
        );
466 9
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_SHOPPING_PAYMENT_INITIALIZE, $event);
467
468 9
        $form = $builder->getForm();
469
470 9
        $form->handleRequest($request);
471
472 9
        if ($form->isSubmitted() && $form->isValid()) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
473
474 7
            log_info('支払い方法変更処理開始', array("id" => $Order->getId()));
475
476 7
            $data = $form->getData();
477 7
            $payment = $data['payment'];
478 7
            $message = $data['message'];
479
480 7
            $Order->setPayment($payment);
481 7
            $Order->setPaymentMethod($payment->getMethod());
482 7
            $Order->setMessage($message);
483 7
            $Order->setCharge($payment->getCharge());
484
485
            // 合計金額の再計算
486 7
            $Order = $app['eccube.service.shopping']->getAmount($Order);
487
488
            // 受注関連情報を最新状態に更新
489 7
            $app['orm.em']->flush();
490
491 7
            $event = new EventArgs(
492
                array(
493 7
                    'form' => $form,
494 7
                    'Order' => $Order,
495
                ),
496
                $request
497
            );
498 7
            $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_SHOPPING_PAYMENT_COMPLETE, $event);
499
500 7
            log_info('支払い方法変更処理完了', array("id" => $Order->getId(), "payment" => $payment->getId()));
501
502 7
            return $app->redirect($app->url('shopping'));
503
        }
504
505 2
        log_info('支払い方法変更入力チェックエラー', array("id" => $Order->getId()));
506 2
        return $app->render('Shopping/index.twig', array(
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
507 2
            'form' => $form->createView(),
508 2
            'Order' => $Order,
509
        ));
510
    }
511
512
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
introduced by
Doc comment for parameter "$request" missing
Loading history...
introduced by
Doc comment for parameter "$id" missing
Loading history...
513
     * お届け先変更がクリックされた場合の処理
514
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
515 6 View Code Duplication
    public function shippingChange(Application $app, Request $request, $id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
516
    {
517 6
        $Order = $app['eccube.service.shopping']->getOrder($app['config']['order_processing']);
518 6
        if (!$Order) {
519
            $app->addError('front.shopping.order.error');
520
            return $app->redirect($app->url('shopping_error'));
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
521
        }
522
523 6
        if ('POST' !== $request->getMethod()) {
524
            return $app->redirect($app->url('shopping'));
525
        }
526
527 6
        $builder = $app['eccube.service.shopping']->getShippingFormBuilder($Order);
528
529 6
        $event = new EventArgs(
530
            array(
531 6
                'builder' => $builder,
532 6
                'Order' => $Order,
533
            ),
534
            $request
535
        );
536 6
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_SHOPPING_SHIPPING_CHANGE_INITIALIZE, $event);
537
538 6
        $form = $builder->getForm();
539
540 6
        $form->handleRequest($request);
541
542 6
        if ($form->isSubmitted() && $form->isValid()) {
543 6
            $data = $form->getData();
544 6
            $message = $data['message'];
545 6
            $Order->setMessage($message);
546
            // 受注情報を更新
547 6
            $app['orm.em']->flush();
548
549
            // お届け先設定一覧へリダイレクト
550 6
            return $app->redirect($app->url('shopping_shipping', array('id' => $id)));
551
        }
552
553
        return $app->render('Shopping/index.twig', array(
554
            'form' => $form->createView(),
555
            'Order' => $Order,
556
        ));
557
    }
558
559
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
introduced by
Doc comment for parameter "$request" missing
Loading history...
introduced by
Doc comment for parameter "$id" missing
Loading history...
560
     * お届け先の設定一覧からの選択
561
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
562 4
    public function shipping(Application $app, Request $request, $id)
563
    {
564
        // カートチェック
565 4
        if (!$app['eccube.service.cart']->isLocked()) {
566
            // カートが存在しない、カートがロックされていない時はエラー
567
            log_info('カートが存在しません');
568
            return $app->redirect($app->url('cart'));
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
569
        }
570
571 4
        if ('POST' === $request->getMethod()) {
572
            $address = $request->get('address');
573
574
            if (is_null($address)) {
575
                // 選択されていなければエラー
576
                log_info('お届け先入力チェックエラー');
577
                return $app->render(
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
578
                    'Shopping/shipping.twig',
579
                    array(
580
                        'Customer' => $app->user(),
581
                        'shippingId' => $id,
582
                        'error' => true,
583
                    )
584
                );
585
            }
586
587
            // 選択されたお届け先情報を取得
588
            $CustomerAddress = $app['eccube.repository.customer_address']->findOneBy(array(
589
                'Customer' => $app->user(),
590
                'id' => $address,
591
            ));
592
            if (is_null($CustomerAddress)) {
593
                throw new NotFoundHttpException('選択されたお届け先住所が存在しない');
594
            }
595
596
            $Order = $app['eccube.service.shopping']->getOrder($app['config']['order_processing']);
597
            if (!$Order) {
598
                log_info('購入処理中の受注情報がないため購入エラー');
599
                $app->addError('front.shopping.order.error');
600
601
                return $app->redirect($app->url('shopping_error'));
602
            }
603
604
            $Shipping = $Order->findShipping($id);
605
            if (!$Shipping) {
606
                throw new NotFoundHttpException('お届け先情報が存在しない');
607
            }
608
609
            log_info('お届先情報更新開始', array($Shipping->getId()));
610
611
            // お届け先情報を更新
612
            $Shipping
613
                ->setFromCustomerAddress($CustomerAddress);
614
615
            // 配送料金の設定
616
            $app['eccube.service.shopping']->setShippingDeliveryFee($Shipping);
617
618
            // 合計金額の再計算
619
            $Order = $app['eccube.service.shopping']->getAmount($Order);
620
621
            // 配送先を更新
622
            $app['orm.em']->flush();
623
624
            $event = new EventArgs(
625
                array(
626
                    'Order' => $Order,
627
                    'shippingId' => $id,
628
                ),
629
                $request
630
            );
631
            $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_SHOPPING_SHIPPING_COMPLETE, $event);
632
633
            log_info('お届先情報更新完了', array($Shipping->getId()));
634
            return $app->redirect($app->url('shopping'));
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
635
        }
636
637 4
        return $app->render(
638 4
            'Shopping/shipping.twig',
639
            array(
640 4
                'Customer' => $app->user(),
641 4
                'shippingId' => $id,
642
                'error' => false,
643
            )
644
        );
645
    }
646
647
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
introduced by
Doc comment for parameter "$request" missing
Loading history...
introduced by
Doc comment for parameter "$id" missing
Loading history...
648
     * お届け先の設定(非会員)がクリックされた場合の処理
649
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
650 5 View Code Duplication
    public function shippingEditChange(Application $app, Request $request, $id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
651
    {
652 5
        $Order = $app['eccube.service.shopping']->getOrder($app['config']['order_processing']);
653 5
        if (!$Order) {
654
            $app->addError('front.shopping.order.error');
655
            return $app->redirect($app->url('shopping_error'));
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
656
        }
657
658 5
        if ('POST' !== $request->getMethod()) {
659 1
            return $app->redirect($app->url('shopping'));
660
        }
661
662 4
        $builder = $app['eccube.service.shopping']->getShippingFormBuilder($Order);
663
664 4
        $event = new EventArgs(
665
            array(
666 4
                'builder' => $builder,
667 4
                'Order' => $Order,
668
            ),
669
            $request
670
        );
671 4
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_SHOPPING_SHIPPING_EDIT_CHANGE_INITIALIZE, $event);
672
673 4
        $form = $builder->getForm();
674
675 4
        $form->handleRequest($request);
676
677 4
        if ($form->isSubmitted() && $form->isValid()) {
678 3
            $data = $form->getData();
679 3
            $message = $data['message'];
680 3
            $Order->setMessage($message);
681
            // 受注情報を更新
682 3
            $app['orm.em']->flush();
683
684
            // お届け先設定一覧へリダイレクト
685 3
            return $app->redirect($app->url('shopping_shipping_edit', array('id' => $id)));
686
        }
687
688 1
        return $app->render('Shopping/index.twig', array(
689 1
            'form' => $form->createView(),
690 1
            'Order' => $Order,
691
        ));
692
    }
693
694
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
introduced by
Doc comment for parameter "$request" missing
Loading history...
introduced by
Doc comment for parameter "$id" missing
Loading history...
695
     * お届け先の設定(非会員でも使用する)
696
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
697 4
    public function shippingEdit(Application $app, Request $request, $id)
698
    {
699
        // 配送先住所最大値判定
700 4
        $Customer = $app->user();
701 4 View Code Duplication
        if ($app->isGranted('IS_AUTHENTICATED_FULLY')) {
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...
702 2
            $addressCurrNum = count($app->user()->getCustomerAddresses());
703 2
            $addressMax = $app['config']['deliv_addr_max'];
704 2
            if ($addressCurrNum >= $addressMax) {
705
                throw new NotFoundHttpException('配送先住所最大数エラー');
706
            }
707
        }
708
709
        // カートチェック
710 4
        if (!$app['eccube.service.cart']->isLocked()) {
711
            // カートが存在しない、カートがロックされていない時はエラー
712
            log_info('カートが存在しません');
713
            return $app->redirect($app->url('cart'));
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
714
        }
715
716 4
        $Order = $app['eccube.service.shopping']->getOrder($app['config']['order_processing']);
717 4
        if (!$Order) {
718
            log_info('購入処理中の受注情報がないため購入エラー');
719
            $app->addError('front.shopping.order.error');
720
            return $app->redirect($app->url('shopping_error'));
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
721
        }
722
723 4
        $Shipping = $Order->findShipping($id);
724 4
        if (!$Shipping) {
725
            throw new NotFoundHttpException('設定されている配送先が存在しない');
726
        }
727 4
        if ($app->isGranted('IS_AUTHENTICATED_FULLY')) {
728 2
            $Shipping->clearCustomerAddress();
729
        }
730
731 4
        $CustomerAddress = new CustomerAddress();
732 4
        if ($app->isGranted('IS_AUTHENTICATED_FULLY')) {
733 2
            $CustomerAddress->setCustomer($Customer);
734
        } else {
735 2
            $CustomerAddress->setFromShipping($Shipping);
736
        }
737
738 4
        $builder = $app['form.factory']->createBuilder('shopping_shipping', $CustomerAddress);
739
740 4
        $event = new EventArgs(
741
            array(
742 4
                'builder' => $builder,
743 4
                'Order' => $Order,
744 4
                'Shipping' => $Shipping,
745 4
                'CustomerAddress' => $CustomerAddress,
746
            ),
747
            $request
748
        );
749 4
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_SHOPPING_SHIPPING_EDIT_INITIALIZE, $event);
750
751 4
        $form = $builder->getForm();
752
753 4
        $form->handleRequest($request);
754
755 4
        if ($form->isSubmitted() && $form->isValid()) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
756
757 3
            log_info('お届け先追加処理開始', array('id' => $Order->getId(), 'shipping' => $id));
758
759
            // 会員の場合、お届け先情報を新規登録
760 3
            $Shipping->setFromCustomerAddress($CustomerAddress);
761
762 3
            if ($Customer instanceof Customer) {
0 ignored issues
show
Bug introduced by
The class Eccube\Entity\Customer does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
763 2
                $app['orm.em']->persist($CustomerAddress);
764 2
                log_info('新規お届け先登録', array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
765 2
                    'id' => $Order->getId(),
766 2
                    'shipping' => $id,
767 2
                    'customer address' => $CustomerAddress->getId()));
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 16 spaces, but found 20.
Loading history...
768
            }
769
770
            // 配送料金の設定
771 3
            $app['eccube.service.shopping']->setShippingDeliveryFee($Shipping);
772
773
            // 合計金額の再計算
774 3
            $app['eccube.service.shopping']->getAmount($Order);
775
776
            // 配送先を更新 
777 3
            $app['orm.em']->flush();
778
779 3
            $event = new EventArgs(
780
                array(
781 3
                    'form' => $form,
782 3
                    'Shipping' => $Shipping,
783 3
                    'CustomerAddress' => $CustomerAddress,
784
                ),
785
                $request
786
            );
787 3
            $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_SHOPPING_SHIPPING_EDIT_COMPLETE, $event);
788
789 3
            log_info('お届け先追加処理完了', array('id' => $Order->getId(), 'shipping' => $id));
790 3
            return $app->redirect($app->url('shopping'));
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
791
        }
792
793 3
        return $app->render('Shopping/shipping_edit.twig', array(
794 3
            'form' => $form->createView(),
795 3
            'shippingId' => $id,
796
        ));
797
    }
798
799
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
introduced by
Doc comment for parameter "$request" missing
Loading history...
800
     * お客様情報の変更(非会員)
801
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
802
    public function customer(Application $app, Request $request)
803
    {
804
        if ($request->isXmlHttpRequest()) {
805
            try {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
806
807
                log_info('非会員お客様情報変更処理開始');
808
809
                $data = $request->request->all();
810
811
                // 入力チェック
812
                $errors = $this->customerValidation($app, $data);
813
814
                foreach ($errors as $error) {
815 View Code Duplication
                    if ($error->count() != 0) {
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...
816
                        log_info('非会員お客様情報変更入力チェックエラー');
817
                        $response = new Response(json_encode('NG'), 400);
818
                        $response->headers->set('Content-Type', 'application/json');
819
                        return $response;
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
820
                    }
821
                }
822
823
                $pref = $app['eccube.repository.master.pref']->findOneBy(array('name' => $data['customer_pref']));
824 View Code Duplication
                if (!$pref) {
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...
825
                    log_info('非会員お客様情報変更入力チェックエラー');
826
                    $response = new Response(json_encode('NG'), 400);
827
                    $response->headers->set('Content-Type', 'application/json');
828
                    return $response;
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
829
                }
830
831
                $Order = $app['eccube.service.shopping']->getOrder($app['config']['order_processing']);
832
                if (!$Order) {
833
                    log_info('カートが存在しません');
834
                    $app->addError('front.shopping.order.error');
835
                    return $app->redirect($app->url('shopping_error'));
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
836
                }
837
838
                $Order
839
                    ->setName01($data['customer_name01'])
840
                    ->setName02($data['customer_name02'])
841
                    ->setCompanyName($data['customer_company_name'])
842
                    ->setTel01($data['customer_tel01'])
843
                    ->setTel02($data['customer_tel02'])
844
                    ->setTel03($data['customer_tel03'])
845
                    ->setZip01($data['customer_zip01'])
846
                    ->setZip02($data['customer_zip02'])
847
                    ->setZipCode($data['customer_zip01'].$data['customer_zip02'])
848
                    ->setPref($pref)
849
                    ->setAddr01($data['customer_addr01'])
850
                    ->setAddr02($data['customer_addr02'])
851
                    ->setEmail($data['customer_email']);
852
853
                // 配送先を更新
854
                $app['orm.em']->flush();
855
856
                // 受注関連情報を最新状態に更新
857
                $app['orm.em']->refresh($Order);
858
859
                $event = new EventArgs(
860
                    array(
861
                        'Order' => $Order,
862
                        'data' => $data,
863
                    ),
864
                    $request
865
                );
866
                $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_SHOPPING_CUSTOMER_INITIALIZE, $event);
867
868
                log_info('非会員お客様情報変更処理完了', array($Order->getId()));
869
                $response = new Response(json_encode('OK'));
870
                $response->headers->set('Content-Type', 'application/json');
871
            } catch (\Exception $e) {
872
                log_error('予期しないエラー', array($e->getMessage()));
873
                $app['monolog']->error($e);
874
875
                $response = new Response(json_encode('NG'), 500);
876
                $response->headers->set('Content-Type', 'application/json');
877
            }
878
879
            return $response;
880
        }
881
    }
882
883
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
introduced by
Doc comment for parameter "$request" missing
Loading history...
884
     * ログイン
885
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
886 4
    public function login(Application $app, Request $request)
887
    {
888 4
        if (!$app['eccube.service.cart']->isLocked()) {
889 3
            return $app->redirect($app->url('cart'));
890
        }
891
892 1
        if ($app->isGranted('IS_AUTHENTICATED_FULLY')) {
893
            return $app->redirect($app->url('shopping'));
894
        }
895
896
        /* @var $form \Symfony\Component\Form\FormInterface */
897 1
        $builder = $app['form.factory']->createNamedBuilder('', 'customer_login');
898
899 1 View Code Duplication
        if ($app->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
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...
900
            $Customer = $app->user();
901
            if ($Customer) {
902
                $builder->get('login_email')->setData($Customer->getEmail());
903
            }
904
        }
905
906 1
        $event = new EventArgs(
907
            array(
908 1
                'builder' => $builder,
909
            ),
910
            $request
911
        );
912 1
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_SHOPPING_LOGIN_INITIALIZE, $event);
913
914 1
        $form = $builder->getForm();
915
916 1
        return $app->render('Shopping/login.twig', array(
917 1
            'error' => $app['security.last_error']($request),
918 1
            'form' => $form->createView(),
919
        ));
920
    }
921
922
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
introduced by
Doc comment for parameter "$request" missing
Loading history...
923
     * 非会員処理
924
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
925 16
    public function nonmember(Application $app, Request $request)
926
    {
927 16
        $cartService = $app['eccube.service.cart'];
928
929
        // カートチェック
930 16
        if (!$cartService->isLocked()) {
931
            // カートが存在しない、カートがロックされていない時はエラー
932 1
            log_info('カートが存在しません');
933 1
            return $app->redirect($app->url('cart'));
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
934
        }
935
936
        // ログイン済みの場合は, 購入画面へリダイレクト.
937 15
        if ($app->isGranted('ROLE_USER')) {
938 1
            return $app->redirect($app->url('shopping'));
939
        }
940
941
        // カートチェック
942 14 View Code Duplication
        if (count($cartService->getCart()->getCartItems()) <= 0) {
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...
943
            // カートが存在しない時はエラー
944
            log_info('カートに商品が入っていないためショッピングカート画面にリダイレクト');
945
            return $app->redirect($app->url('cart'));
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
946
        }
947
948 14
        $builder = $app['form.factory']->createBuilder('nonmember');
949
950 14
        $event = new EventArgs(
951
            array(
952 14
                'builder' => $builder,
953
            ),
954
            $request
955
        );
956 14
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_SHOPPING_NONMEMBER_INITIALIZE, $event);
957
958 14
        $form = $builder->getForm();
959
960 14
        $form->handleRequest($request);
961
962 14
        if ($form->isSubmitted() && $form->isValid()) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
963
964 13
            log_info('非会員お客様情報登録開始');
965
966 13
            $data = $form->getData();
967 13
            $Customer = new Customer();
968
            $Customer
969 13
                ->setName01($data['name01'])
970 13
                ->setName02($data['name02'])
971 13
                ->setKana01($data['kana01'])
972 13
                ->setKana02($data['kana02'])
973 13
                ->setCompanyName($data['company_name'])
974 13
                ->setEmail($data['email'])
975 13
                ->setTel01($data['tel01'])
976 13
                ->setTel02($data['tel02'])
977 13
                ->setTel03($data['tel03'])
978 13
                ->setZip01($data['zip01'])
979 13
                ->setZip02($data['zip02'])
980 13
                ->setZipCode($data['zip01'].$data['zip02'])
981 13
                ->setPref($data['pref'])
982 13
                ->setAddr01($data['addr01'])
983 13
                ->setAddr02($data['addr02']);
984
985
            // 非会員複数配送用
986 13
            $CustomerAddress = new CustomerAddress();
987
            $CustomerAddress
988 13
                ->setCustomer($Customer)
989 13
                ->setName01($data['name01'])
990 13
                ->setName02($data['name02'])
991 13
                ->setKana01($data['kana01'])
992 13
                ->setKana02($data['kana02'])
993 13
                ->setCompanyName($data['company_name'])
994 13
                ->setTel01($data['tel01'])
995 13
                ->setTel02($data['tel02'])
996 13
                ->setTel03($data['tel03'])
997 13
                ->setZip01($data['zip01'])
998 13
                ->setZip02($data['zip02'])
999 13
                ->setZipCode($data['zip01'].$data['zip02'])
1000 13
                ->setPref($data['pref'])
1001 13
                ->setAddr01($data['addr01'])
1002 13
                ->setAddr02($data['addr02'])
1003 13
                ->setDelFlg(Constant::DISABLED);
1004 13
            $Customer->addCustomerAddress($CustomerAddress);
1005
1006
            // 受注情報を取得
1007 13
            $Order = $app['eccube.service.shopping']->getOrder($app['config']['order_processing']);
1008
1009
            // 初回アクセス(受注データがない)の場合は, 受注情報を作成
1010 13
            if (is_null($Order)) {
1011
                // 受注情報を作成
1012
                try {
1013
                    // 受注情報を作成
1014 13
                    $Order = $app['eccube.service.shopping']->createOrder($Customer);
1015
                } catch (CartException $e) {
1016
                    $app->addRequestError($e->getMessage());
1017
                    return $app->redirect($app->url('cart'));
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
1018
                }
1019
            }
1020
1021
            // 非会員用セッションを作成
1022 13
            $nonMember = array();
1023 13
            $nonMember['customer'] = $Customer;
1024 13
            $nonMember['pref'] = $Customer->getPref()->getId();
1025 13
            $app['session']->set($this->sessionKey, $nonMember);
1026
1027 13
            $customerAddresses = array();
1028 13
            $customerAddresses[] = $CustomerAddress;
1029 13
            $app['session']->set($this->sessionCustomerAddressKey, serialize($customerAddresses));
1030
1031 13
            $event = new EventArgs(
1032
                array(
1033 13
                    'form' => $form,
1034 13
                    'Order' => $Order,
1035
                ),
1036
                $request
1037
            );
1038 13
            $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_SHOPPING_NONMEMBER_COMPLETE, $event);
1039
1040 13
            if ($event->getResponse() !== null) {
1041
                return $event->getResponse();
1042
            }
1043
1044 13
            log_info('非会員お客様情報登録完了', array($Order->getId()));
1045
1046 13
            return $app->redirect($app->url('shopping'));
1047
        }
1048
1049 1
        return $app->render('Shopping/nonmember.twig', array(
1050 1
            'form' => $form->createView(),
1051
        ));
1052
    }
1053
1054
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
introduced by
Doc comment for parameter "$request" missing
Loading history...
1055
     * 複数配送処理がクリックされた場合の処理
1056
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
1057 View Code Duplication
    public function shippingMultipleChange(Application $app, Request $request)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
1058
    {
1059
        $Order = $app['eccube.service.shopping']->getOrder($app['config']['order_processing']);
1060
        if (!$Order) {
1061
            $app->addError('front.shopping.order.error');
1062
            return $app->redirect($app->url('shopping_error'));
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
1063
        }
1064
1065
        if ('POST' !== $request->getMethod()) {
1066
            return $app->redirect($app->url('shopping'));
1067
        }
1068
1069
        $builder = $app['eccube.service.shopping']->getShippingFormBuilder($Order);
1070
1071
        $event = new EventArgs(
1072
            array(
1073
                'builder' => $builder,
1074
                'Order' => $Order,
1075
            ),
1076
            $request
1077
        );
1078
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_SHOPPING_SHIPPING_MULTIPLE_CHANGE_INITIALIZE, $event);
1079
1080
        $form = $builder->getForm();
1081
1082
        $form->handleRequest($request);
1083
1084
        if ($form->isSubmitted() && $form->isValid()) {
1085
            $data = $form->getData();
1086
            $message = $data['message'];
1087
            $Order->setMessage($message);
1088
            // 受注情報を更新
1089
            $app['orm.em']->flush();
1090
1091
            // 複数配送設定へリダイレクト
1092
            return $app->redirect($app->url('shopping_shipping_multiple'));
1093
        }
1094
1095
        return $app->render('Shopping/index.twig', array(
1096
            'form' => $form->createView(),
1097
            'Order' => $Order,
1098
        ));
1099
    }
1100
1101
1102
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
introduced by
Doc comment for parameter "$request" missing
Loading history...
1103
     * 複数配送処理
1104
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
1105 3
    public function shippingMultiple(Application $app, Request $request)
1106
    {
1107 3
        $cartService = $app['eccube.service.cart'];
1108
1109
        // カートチェック
1110 3
        if (!$cartService->isLocked()) {
1111
            // カートが存在しない、カートがロックされていない時はエラー
1112
            log_info('カートが存在しません');
1113
            return $app->redirect($app->url('cart'));
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
1114
        }
1115
1116
        // カートチェック
1117 3
        if (count($cartService->getCart()->getCartItems()) <= 0) {
1118
            // カートが存在しない時はエラー
1119
            log_info('カートに商品が入っていないためショッピングカート画面にリダイレクト');
1120
            return $app->redirect($app->url('cart'));
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
1121
        }
1122
1123
        /** @var \Eccube\Entity\Order $Order */
1124 3
        $Order = $app['eccube.service.shopping']->getOrder($app['config']['order_processing']);
1125 3
        if (!$Order) {
1126
            log_info('購入処理中の受注情報がないため購入エラー');
1127
            $app->addError('front.shopping.order.error');
1128
            return $app->redirect($app->url('shopping_error'));
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
1129
        }
1130
1131
        // 複数配送時は商品毎でお届け先を設定する為、商品をまとめた数量を設定
1132 3
        $compItemQuantities = array();
1133 3 View Code Duplication
        foreach ($Order->getShippings() as $Shipping) {
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...
1134 3
            foreach ($Shipping->getShipmentItems() as $ShipmentItem) {
1135 3
                $itemId = $ShipmentItem->getProductClass()->getId();
1136 3
                $quantity = $ShipmentItem->getQuantity();
1137 3
                if (array_key_exists($itemId, $compItemQuantities)) {
1138
                    $compItemQuantities[$itemId] = $compItemQuantities[$itemId] + $quantity;
1139
                } else {
1140 3
                    $compItemQuantities[$itemId] = $quantity;
1141
                }
1142
            }
1143
        }
1144
1145
        // 商品に紐づく商品情報を取得
1146 3
        $shipmentItems = array();
1147 3
        $productClassIds = array();
1148 3
        foreach ($Order->getShippings() as $Shipping) {
1149 3
            foreach ($Shipping->getShipmentItems() as $ShipmentItem) {
1150 3
                if (!in_array($ShipmentItem->getProductClass()->getId(), $productClassIds)) {
1151 3
                    $shipmentItems[] = $ShipmentItem;
1152
                }
1153 3
                $productClassIds[] = $ShipmentItem->getProductClass()->getId();
1154
            }
1155
        }
1156
1157 3
        $builder = $app->form();
1158
        $builder
1159 3
            ->add('shipping_multiple', 'collection', array(
1160 3
                'type' => 'shipping_multiple',
1161 3
                'data' => $shipmentItems,
1162
                'allow_add' => true,
1163
                'allow_delete' => true,
1164
            ));
1165
1166 3
        $event = new EventArgs(
1167
            array(
1168 3
                'builder' => $builder,
1169 3
                'Order' => $Order,
1170
            ),
1171
            $request
1172
        );
1173 3
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_SHOPPING_SHIPPING_MULTIPLE_INITIALIZE, $event);
1174
1175 3
        $form = $builder->getForm();
1176
1177 3
        $form->handleRequest($request);
1178
1179 3
        $errors = array();
1180 3
        if ($form->isSubmitted() && $form->isValid()) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
1181
1182 2
            log_info('複数配送設定処理開始', array($Order->getId()));
1183 2
            $data = $form['shipping_multiple'];
1184
1185
            // 数量が超えていないか、同一でないとエラー
1186 2
            $itemQuantities = array();
1187 2
            foreach ($data as $mulitples) {
1188
                /** @var \Eccube\Entity\ShipmentItem $multipleItem */
1189 2
                $multipleItem = $mulitples->getData();
1190 2 View Code Duplication
                foreach ($mulitples as $items) {
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...
1191 2
                    foreach ($items as $item) {
1192 2
                        $quantity = $item['quantity']->getData();
1193 2
                        $itemId = $multipleItem->getProductClass()->getId();
1194 2
                        if (array_key_exists($itemId, $itemQuantities)) {
1195 2
                            $itemQuantities[$itemId] = $itemQuantities[$itemId] + $quantity;
1196
                        } else {
1197 2
                            $itemQuantities[$itemId] = $quantity;
1198
                        }
1199
                    }
1200
                }
1201
            }
1202
1203 2
            foreach ($compItemQuantities as $key => $value) {
1204 2
                if (array_key_exists($key, $itemQuantities)) {
1205 2
                    if ($itemQuantities[$key] != $value) {
1206
                        $errors[] = array('message' => $app->trans('shopping.multiple.quantity.diff'));
1207
1208
                        // 対象がなければエラー
1209
                        log_info('複数配送設定入力チェックエラー', array($Order->getId()));
1210
                        return $app->render('Shopping/shipping_multiple.twig', array(
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
1211
                            'form' => $form->createView(),
1212
                            'shipmentItems' => $shipmentItems,
1213
                            'compItemQuantities' => $compItemQuantities,
1214 2
                            'errors' => $errors,
1215
                        ));
1216
                    }
1217
                }
1218
            }
1219
1220
            // お届け先情報をdelete/insert
1221 2
            $shippings = $Order->getShippings();
1222 2
            foreach ($shippings as $Shipping) {
1223 2
                $Order->removeShipping($Shipping);
1224 2
                $app['orm.em']->remove($Shipping);
1225
            }
1226
1227 2
            foreach ($data as $mulitples) {
1228
                /** @var \Eccube\Entity\ShipmentItem $multipleItem */
1229 2
                $multipleItem = $mulitples->getData();
1230
1231 2
                foreach ($mulitples as $items) {
1232 2
                    foreach ($items as $item) {
1233
                        // 追加された配送先情報を作成
1234 2
                        $Delivery = $multipleItem->getShipping()->getDelivery();
1235
1236
                        // 選択された情報を取得
1237 2
                        $data = $item['customer_address']->getData();
1238 2
                        if ($data instanceof CustomerAddress) {
0 ignored issues
show
Bug introduced by
The class Eccube\Entity\CustomerAddress does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
1239
                            // 会員の場合、CustomerAddressオブジェクトを取得
1240 1
                            $CustomerAddress = $data;
1241
                        } else {
1242
                            // 非会員の場合、選択されたindexが取得される
1243 1
                            $customerAddresses = $app['session']->get($this->sessionCustomerAddressKey);
1244 1
                            $customerAddresses = unserialize($customerAddresses);
1245 1
                            $CustomerAddress = $customerAddresses[$data];
1246 1
                            $pref = $app['eccube.repository.master.pref']->find($CustomerAddress->getPref()->getId());
1247 1
                            $CustomerAddress->setPref($pref);
1248
                        }
1249
1250 2
                        $Shipping = new Shipping();
1251
                        $Shipping
1252 2
                            ->setFromCustomerAddress($CustomerAddress)
1253 2
                            ->setDelivery($Delivery)
1254 2
                            ->setDelFlg(Constant::DISABLED)
1255 2
                            ->setOrder($Order);
1256 2
                        $app['orm.em']->persist($Shipping);
1257
1258 2
                        $ProductClass = $multipleItem->getProductClass();
1259 2
                        $Product = $multipleItem->getProduct();
1260 2
                        $quantity = $item['quantity']->getData();
1261
1262 2
                        $ShipmentItem = new ShipmentItem();
1263 2
                        $ShipmentItem->setShipping($Shipping)
1264 2
                            ->setOrder($Order)
1265 2
                            ->setProductClass($ProductClass)
1266 2
                            ->setProduct($Product)
1267 2
                            ->setProductName($Product->getName())
1268 2
                            ->setProductCode($ProductClass->getCode())
1269 2
                            ->setPrice($ProductClass->getPrice02())
1270 2
                            ->setQuantity($quantity);
1271
1272 2
                        $ClassCategory1 = $ProductClass->getClassCategory1();
1273 2
                        if (!is_null($ClassCategory1)) {
1274 2
                            $ShipmentItem->setClasscategoryName1($ClassCategory1->getName());
1275 2
                            $ShipmentItem->setClassName1($ClassCategory1->getClassName()->getName());
1276
                        }
1277 2
                        $ClassCategory2 = $ProductClass->getClassCategory2();
1278 2
                        if (!is_null($ClassCategory2)) {
1279 2
                            $ShipmentItem->setClasscategoryName2($ClassCategory2->getName());
1280 2
                            $ShipmentItem->setClassName2($ClassCategory2->getClassName()->getName());
1281
                        }
1282 2
                        $Shipping->addShipmentItem($ShipmentItem);
1283 2
                        $app['orm.em']->persist($ShipmentItem);
1284
1285
                        // 配送料金の設定
1286 2
                        $app['eccube.service.shopping']->setShippingDeliveryFee($Shipping);
1287
1288 2
                        $Order->addShipping($Shipping);
1289
                    }
1290
                }
1291
            }
1292
1293
            // 合計金額の再計算
1294 2
            $Order = $app['eccube.service.shopping']->getAmount($Order);
1295
1296
            // 配送先を更新
1297 2
            $app['orm.em']->flush();
1298
1299 2
            $event = new EventArgs(
1300
                array(
1301 2
                    'form' => $form,
1302 2
                    'Order' => $Order,
1303
                ),
1304
                $request
1305
            );
1306 2
            $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_SHOPPING_SHIPPING_MULTIPLE_COMPLETE, $event);
1307
1308 2
            log_info('複数配送設定処理完了', array($Order->getId()));
1309 2
            return $app->redirect($app->url('shopping'));
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
1310
        }
1311
1312 3
        return $app->render('Shopping/shipping_multiple.twig', array(
1313 3
            'form' => $form->createView(),
1314 3
            'shipmentItems' => $shipmentItems,
1315 3
            'compItemQuantities' => $compItemQuantities,
1316 3
            'errors' => $errors,
1317
        ));
1318
    }
1319
1320
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
introduced by
Doc comment for parameter "$request" missing
Loading history...
1321
     * 非会員用複数配送設定時の新規お届け先の設定
1322
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
1323 1
    public function shippingMultipleEdit(Application $app, Request $request)
1324
    {
1325
        // カートチェック
1326 1
        if (!$app['eccube.service.cart']->isLocked()) {
1327
            log_info('カートが存在しません');
1328
            // カートが存在しない、カートがロックされていない時はエラー
1329
            return $app->redirect($app->url('cart'));
1330
        }
1331
1332
        // 非会員用Customerを取得
1333 1
        $Customer = $app['eccube.service.shopping']->getNonMember($this->sessionKey);
1334 1
        $CustomerAddress = new CustomerAddress();
1335 1
        $CustomerAddress->setCustomer($Customer);
1336 1
        $Customer->addCustomerAddress($CustomerAddress);
1337
1338 1
        $builder = $app['form.factory']->createBuilder('shopping_shipping', $CustomerAddress);
1339
1340 1
        $event = new EventArgs(
1341
            array(
1342 1
                'builder' => $builder,
1343 1
                'Customer' => $Customer,
1344
            ),
1345
            $request
1346
        );
1347 1
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_SHOPPING_SHIPPING_MULTIPLE_EDIT_INITIALIZE, $event);
1348
1349 1
        $form = $builder->getForm();
1350
1351 1
        $form->handleRequest($request);
1352
1353 1
        if ($form->isSubmitted() && $form->isValid()) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
1354
1355 1
            log_info('非会員お届け先追加処理開始');
1356
1357
            // 非会員用のセッションに追加
1358 1
            $customerAddresses = $app['session']->get($this->sessionCustomerAddressKey);
1359 1
            $customerAddresses = unserialize($customerAddresses);
1360 1
            $customerAddresses[] = $CustomerAddress;
1361 1
            $app['session']->set($this->sessionCustomerAddressKey, serialize($customerAddresses));
1362
1363 1
            $event = new EventArgs(
1364
                array(
1365 1
                    'form' => $form,
1366 1
                    'CustomerAddresses' => $customerAddresses,
1367
                ),
1368
                $request
1369
            );
1370 1
            $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_SHOPPING_SHIPPING_MULTIPLE_EDIT_COMPLETE, $event);
1371
1372 1
            log_info('非会員お届け先追加処理完了');
1373
1374 1
            return $app->redirect($app->url('shopping_shipping_multiple'));
1375
        }
1376
1377 1
        return $app->render('Shopping/shipping_multiple_edit.twig', array(
1378 1
            'form' => $form->createView(),
1379
        ));
1380
    }
1381
1382
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
introduced by
Doc comment for parameter "$request" missing
Loading history...
1383
     * 購入エラー画面表示
1384
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
1385 2
    public function shoppingError(Application $app, Request $request)
1386
    {
1387
1388 2
        $event = new EventArgs(
1389 2
            array(),
1390
            $request
1391
        );
1392 2
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_SHOPPING_SHIPPING_ERROR_COMPLETE, $event);
1393
1394 2
        if ($event->getResponse() !== null) {
1395
            return $event->getResponse();
1396
        }
1397
1398 2
        return $app->render('Shopping/shopping_error.twig');
1399
    }
1400
1401
    /**
1402
     * 非会員でのお客様情報変更時の入力チェック
1403
     *
1404
     * @param Application $app
1405
     * @param array $data リクエストパラメータ
1406
     * @return array
1407
     */
1408
    private function customerValidation(Application $app, array $data)
1409
    {
1410
        // 入力チェック
1411
        $errors = array();
1412
1413
        $errors[] = $app['validator']->validateValue($data['customer_name01'], array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
1414
            new Assert\NotBlank(),
1415
            new Assert\Length(array('max' => $app['config']['name_len'],)),
0 ignored issues
show
introduced by
Add a single space after each comma delimiter
Loading history...
1416
            new Assert\Regex(array('pattern' => '/^[^\s ]+$/u', 'message' => 'form.type.name.firstname.nothasspace'))
1417
        ));
1418
1419
        $errors[] = $app['validator']->validateValue($data['customer_name02'], array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
1420
            new Assert\NotBlank(),
1421
            new Assert\Length(array('max' => $app['config']['name_len'],)),
0 ignored issues
show
introduced by
Add a single space after each comma delimiter
Loading history...
1422
            new Assert\Regex(array('pattern' => '/^[^\s ]+$/u', 'message' => 'form.type.name.firstname.nothasspace'))
1423
        ));
1424
1425
        $errors[] = $app['validator']->validateValue($data['customer_company_name'], array(
1426
            new Assert\Length(array('max' => $app['config']['stext_len'])),
1427
        ));
1428
1429
        $errors[] = $app['validator']->validateValue($data['customer_tel01'], array(
1430
            new Assert\NotBlank(),
1431
            new Assert\Type(array('type' => 'numeric', 'message' => 'form.type.numeric.invalid')),
1432
            new Assert\Length(array('max' => $app['config']['tel_len'], 'min' => $app['config']['tel_len_min'])),
1433
        ));
1434
1435
        $errors[] = $app['validator']->validateValue($data['customer_tel02'], array(
1436
            new Assert\NotBlank(),
1437
            new Assert\Type(array('type' => 'numeric', 'message' => 'form.type.numeric.invalid')),
1438
            new Assert\Length(array('max' => $app['config']['tel_len'], 'min' => $app['config']['tel_len_min'])),
1439
        ));
1440
1441
        $errors[] = $app['validator']->validateValue($data['customer_tel03'], array(
1442
            new Assert\NotBlank(),
1443
            new Assert\Type(array('type' => 'numeric', 'message' => 'form.type.numeric.invalid')),
1444
            new Assert\Length(array('max' => $app['config']['tel_len'], 'min' => $app['config']['tel_len_min'])),
1445
        ));
1446
1447
        $errors[] = $app['validator']->validateValue($data['customer_zip01'], array(
1448
            new Assert\NotBlank(),
1449
            new Assert\Type(array('type' => 'numeric', 'message' => 'form.type.numeric.invalid')),
1450
            new Assert\Length(array('min' => $app['config']['zip01_len'], 'max' => $app['config']['zip01_len'])),
1451
        ));
1452
1453
        $errors[] = $app['validator']->validateValue($data['customer_zip02'], array(
1454
            new Assert\NotBlank(),
1455
            new Assert\Type(array('type' => 'numeric', 'message' => 'form.type.numeric.invalid')),
1456
            new Assert\Length(array('min' => $app['config']['zip02_len'], 'max' => $app['config']['zip02_len'])),
1457
        ));
1458
1459
        $errors[] = $app['validator']->validateValue($data['customer_addr01'], array(
1460
            new Assert\NotBlank(),
1461
            new Assert\Length(array('max' => $app['config']['address1_len'])),
1462
        ));
1463
1464
        $errors[] = $app['validator']->validateValue($data['customer_addr02'], array(
1465
            new Assert\NotBlank(),
1466
            new Assert\Length(array('max' => $app['config']['address2_len'])),
1467
        ));
1468
1469
        $errors[] = $app['validator']->validateValue($data['customer_email'], array(
1470
            new Assert\NotBlank(),
1471
            new Assert\Email(array('strict' => true)),
1472
        ));
1473
1474
        return $errors;
1475
    }
1476
}
1477