Failed Conditions
Push — experimental/3.1 ( 965511...751c7a )
by chihiro
21s
created

CartController::addTestProduct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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