Failed Conditions
Push — 3.0.9-dev ( 4e695e...ec0f79 )
by chihiro
26:04
created

ProductController::detail()   D

Complexity

Conditions 15
Paths 34

Size

Total Lines 114
Code Lines 72

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 15

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 114
ccs 11
cts 11
cp 1
rs 4.9121
cc 15
eloc 72
nc 34
nop 3
crap 15

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