Failed Conditions
Push — master ( dbea68...9459a9 )
by Kentaro
01:14
created

CartController::index()   B

Complexity

Conditions 6
Paths 18

Size

Total Lines 57
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 6.5625

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 57
ccs 21
cts 28
cp 0.75
rs 8.7433
cc 6
eloc 33
nc 18
nop 2
crap 6.5625

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Event\EccubeEvents;
29
use Eccube\Event\EventArgs;
30
use Eccube\Exception\CartException;
31
use Symfony\Component\HttpFoundation\Request;
32
33
class CartController extends AbstractController
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
34
{
35
    /**
36
     * カート画面.
37
     *
38
     * @param Application $app
39
     * @param Request $request
0 ignored issues
show
introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
40
     * @return \Symfony\Component\HttpFoundation\Response
41
     */
42 2
    public function index(Application $app, Request $request)
43
    {
44 2
        $Cart = $app['eccube.service.cart']->getCart();
45
46
        // FRONT_CART_INDEX_INITIALIZE
47 2
        $event = new EventArgs(
48 2
            array(),
49
            $request
50
        );
51 2
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_CART_INDEX_INITIALIZE, $event);
52
53
        /* @var $BaseInfo \Eccube\Entity\BaseInfo */
54
        /* @var $Cart \Eccube\Entity\Cart */
55 2
        $BaseInfo = $app['eccube.repository.base_info']->get();
56
57 2
        $isDeliveryFree = false;
58 2
        $least = 0;
59 2
        $quantity = 0;
60 2
        if ($BaseInfo->getDeliveryFreeAmount()) {
61
            if ($BaseInfo->getDeliveryFreeAmount() <= $Cart->getTotalPrice()) {
62
                // 送料無料(金額)を超えている
63
                $isDeliveryFree = true;
64
            } else {
65
                $least = $BaseInfo->getDeliveryFreeAmount() - $Cart->getTotalPrice();
66
            }
67
        }
68
69 2
        if ($BaseInfo->getDeliveryFreeQuantity()) {
70
            if ($BaseInfo->getDeliveryFreeQuantity() <= $Cart->getTotalQuantity()) {
71
                // 送料無料(個数)を超えている
72
                $isDeliveryFree = true;
73
            } else {
74
                $quantity = $BaseInfo->getDeliveryFreeQuantity() - $Cart->getTotalQuantity();
75
            }
76
        }
77
78
        // FRONT_CART_INDEX_COMPLETE
79 2
        $event = new EventArgs(
80 2
            array(),
81
            $request
82
        );
83 2
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_CART_INDEX_COMPLETE, $event);
84
85 2
        if ($event->hasResponse()) {
86
            return $event->getResponse();
87
        }
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
    /**
101
     * カートに商品を追加する.
102
     *
103
     * @param Application $app
104
     * @param Request $request
0 ignored issues
show
introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
105
     * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
106
     */
107 34
    public function add(Application $app, Request $request)
108
    {
109 34
        $productClassId = $request->get('product_class_id');
110 34
        $quantity = $request->request->has('quantity') ? $request->get('quantity') : 1;
111
112
        // FRONT_CART_ADD_INITIALIZE
113 34
        $event = new EventArgs(
114
            array(
115 34
                'productClassId' => $productClassId,
116 34
                'quantity' => $quantity,
117
            ),
118
            $request
119
        );
120 34
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_CART_ADD_INITIALIZE, $event);
121
122
        try {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
123
124 34
            $productClassId = $event->getArgument('productClassId');
125 34
            $quantity = $event->getArgument('quantity');
126
127 34
            $app['eccube.service.cart']->addProduct($productClassId, $quantity)->save();
128
129
            // FRONT_CART_ADD_COMPLETE
130 33
            $event = new EventArgs(
131
                array(
132 33
                    'productClassId' => $productClassId,
133 33
                    'quantity' => $quantity,
134
                ),
135
                $request
136
            );
137 33
            $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_CART_ADD_COMPLETE, $event);
138
139 33
            if ($event->hasResponse()) {
140 33
                return $event->getResponse();
141
            }
142
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
143 1
        } catch (CartException $e) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
144
145
            // FRONT_CART_ADD_EXCEPTION
146 1
            $event = new EventArgs(
147
                array(
148 1
                    'exception' => $e,
149
                ),
150
                $request
151
            );
152 1
            $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_CART_ADD_EXCEPTION, $event);
153
154 1
            if ($event->hasResponse()) {
155
                return $event->getResponse();
156
            }
157
158 1
            $app->addRequestError($e->getMessage());
159
        }
160
161 34
        return $app->redirect($app->url('cart'));
162
    }
163
164
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$productClassId" missing
Loading history...
165
     * カートに入っている商品の個数を1増やす.
166
     *
167
     * @param Application $app
0 ignored issues
show
introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
168
     * @param Request $request
0 ignored issues
show
introduced by
Expected 8 spaces after parameter type; 1 found
Loading history...
169
     * @param $productClassId
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
170
     * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
171
     */
172 3 View Code Duplication
    public function up(Application $app, Request $request, $productClassId)
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...
173
    {
174 3
        $this->isTokenValid($app);
175
176
        // FRONT_CART_UP_INITIALIZE
177 3
        $event = new EventArgs(
178
            array(
179 3
                'productClassId' => $productClassId,
180
            ),
181
            $request
182
        );
183 3
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_CART_UP_INITIALIZE, $event);
184
185
        try {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
186
187 3
            $productClassId = $event->getArgument('productClassId');
188
189 3
            $app['eccube.service.cart']->upProductQuantity($productClassId)->save();
190
191
            // FRONT_CART_UP_COMPLETE
192 2
            $event = new EventArgs(
193
                array(
194 2
                    'productClassId' => $productClassId,
195
                ),
196
                $request
197
            );
198 2
            $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_CART_UP_COMPLETE, $event);
199
200 2
            if ($event->hasResponse()) {
201 2
                return $event->getResponse();
202
            }
203
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
204 1
        } catch (CartException $e) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
205
206
            // FRONT_CART_UP_EXCEPTION
207 1
            $event = new EventArgs(
208
                array(
209 1
                    'exception' => $e,
210
                ),
211
                $request
212
            );
213 1
            $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_CART_UP_EXCEPTION, $event);
214
215 1
            if ($event->hasResponse()) {
216
                return $event->getResponse();
217
            }
218
219 1
            $app->addRequestError($e->getMessage());
220
        }
221
222 3
        return $app->redirect($app->url('cart'));
223
    }
224
225
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$productClassId" missing
Loading history...
226
     * カートに入っている商品の個数を1減らす.
227
     * マイナスになる場合は, 商品をカートから削除する.
228
     *
229
     * @param Application $app
0 ignored issues
show
introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
230
     * @param Request $request
0 ignored issues
show
introduced by
Expected 8 spaces after parameter type; 1 found
Loading history...
231
     * @param $productClassId
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
232
     * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
233
     */
234 3 View Code Duplication
    public function down(Application $app, Request $request, $productClassId)
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...
235
    {
236 3
        $this->isTokenValid($app);
237
238
        // FRONT_CART_DOWN_INITIALIZE
239 3
        $event = new EventArgs(
240
            array(
241 3
                'productClassId' => $productClassId,
242
            ),
243
            $request
244
        );
245 3
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_CART_DOWN_INITIALIZE, $event);
246
247
        try {
248 3
            $productClassId = $event->getArgument('productClassId');
249 3
            $app['eccube.service.cart']->downProductQuantity($productClassId)->save();
250
251
            // FRONT_CART_UP_COMPLETE
252 3
            $event = new EventArgs(
253
                array(
254 3
                    'productClassId' => $productClassId,
255
                ),
256
                $request
257
            );
258 3
            $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_CART_DOWN_COMPLETE, $event);
259
260 3
            if ($event->hasResponse()) {
261 3
                return $event->getResponse();
262
            }
263
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
264
        } catch (CartException $e) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
265
266
            // FRONT_CART_DOWN_EXCEPTION
267
            $event = new EventArgs(
268
                array(
269
                    'exception' => $e,
270
                ),
271
                $request
272
            );
273
            $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_CART_DOWN_EXCEPTION, $event);
274
275
            if ($event->hasResponse()) {
276
                return $event->getResponse();
277
            }
278
279
            $app->addRequestError($e->getMessage());
280
        }
281
282 3
        return $app->redirect($app->url('cart'));
283
    }
284
285
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$productClassId" missing
Loading history...
286
     * カートに入っている商品を削除する.
287
     *
288
     * @param Application $app
0 ignored issues
show
introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
289
     * @param Request $request
0 ignored issues
show
introduced by
Expected 8 spaces after parameter type; 1 found
Loading history...
290
     * @param $productClassId
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
291
     * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
292
     */
293 2
    public function remove(Application $app, Request $request, $productClassId)
294
    {
295 2
        $this->isTokenValid($app);
296
297
        // FRONT_CART_REMOVE_INITIALIZE
298 2
        $event = new EventArgs(
299
            array(
300 2
                'productClassId' => $productClassId,
301
            ),
302
            $request
303
        );
304 2
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_CART_REMOVE_INITIALIZE, $event);
305
306 2
        $productClassId = $event->getArgument('productClassId');
307 2
        $app['eccube.service.cart']->removeProduct($productClassId)->save();
308
309
        // FRONT_CART_REMOVE_COMPLETE
310 2
        $event = new EventArgs(
311
            array(
312 2
                'productClassId' => $productClassId,
313
            ),
314
            $request
315
        );
316 2
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_CART_REMOVE_COMPLETE, $event);
317
318 2
        if ($event->hasResponse()) {
319
            return $event->getResponse();
320
        }
321
322 2
        return $app->redirect($app->url('cart'));
323
    }
324
325
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$quantity" missing
Loading history...
introduced by
Doc comment for parameter "$productClassId" missing
Loading history...
326
     * カートに商品を個数を指定して設定する.
327
     *
328
     * @param Application $app
0 ignored issues
show
introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
329
     * @param Request $request
0 ignored issues
show
introduced by
Expected 8 spaces after parameter type; 1 found
Loading history...
330
     * @param $productClassId
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
331
     * @param $quantity
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
332
     * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
333
     * @throws CartException
334
     *
335
     * @deprecated since 3.0.0, to be removed in 3.1
336
     */
337
    public function setQuantity(Application $app, Request $request, $productClassId, $quantity)
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...
338
    {
339
        $this->isTokenValid($app);
340
341
        $app['eccube.service.cart']->setProductQuantity($productClassId, $quantity)->save();
342
343
        return $app->redirect($app->url('cart'));
344
    }
345
346
    /**
347
     * カートをロック状態に設定し、購入確認画面へ遷移する.
348
     *
349
     * @param Application $app
350
     * @param Request $request
0 ignored issues
show
introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
351
     * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
352
     */
353
    public function buystep(Application $app, Request $request)
354
    {
355
        // FRONT_CART_BUYSTEP_INITIALIZE
356
        $event = new EventArgs(
357
            array(),
358
            $request
359
        );
360
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_CART_BUYSTEP_INITIALIZE, $event);
361
362
        $app['eccube.service.cart']->lock();
363
        $app['eccube.service.cart']->save();
364
365
        // FRONT_CART_BUYSTEP_COMPLETE
366
        $event = new EventArgs(
367
            array(),
368
            $request
369
        );
370
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_CART_BUYSTEP_COMPLETE, $event);
371
372
        if ($event->hasResponse()) {
373
            return $event->getResponse();
374
        }
375
376
        return $app->redirect($app->url('shopping'));
377
    }
378
}
379