Failed Conditions
Pull Request — experimental/3.1 (#2641)
by Kiyotaka
105:29 queued 74:48
created

CartController::execPurchaseFlow()   C

Complexity

Conditions 7
Paths 16

Size

Total Lines 31
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 7

Importance

Changes 0
Metric Value
cc 7
eloc 18
nc 16
nop 2
dl 0
loc 31
ccs 4
cts 4
cp 1
crap 7
rs 6.7272
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\Inject;
28
use Eccube\Application;
29
use Eccube\Entity\Cart;
30
use Eccube\Entity\ProductClass;
31
use Eccube\Event\EccubeEvents;
32
use Eccube\Event\EventArgs;
33
use Eccube\Repository\ProductClassRepository;
34
use Eccube\Service\CartService;
35
use Eccube\Service\PurchaseFlow\PurchaseFlow;
36
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
37
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
38
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
39
use Symfony\Component\EventDispatcher\EventDispatcher;
40
use Symfony\Component\HttpFoundation\Request;
41
42
/**
43
 * @Route(service=CartController::class)
44
 */
45
class CartController extends AbstractController
46
{
47
    /**
48
     * @Inject("eccube.event.dispatcher")
49
     * @var EventDispatcher
50
     */
51
    protected $eventDispatcher;
52
53
    /**
54
     * @Inject(ProductClassRepository::class)
55
     * @var ProductClassRepository
56
     */
57
    protected $productClassRepository;
58
59
    /**
60
     * @Inject("eccube.purchase.flow.cart")
61
     * @var PurchaseFlow
62
     */
63
    protected $purchaseFlow;
64
65
    /**
66
     * @Inject(CartService::class)
67
     * @var CartService
68
     */
69
    protected $cartService;
70
71
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
introduced by
Doc comment for parameter "$request" missing
Loading history...
72
     * カート画面.
73
     *
74
     * @Route("/cart", name="cart")
75
     * @Template("Cart/index.twig")
76
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
77 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...
78
    {
79
        // カートを取得して明細の正規化を実行
80 1
        $Carts = $this->cartService->getCarts();
81
82 1
        $this->execPurchaseFlow($app, $Carts);
83
84
        // TODO itemHolderから取得できるように
85 1
        $least = 0;
86
        $quantity = 0;
87
        $isDeliveryFree = false;
88
89
        $totalQuantity = array_reduce($Carts, function($total, $Cart) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
90
            /** @var Cart $Cart */
91
            $total += $Cart->getQuantity();
92
            return $total;
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
93
        }, 0);
94
        $totalPrice = array_reduce($Carts, function($total, $Cart) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
95 1
            /** @var Cart $Cart */
96
            $total += $Cart->getTotalPrice();
97 1
            return $total;
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
98
        }, 0);
99
100
        return [
101
            'totalPrice' => $totalPrice,
102 1
            'totalQuantity' => $totalQuantity,
103 1
            'Carts' => $Carts,
104 1
            'least' => $least,
105
            'quantity' => $quantity,
106
            'is_delivery_free' => $isDeliveryFree,
107 1
        ];
108 1
    }
109 1
110 1
    protected function execPurchaseFlow(Application $app, $Carts)
111
    {
112
        $flowResults = array_map(function($Cart) use ($app) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
113
            return $this->purchaseFlow->calculate($Cart, $app['eccube.purchase.context']());
114
        }, $Carts);
115
116
117
        // 復旧不可のエラーが発生した場合はカートをクリアして再描画
118
        $hasError = false;
119
        foreach ($flowResults as $result) {
120
            if ($result->hasError()) {
121
                $hasError = true;
122
                foreach ($result->getErrors() as $error) {
123
                    $app->addRequestError($error->getMessage());
124
                }
125
            }
126
        }
127
        if ($hasError) {
128
            $this->cartService->clear();
129
            $this->cartService->save();
130
            return $app->redirect($app->url('cart'));
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
131
        }
132
133
        $this->cartService->save();
134
135 16
        foreach ($flowResults as $index=>$result) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space before "=>"; 0 found
Loading history...
Coding Style introduced by
Expected 1 space after "=>"; 0 found
Loading history...
136
            foreach ($result->getWarning() as $warning) {
137 16
                $app->addRequestError($warning->getMessage(), "front.cart.${index}");
138
            }
139 16
        }
140
    }
141
142 16
    /**
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 "$productClassId" missing
Loading history...
143
     * カート明細の加算/減算/削除を行う.
144 16
     *
145
     * - 加算
146
     *      - 明細の個数を1増やす
147
     * - 減算
148
     *      - 明細の個数を1減らす
149
     *      - 個数が0になる場合は、明細を削除する
150
     * - 削除
151
     *      - 明細を削除する
152 16
     *
153 14
     * @Method("PUT")
154 14
     * @Route(
155 2
     *     path="/cart/{operation}/{productClassId}",
156 1
     *     name="cart_handle_item",
157 1
     *     requirements={
158 1
     *          "operation": "up|down|remove",
159 1
     *          "productClassId": "\d+"
160 1
     *     }
161
     * )
162
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
163
    public function handleCartItem(Application $app, Request $request, $operation, $productClassId)
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...
introduced by
Declare public methods first, then protected ones and finally private ones
Loading history...
164 16
    {
165
        log_info('カート明細操作開始', ['operation' => $operation, 'product_class_id' => $productClassId]);
166 16
167
        $this->isTokenValid($app);
168
169 16
        /** @var ProductClass $ProductClass */
170 1
        $ProductClass = $this->productClassRepository->find($productClassId);
171 1
172
        if (is_null($ProductClass)) {
173 1
            log_info('商品が存在しないため、カート画面へredirect', ['operation' => $operation, 'product_class_id' => $productClassId]);
174 1
175
            return $app->redirect($app->url('cart'));
176 1
        }
177
178
        // 明細の増減・削除
179 15
        switch ($operation) {
180
            case 'up':
181 15
                $this->cartService->addProduct($ProductClass, 1);
182
                break;
183
            case 'down':
184
                $this->cartService->addProduct($ProductClass, -1);
185 15
                break;
186
            case 'remove':
187 15
                $this->cartService->removeProduct($ProductClass);
188
                break;
189
        }
190
191
        // カートを取得して明細の正規化を実行
192
        $Carts = $this->cartService->getCarts();
193
        $this->execPurchaseFlow($app, $Carts);
194
195
        log_info('カート演算処理終了', ['operation' => $operation, 'product_class_id' => $productClassId]);
196
197
        return $app->redirect($app->url('cart'));
198
    }
199
200
    /**
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 "$index" missing
Loading history...
201
     * カートをロック状態に設定し、購入確認画面へ遷移する.
202
     *
203
     * @Route("/cart/buystep/{index}", name="cart_buystep", requirements={"index" = "\d+"}, defaults={"index" = 0})
204
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
205
    public function buystep(Application $app, Request $request, $index)
206
    {
207
        // FRONT_CART_BUYSTEP_INITIALIZE
208
        $event = new EventArgs(
209
            array(),
210
            $request
211
        );
212
        $this->eventDispatcher->dispatch(EccubeEvents::FRONT_CART_BUYSTEP_INITIALIZE, $event);
213
214
        $this->cartService->setPrimary($index);
215
        $this->cartService->lock();
216
        $this->cartService->save();
217
218
        // FRONT_CART_BUYSTEP_COMPLETE
219
        $event = new EventArgs(
220
            array(),
221
            $request
222
        );
223
        $this->eventDispatcher->dispatch(EccubeEvents::FRONT_CART_BUYSTEP_COMPLETE, $event);
224
225
        if ($event->hasResponse()) {
226
            return $event->getResponse();
227
        }
228
229
        return $app->redirect($app->url('shopping'));
230
    }
231
}
232