Completed
Push — experimental/sf ( 8d4b59...028691 )
by
unknown
158:31 queued 151:02
created

CartController::index()   B

Complexity

Conditions 7
Paths 10

Size

Total Lines 49

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 7.4822

Importance

Changes 0
Metric Value
cc 7
nc 10
nop 1
dl 0
loc 49
ccs 22
cts 28
cp 0.7856
crap 7.4822
rs 8.1793
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of EC-CUBE
5
 *
6
 * Copyright(c) LOCKON CO.,LTD. All Rights Reserved.
7
 *
8
 * http://www.lockon.co.jp/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Eccube\Controller;
15
16
use Eccube\Entity\Cart;
17
use Eccube\Entity\ProductClass;
18
use Eccube\Event\EccubeEvents;
19
use Eccube\Event\EventArgs;
20
use Eccube\Repository\ProductClassRepository;
21
use Eccube\Service\CartService;
22
use Eccube\Service\PurchaseFlow\PurchaseContext;
23
use Eccube\Service\PurchaseFlow\PurchaseFlow;
24
use Eccube\Service\PurchaseFlow\PurchaseFlowResult;
25
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
26
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
27
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
28
use Symfony\Component\HttpFoundation\Request;
29
use Eccube\Entity\BaseInfo;
30
31
class CartController extends AbstractController
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
32
{
33
    /**
34
     * @var ProductClassRepository
35
     */
36
    protected $productClassRepository;
37
38
    /**
39
     * @var CartService
40
     */
41
    protected $cartService;
42
43
    /**
44
     * @var PurchaseFlow
45
     */
46
    protected $purchaseFlow;
47
48
    /**
49
     * @var BaseInfo
50
     */
51
    protected $baseInfo;
52
53
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$baseInfo" missing
Loading history...
54
     * CartController constructor.
55
     *
56
     * @param ProductClassRepository $productClassRepository
57
     * @param CartService $cartService
0 ignored issues
show
introduced by
Expected 12 spaces after parameter type; 1 found
Loading history...
58
     * @param PurchaseFlow $cartPurchaseFlow
0 ignored issues
show
introduced by
Expected 11 spaces after parameter type; 1 found
Loading history...
59
     * @param BaseInfo $BaseInfo
0 ignored issues
show
Documentation introduced by
There is no parameter named $BaseInfo. Did you maybe mean $baseInfo?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
introduced by
Expected 15 spaces after parameter type; 1 found
Loading history...
introduced by
Doc comment for parameter $BaseInfo does not match case of actual variable name $baseInfo
Loading history...
60
     */
61 77
    public function __construct(
62
        ProductClassRepository $productClassRepository,
63
        CartService $cartService,
64
        PurchaseFlow $cartPurchaseFlow,
65
        BaseInfo $baseInfo
66
    ) {
67 77
        $this->productClassRepository = $productClassRepository;
68 77
        $this->cartService = $cartService;
69 77
        $this->purchaseFlow = $cartPurchaseFlow;
70 77
        $this->baseInfo = $baseInfo;
71
    }
72
73
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$request" missing
Loading history...
74
     * カート画面.
75
     *
76
     * @Route("/cart", name="cart")
77
     * @Template("Cart/index.twig")
78
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
79 36
    public function index(Request $request)
80
    {
81
        // カートを取得して明細の正規化を実行
82 36
        $Carts = $this->cartService->getCarts();
83 36
        $this->execPurchaseFlow($Carts);
84
85
        // TODO itemHolderから取得できるように
86 36
        $least = [];
87 36
        $quantity = [];
88 36
        $isDeliveryFree = [];
89 36
        $totalPrice = 0;
90 36
        $totalQuantity = 0;
91
92 36
        foreach ($Carts as $Cart) {
93 35
            $quantity[$Cart->getCartKey()] = 0;
94 35
            $isDeliveryFree[$Cart->getCartKey()] = false;
95
96 35
            if ($this->baseInfo->getDeliveryFreeQuantity()) {
97
                if ($this->baseInfo->getDeliveryFreeQuantity() > $Cart->getQuantity()) {
98
                    $quantity[$Cart->getCartKey()] = $this->baseInfo->getDeliveryFreeQuantity() - $Cart->getQuantity();
99
                } else {
100
                    $isDeliveryFree[$Cart->getCartKey()] = true;
101
                }
102
            }
103
104 35
            if ($this->baseInfo->getDeliveryFreeAmount()) {
105
                if (!$isDeliveryFree[$Cart->getCartKey()] && $this->baseInfo->getDeliveryFreeAmount() <= $Cart->getTotalPrice()) {
106
                    $isDeliveryFree[$Cart->getCartKey()] = true;
107
                } else {
108
                    $least[$Cart->getCartKey()] = $this->baseInfo->getDeliveryFreeAmount() - $Cart->getTotalPrice();
109
                }
110
            }
111
112 35
            $totalPrice += $Cart->getTotalPrice();
113 35
            $totalQuantity += $Cart->getQuantity();
114
        }
115
116
        // カートが分割された時のセッション情報を削除
117 36
        $request->getSession()->remove('cart.divide');
118
119
        return [
120 36
            'totalPrice' => $totalPrice,
121 36
            'totalQuantity' => $totalQuantity,
122 36
            'Carts' => $Carts,
123 36
            'least' => $least,
124 36
            'quantity' => $quantity,
125 36
            'is_delivery_free' => $isDeliveryFree,
126
        ];
127
    }
128
129
    /**
130
     * @param $Carts
131
     *
132
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
133
     */
134
    protected function execPurchaseFlow($Carts)
135
    {
136
        /** @var PurchaseFlowResult[] $flowResults */
137 77
        $flowResults = array_map(function ($Cart) {
138 75
            $purchaseContext = new PurchaseContext($Cart, $this->getUser());
139
140 75
            return $this->purchaseFlow->validate($Cart, $purchaseContext);
141 77
        }, $Carts);
142
143
        // 復旧不可のエラーが発生した場合はカートをクリアして再描画
144 77
        $hasError = false;
145 77
        foreach ($flowResults as $result) {
146 75
            if ($result->hasError()) {
147 1
                $hasError = true;
148 1
                foreach ($result->getErrors() as $error) {
149 75
                    $this->addRequestError($error->getMessage());
150
                }
151
            }
152
        }
153 77
        if ($hasError) {
154 1
            $this->cartService->clear();
155
156 1
            return $this->redirectToRoute('cart');
157
        }
158
159 76
        $this->cartService->save();
160
161 76
        foreach ($flowResults as $index => $result) {
162 74
            foreach ($result->getWarning() as $warning) {
163 74
                $this->addRequestError($warning->getMessage(), "front.cart.${index}");
164
            }
165
        }
166
    }
167
168
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$operation" missing
Loading history...
introduced by
Doc comment for parameter "$productClassId" missing
Loading history...
169
     * カート明細の加算/減算/削除を行う.
170
     *
171
     * - 加算
172
     *      - 明細の個数を1増やす
173
     * - 減算
174
     *      - 明細の個数を1減らす
175
     *      - 個数が0になる場合は、明細を削除する
176
     * - 削除
177
     *      - 明細を削除する
178
     *
179
     * @Method("PUT")
180
     * @Route(
181
     *     path="/cart/{operation}/{productClassId}",
182
     *     name="cart_handle_item",
183
     *     requirements={
184
     *          "operation": "up|down|remove",
185
     *          "productClassId": "\d+"
186
     *     }
187
     * )
188
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
189
    public function handleCartItem($operation, $productClassId)
0 ignored issues
show
introduced by
Declare public methods first, then protected ones and finally private ones
Loading history...
190
    {
191 51
        log_info('カート明細操作開始', ['operation' => $operation, 'product_class_id' => $productClassId]);
192
193 51
        $this->isTokenValid();
194
195
        /** @var ProductClass $ProductClass */
196 51
        $ProductClass = $this->productClassRepository->find($productClassId);
197
198 51
        if (is_null($ProductClass)) {
199
            log_info('商品が存在しないため、カート画面へredirect', ['operation' => $operation, 'product_class_id' => $productClassId]);
200
201
            return $this->redirectToRoute('cart');
202
        }
203
204
        // 明細の増減・削除
205 51
        switch ($operation) {
206
            case 'up':
207 44
                $this->cartService->addProduct($ProductClass, 1);
208 44
                break;
209
            case 'down':
210 6
                $this->cartService->addProduct($ProductClass, -1);
211 6
                break;
212
            case 'remove':
213 1
                $this->cartService->removeProduct($ProductClass);
214 1
                break;
215
        }
216
217
        // カートを取得して明細の正規化を実行
218 51
        $Carts = $this->cartService->getCarts();
219 51
        $this->execPurchaseFlow($Carts);
220
221 51
        log_info('カート演算処理終了', ['operation' => $operation, 'product_class_id' => $productClassId]);
222
223 51
        return $this->redirectToRoute('cart');
224
    }
225
226
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$request" missing
Loading history...
introduced by
Doc comment for parameter "$index" missing
Loading history...
227
     * カートをロック状態に設定し、購入確認画面へ遷移する.
228
     *
229
     * @Route("/cart/buystep/{index}", name="cart_buystep", requirements={"index" = "\d+"}, defaults={"index" = 0})
230
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
231
    public function buystep(Request $request, $index)
232
    {
233 53
        $Carts = $this->cartService->getCart();
234 53
        if (!is_object($Carts)) {
235
            return $this->redirectToRoute('cart');
236
        }
237
        // FRONT_CART_BUYSTEP_INITIALIZE
238 53
        $event = new EventArgs(
239 53
            [],
240 53
            $request
241
        );
242 53
        $this->eventDispatcher->dispatch(EccubeEvents::FRONT_CART_BUYSTEP_INITIALIZE, $event);
243
244 53
        $this->cartService->setPrimary($index);
245 53
        $this->cartService->save();
246
247
        // FRONT_CART_BUYSTEP_COMPLETE
248 53
        $event = new EventArgs(
249 53
            [],
250 53
            $request
251
        );
252 53
        $this->eventDispatcher->dispatch(EccubeEvents::FRONT_CART_BUYSTEP_COMPLETE, $event);
253
254 53
        if ($event->hasResponse()) {
255
            return $event->getResponse();
256
        }
257
258 53
        return $this->redirectToRoute('shopping');
259
    }
260
}
261