Failed Conditions
Pull Request — experimental/3.1 (#2486)
by
unknown
92:49 queued 86:04
created

CartController::index()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 36
Code Lines 20

Duplication

Lines 9
Ratio 25 %

Code Coverage

Tests 13
CRAP Score 4.5039

Importance

Changes 0
Metric Value
cc 4
eloc 20
nc 4
nop 2
dl 9
loc 36
ccs 13
cts 19
cp 0.6842
crap 4.5039
rs 8.5806
c 0
b 0
f 0
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\Annotation\Component;
28
use Eccube\Annotation\Inject;
29
use Eccube\Application;
30
use Eccube\Entity\CartItem;
31
use Eccube\Entity\ProductClass;
32
use Eccube\Event\EccubeEvents;
33
use Eccube\Event\EventArgs;
34
use Eccube\Repository\ProductClassRepository;
35
use Eccube\Service\CartService;
36
use Eccube\Service\PurchaseFlow\PurchaseFlow;
37
use Eccube\Service\PurchaseFlow\PurchaseFlowResult;
38
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
39
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
40
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
41
use Symfony\Component\EventDispatcher\EventDispatcher;
42
use Symfony\Component\HttpFoundation\Request;
43
44
/**
45
 * @Component
46
 * @Route(service=CartController::class)
47
 */
48
class CartController extends AbstractController
49
{
50
    /**
51
     * @Inject("eccube.event.dispatcher")
52
     * @var EventDispatcher
53
     */
54
    protected $eventDispatcher;
55
56
    /**
57
     * @Inject(ProductClassRepository::class)
58
     * @var ProductClassRepository
59
     */
60
    protected $productClassRepository;
61
62
    /**
63
     * @Inject("eccube.purchase.flow.cart")
64
     * @var PurchaseFlow
65
     */
66
    protected $purchaseFlow;
67
68
    /**
69
     * @Inject(CartService::class)
70
     * @var CartService
71
     */
72
    protected $cartService;
73
74
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
introduced by
Doc comment for parameter "$request" missing
Loading history...
75
     * カート画面.
76
     *
77
     * @Route("/cart", name="cart")
78
     * @Template("Cart/index.twig")
79
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
80 1
    public function index(Application $app, Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
81
    {
82
        // カートを取得して明細の正規化を実行
83 1
        $Cart = $this->cartService->getCart();
84
        /** @var PurchaseFlowResult $result */
85 1
        $result = $this->purchaseFlow->calculate($Cart, $app['eccube.purchase.context']());
86
87
        // 復旧不可のエラーが発生した場合はカートをクリアして再描画
88 1 View Code Duplication
        if ($result->hasError()) {
89
            foreach ($result->getErrors() as $error) {
90
                $app->addRequestError($error->getMessage());
91
            }
92
            $this->cartService->clear();
93
            $this->cartService->save();
94
95
            return $app->redirect($app->url('cart'));
96
        }
97
98 1
        $this->cartService->save();
99
100 1
        foreach ($result->getWarning() as $warning) {
101
            $app->addRequestError($warning->getMessage());
102
        }
103
104
        // TODO itemHolderから取得できるように
105 1
        $least = 0;
106 1
        $quantity = 0;
107 1
        $isDeliveryFree = false;
108
109
        return [
110 1
            'Cart' => $Cart,
111 1
            'least' => $least,
112 1
            'quantity' => $quantity,
113 1
            'is_delivery_free' => $isDeliveryFree,
114
        ];
115
    }
116
117
    /**
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 "$operation" missing
Loading history...
introduced by
Doc comment for parameter "$cartNo" missing
Loading history...
118
     * カート明細の加算/減算/削除を行う.
119
     *
120
     * - 加算
121
     *      - 明細の個数を1増やす
122
     * - 減算
123
     *      - 明細の個数を1減らす
124
     *      - 個数が0になる場合は、明細を削除する
125
     * - 削除
126
     *      - 明細を削除する
127
     *
128
     * @Method("PUT")
129
     * @Route(
130
     *     path="/cart/{operation}/{cartNo}",
131
     *     name="cart_handle_item",
132
     *     requirements={
133
     *          "operation": "up|down|remove",
134
     *          "cartNo": "\d+"
135
     *     }
136
     * )
137
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
138 3
    public function handleCartItem(Application $app, Request $request, $operation, $cartNo)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
139
    {
140 3
        log_info('カート明細操作開始', ['operation' => $operation, 'cart_no' => $cartNo]);
141
142 3
        $this->isTokenValid($app);
143
144
        /** @var CartItem $CartItem */
145 3
        $CartItem = $this->cartService->getCartItem($cartNo);
146
147 3
        if (is_null($CartItem)) {
148 3
            log_info('商品が存在しないため、カート画面へredirect', ['operation' => $operation, 'cart_no' => $cartNo]);
149
150 3
            return $app->redirect($app->url('cart'));
151
        }
152
153
        // 明細の増減・削除
154
        switch ($operation) {
155
            case 'up':
156
                $this->cartService->addQuantity($cartNo, 1);
157
                break;
158
            case 'down':
159
                $this->cartService->addQuantity($cartNo, -1);
160
                break;
161
            case 'remove':
162
                $this->cartService->removeProduct($cartNo);
163
                break;
164
        }
165
166
        // カートを取得して明細の正規化を実行
167
        $Cart = $this->cartService->getCart();
168
        /** @var PurchaseFlowResult $result */
169
        $result = $this->purchaseFlow->calculate($Cart, $app['eccube.purchase.context']());
170
171
        // 復旧不可のエラーが発生した場合はカートをクリアしてカート一覧へ
172 View Code Duplication
        if ($result->hasError()) {
173
            foreach ($result->getErrors() as $error) {
174
                $app->addRequestError($error->getMessage());
175
            }
176
            $this->cartService->clear();
177
            $this->cartService->save();
178
179
            return $app->redirect($app->url('cart'));
180
        }
181
182
        $this->cartService->save();
183
184
        foreach ($result->getWarning() as $warning) {
185
            $app->addRequestError($warning->getMessage());
186
        }
187
188
        log_info('カート演算処理終了', ['operation' => $operation, 'cart_no' => $cartNo]);
189
190
        return $app->redirect($app->url('cart'));
191
    }
192
193
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
introduced by
Doc comment for parameter "$request" missing
Loading history...
194
     * カートをロック状態に設定し、購入確認画面へ遷移する.
195
     *
196
     * @Route("/cart/buystep", name="cart_buystep")
197
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
198
    public function buystep(Application $app, Request $request)
199
    {
200
        // FRONT_CART_BUYSTEP_INITIALIZE
201
        $event = new EventArgs(
202
            array(),
203
            $request
204
        );
205
        $this->eventDispatcher->dispatch(EccubeEvents::FRONT_CART_BUYSTEP_INITIALIZE, $event);
206
207
        $this->cartService->lock();
208
        $this->cartService->save();
209
210
        // FRONT_CART_BUYSTEP_COMPLETE
211
        $event = new EventArgs(
212
            array(),
213
            $request
214
        );
215
        $this->eventDispatcher->dispatch(EccubeEvents::FRONT_CART_BUYSTEP_COMPLETE, $event);
216
217
        if ($event->hasResponse()) {
218
            return $event->getResponse();
219
        }
220
221
        return $app->redirect($app->url('shopping'));
222
    }
223
}
224