Completed
Pull Request — master (#1922)
by chihiro
39:42
created

ProductController::detail()   D

Complexity

Conditions 16
Paths 38

Size

Total Lines 121
Code Lines 75

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 67.1465

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 16
eloc 75
nc 38
nop 3
dl 0
loc 121
ccs 27
cts 65
cp 0.4154
crap 67.1465
rs 4.8736
c 2
b 0
f 0

How to fix   Long Method    Complexity   

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\Common\Constant;
29
use Eccube\Event\EccubeEvents;
30
use Eccube\Event\EventArgs;
31
use Eccube\Exception\CartException;
32
use Symfony\Component\HttpFoundation\Request;
33
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
34
35
class ProductController
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
36
{
37
38
    private $title;
39
40 5
    public function __construct()
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
41
    {
42 5
        $this->title = '';
43
    }
44
45 2
    public function index(Application $app, Request $request)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
46
    {
47 2
        $BaseInfo = $app['eccube.repository.base_info']->get();
48
49
        // Doctrine SQLFilter
50 2
        if ($BaseInfo->getNostockHidden() === Constant::ENABLED) {
51
            $app['orm.em']->getFilters()->enable('nostock_hidden');
52
        }
53
54
        // handleRequestは空のqueryの場合は無視するため
55 2
        if ($request->getMethod() === 'GET') {
56 2
            $request->query->set('pageno', $request->query->get('pageno', ''));
57
        }
58
59
        // searchForm
60
        /* @var $builder \Symfony\Component\Form\FormBuilderInterface */
61 2
        $builder = $app['form.factory']->createNamedBuilder('', 'search_product');
62 2
        $builder->setAttribute('freeze', true);
63 2
        $builder->setAttribute('freeze_display_text', false);
64 2
        if ($request->getMethod() === 'GET') {
65 2
            $builder->setMethod('GET');
66
        }
67
68 2
        $event = new EventArgs(
69
            array(
70 2
                'builder' => $builder,
71
            ),
72
            $request
73
        );
74 2
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_PRODUCT_INDEX_INITIALIZE, $event);
75
76
        /* @var $searchForm \Symfony\Component\Form\FormInterface */
77 2
        $searchForm = $builder->getForm();
78
79 2
        $searchForm->handleRequest($request);
80
81
        // paginator
82 2
        $searchData = $searchForm->getData();
83 2
        $qb = $app['eccube.repository.product']->getQueryBuilderBySearchData($searchData);
84
85 2
        $event = new EventArgs(
86
            array(
87 2
                'searchData' => $searchData,
88 2
                'qb' => $qb,
89
            ),
90
            $request
91
        );
92 2
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_PRODUCT_INDEX_SEARCH, $event);
93 2
        $searchData = $event->getArgument('searchData');
94
95 2
        $pagination = $app['paginator']()->paginate(
96
            $qb,
97 2
            !empty($searchData['pageno']) ? $searchData['pageno'] : 1,
98 2
            $searchData['disp_number']->getId(),
99
            array(
100 2
                'wrap-queries' => true,
101
            )
102
        );
103
104
        // addCart form
105 2
        $forms = array();
106 2
        foreach ($pagination as $Product) {
107
            /* @var $builder \Symfony\Component\Form\FormBuilderInterface */
108 2
            $builder = $app['form.factory']->createNamedBuilder('', 'add_cart', null, array(
109 2
                'product' => $Product,
110
                'allow_extra_fields' => true,
111
            ));
112 2
            $addCartForm = $builder->getForm();
113
114 2
            if ($request->getMethod() === 'POST' && (string)$Product->getId() === $request->get('product_id')) {
0 ignored issues
show
Coding Style introduced by
As per coding-style, a cast statement should be followed by a single space.
Loading history...
115
                $addCartForm->handleRequest($request);
116
117
                if ($addCartForm->isValid()) {
118
                    $addCartData = $addCartForm->getData();
119
120
                    try {
121
                        $app['eccube.service.cart']->addProduct($addCartData['product_class_id'], $addCartData['quantity'])->save();
122
                    } catch (CartException $e) {
123
                        $app->addRequestError($e->getMessage());
124
                    }
125
126
                    $event = new EventArgs(
127
                        array(
128
                            'form' => $addCartForm,
129
                            'Product' => $Product,
130
                        ),
131
                        $request
132
                    );
133
                    $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_PRODUCT_INDEX_COMPLETE, $event);
134
135
                    if ($event->getResponse() !== null) {
136
                        return $event->getResponse();
137
                    }
138
139
                    return $app->redirect($app->url('cart'));
140
                }
141
            }
142
143 2
            $forms[$Product->getId()] = $addCartForm->createView();
144
        }
145
146
        // 表示件数
147 2
        $builder = $app['form.factory']->createNamedBuilder('disp_number', 'product_list_max', null, array(
148 2
            'empty_data' => null,
149
            'required' => false,
150
            'label' => '表示件数',
151
            'allow_extra_fields' => true,
152
        ));
153 2
        if ($request->getMethod() === 'GET') {
154 2
            $builder->setMethod('GET');
155
        }
156
157 2
        $event = new EventArgs(
158
            array(
159 2
                'builder' => $builder,
160
            ),
161
            $request
162
        );
163 2
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_PRODUCT_INDEX_DISP, $event);
164
165 2
        $dispNumberForm = $builder->getForm();
166
167 2
        $dispNumberForm->handleRequest($request);
168
169
        // ソート順
170 2
        $builder = $app['form.factory']->createNamedBuilder('orderby', 'product_list_order_by', null, array(
171 2
            'empty_data' => null,
172
            'required' => false,
173
            'label' => '表示順',
174
            'allow_extra_fields' => true,
175
        ));
176 2
        if ($request->getMethod() === 'GET') {
177 2
            $builder->setMethod('GET');
178
        }
179
180 2
        $event = new EventArgs(
181
            array(
182 2
                'builder' => $builder,
183
            ),
184
            $request
185
        );
186 2
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_PRODUCT_INDEX_ORDER, $event);
187
188 2
        $orderByForm = $builder->getForm();
189
190 2
        $orderByForm->handleRequest($request);
191
192 2
        $Category = $searchForm->get('category_id')->getData();
193
194 2
        return $app->render('Product/list.twig', array(
195 2
            'subtitle' => $this->getPageTitle($searchData),
196 2
            'pagination' => $pagination,
197 2
            'search_form' => $searchForm->createView(),
198 2
            'disp_number_form' => $dispNumberForm->createView(),
199 2
            'order_by_form' => $orderByForm->createView(),
200 2
            'forms' => $forms,
201 2
            'Category' => $Category,
202
        ));
203
    }
204
205 3
    public function detail(Application $app, Request $request, $id)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
206
    {
207 3
        $BaseInfo = $app['eccube.repository.base_info']->get();
208 3
        if ($BaseInfo->getNostockHidden() === Constant::ENABLED) {
209
            $app['orm.em']->getFilters()->enable('nostock_hidden');
210
        }
211
212
        /* @var $Product \Eccube\Entity\Product */
213 3
        $Product = $app['eccube.repository.product']->get($id);
214 3
        if (!$request->getSession()->has('_security_admin') && $Product->getStatus()->getId() !== 1) {
215
            throw new NotFoundHttpException();
216
        }
217 3
        if (count($Product->getProductClasses()) < 1) {
218
            throw new NotFoundHttpException();
219
        }
220
221
        /* @var $builder \Symfony\Component\Form\FormBuilderInterface */
222 3
        $builder = $app['form.factory']->createNamedBuilder('', 'add_cart', null, array(
223 3
            'product' => $Product,
224
            'id_add_product_id' => false,
225
        ));
226
227 3
        $event = new EventArgs(
228
            array(
229 3
                'builder' => $builder,
230 3
                'Product' => $Product,
231
            ),
232
            $request
233
        );
234 3
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_PRODUCT_DETAIL_INITIALIZE, $event);
235
236
        /* @var $form \Symfony\Component\Form\FormInterface */
237 3
        $form = $builder->getForm();
238
239 3
        if ($request->getMethod() === 'POST') {
240 1
            $form->handleRequest($request);
241
242 1
            if ($form->isValid()) {
243
                $addCartData = $form->getData();
244
                if ($addCartData['mode'] === 'add_favorite') {
245
                    if ($app->isGranted('ROLE_USER')) {
246
                        $Customer = $app->user();
247
                        $app['eccube.repository.customer_favorite_product']->addFavorite($Customer, $Product);
248
                        $app['session']->getFlashBag()->set('product_detail.just_added_favorite', $Product->getId());
249
250
                        $event = new EventArgs(
251
                            array(
252
                                'form' => $form,
253
                                'Product' => $Product,
254
                            ),
255
                            $request
256
                        );
257
                        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_PRODUCT_DETAIL_FAVORITE, $event);
258
259
                        if ($event->getResponse() !== null) {
260
                            return $event->getResponse();
261
                        }
262
263
                        return $app->redirect($app->url('product_detail', array('id' => $Product->getId())));
264
                    } else {
265
                        // 非会員の場合、ログイン画面を表示
266
                        //  ログイン後の画面遷移先を設定
267
                        $app->setLoginTargetPath($app->url('product_detail', array('id' => $Product->getId())));
268
                        $app['session']->getFlashBag()->set('eccube.add.favorite', true);
269
                        return $app->redirect($app->url('mypage_login'));
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
270
                    }
271
                } elseif ($addCartData['mode'] === 'add_cart') {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
272
273
                    log_info('カート追加処理開始', array('product_id' => $Product->getId(), 'product_class_id' => $addCartData['product_class_id'], 'quantity' => $addCartData['quantity']));
274
275
                    try {
276
                        $app['eccube.service.cart']->addProduct($addCartData['product_class_id'], $addCartData['quantity'])->save();
277
                    } catch (CartException $e) {
278
                        log_info('カート追加エラー', array($e->getMessage()));
279
                        $app->addRequestError($e->getMessage());
280
                    }
281
282
                    log_info('カート追加処理完了', array('product_id' => $Product->getId(), 'product_class_id' => $addCartData['product_class_id'], 'quantity' => $addCartData['quantity']));
283
284
                    $event = new EventArgs(
285
                        array(
286
                            'form' => $form,
287
                            'Product' => $Product,
288
                        ),
289
                        $request
290
                    );
291
                    $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_PRODUCT_DETAIL_COMPLETE, $event);
292
293
                    if ($event->getResponse() !== null) {
294
                        return $event->getResponse();
295
                    }
296
297 1
                    return $app->redirect($app->url('cart'));
298
                }
299
            }
300
        } else {
301 2
            $addFavorite = $app['session']->getFlashBag()->get('eccube.add.favorite');
302 2
            if (!empty($addFavorite)) {
303
                // お気に入り登録時にログインされていない場合、ログイン後にお気に入り追加処理を行う
304
                if ($app->isGranted('ROLE_USER')) {
305
                    $Customer = $app->user();
306
                    $app['eccube.repository.customer_favorite_product']->addFavorite($Customer, $Product);
307
                    $app['session']->getFlashBag()->set('product_detail.just_added_favorite', $Product->getId());
308
                }
309
            }
310
        }
311
312 3
        $is_favorite = false;
313 3
        if ($app->isGranted('ROLE_USER')) {
314
            $Customer = $app->user();
315
            $is_favorite = $app['eccube.repository.customer_favorite_product']->isFavorite($Customer, $Product);
316
        }
317
318 3
        return $app->render('Product/detail.twig', array(
319 3
            'title' => $this->title,
320 3
            'subtitle' => $Product->getName(),
321 3
            'form' => $form->createView(),
322 3
            'Product' => $Product,
323 3
            'is_favorite' => $is_favorite,
324
        ));
325
    }
326
327
    /**
328
     * ページタイトルの設定
329
     *
330
     * @param  null|array $searchData
331
     * @return str
332
     */
333 2
    private function getPageTitle($searchData)
334
    {
335 2
        if (isset($searchData['name']) && !empty($searchData['name'])) {
336
            return '検索結果';
337 2
        } elseif (isset($searchData['category_id']) && $searchData['category_id']) {
338
            return $searchData['category_id']->getName();
339
        } else {
340 2
            return '全商品';
341
        }
342
    }
343
}
344