Failed Conditions
Push — master ( 51b8bf...a12a0c )
by Ryo
45:25
created

MypageController   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 283
Duplicated Lines 7.42 %

Coupling/Cohesion

Components 0
Dependencies 7

Test Coverage

Coverage 93.04%

Importance

Changes 0
Metric Value
dl 21
loc 283
ccs 107
cts 115
cp 0.9304
rs 10
c 0
b 0
f 0
wmc 18
lcom 0
cbo 7

6 Methods

Rating   Name   Duplication   Size   Complexity  
B login() 6 34 4
B index() 0 37 1
B favorite() 0 33 2
B delete() 15 34 2
B order() 0 60 7
B history() 0 32 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/*
4
 * This file is part of EC-CUBE
5
 *
6
 * Copyright(c) 2000-2015 LOCKON CO.,LTD. All Rights Reserved.
7
 *
8
 * http://www.lockon.co.jp/
9
 *
10
 * This program is free software; you can redistribute it and/or
11
 * modify it under the terms of the GNU General Public License
12
 * as published by the Free Software Foundation; either version 2
13
 * of the License, or (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU General Public License
21
 * along with this program; if not, write to the Free Software
22
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23
 */
24
25
namespace Eccube\Controller\Mypage;
26
27
use Eccube\Application;
28
use Eccube\Common\Constant;
29
use Eccube\Controller\AbstractController;
30
use Eccube\Event\EccubeEvents;
31
use Eccube\Event\EventArgs;
32
use Eccube\Exception\CartException;
33
use Symfony\Component\HttpFoundation\Request;
34
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
35
36
class MypageController extends AbstractController
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
37
{
38
    /**
39
     * ログイン画面.
40
     *
41
     * @param Application $app
42
     * @param Request $request
0 ignored issues
show
introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
43
     * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
44
     */
45 4
    public function login(Application $app, Request $request)
46
    {
47 4
        if ($app->isGranted('IS_AUTHENTICATED_FULLY')) {
48 2
            log_info('認証済のためログイン処理をスキップ');
49
50 2
            return $app->redirect($app->url('mypage'));
51
        }
52
53
        /* @var $form \Symfony\Component\Form\FormInterface */
54 2
        $builder = $app['form.factory']
55 2
            ->createNamedBuilder('', 'customer_login');
56
57 2 View Code Duplication
        if ($app->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
58
            $Customer = $app->user();
59
            if ($Customer) {
60
                $builder->get('login_email')->setData($Customer->getEmail());
61
            }
62
        }
63
64 2
        $event = new EventArgs(
65
            array(
66 2
                'builder' => $builder,
67
            ),
68
            $request
69
        );
70 2
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_MYPAGE_MYPAGE_LOGIN_INITIALIZE, $event);
71
72 2
        $form = $builder->getForm();
73
74 2
        return $app->render('Mypage/login.twig', array(
75 2
            'error' => $app['security.last_error']($request),
76 2
            'form' => $form->createView(),
77
        ));
78
    }
79
80
    /**
81
     * マイページ
82
     *
83
     * @param Application $app
84
     * @param Request $request
0 ignored issues
show
introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
85
     * @return \Symfony\Component\HttpFoundation\Response
86
     */
87 8
    public function index(Application $app, Request $request)
88
    {
89 8
        $Customer = $app['user'];
90
91
        /* @var $softDeleteFilter \Eccube\Doctrine\Filter\SoftDeleteFilter */
92 8
        $softDeleteFilter = $app['orm.em']->getFilters()->getFilter('soft_delete');
93 8
        $softDeleteFilter->setExcludes(array(
94 8
            'Eccube\Entity\ProductClass',
95
        ));
96
97
        // 購入処理中/決済処理中ステータスの受注を非表示にする.
98 8
        $app['orm.em']
99 8
            ->getFilters()
100 8
            ->enable('incomplete_order_status_hidden');
101
102
        // paginator
103 8
        $qb = $app['eccube.repository.order']->getQueryBuilderByCustomer($Customer);
104
105 8
        $event = new EventArgs(
106
            array(
107 8
                'qb' => $qb,
108 8
                'Customer' => $Customer,
109
            ),
110
            $request
111
        );
112 8
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_MYPAGE_MYPAGE_INDEX_SEARCH, $event);
113
114 8
        $pagination = $app['paginator']()->paginate(
115
            $qb,
116 8
            $request->get('pageno', 1),
117 8
            $app['config']['search_pmax']
118
        );
119
120 8
        return $app->render('Mypage/index.twig', array(
121 8
            'pagination' => $pagination,
122
        ));
123
    }
124
125
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$id" missing
Loading history...
126
     * 購入履歴詳細を表示する.
127
     *
128
     * @param Application $app
129
     * @param Request $request
0 ignored issues
show
introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
130
     * @param $id
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
131
     * @return \Symfony\Component\HttpFoundation\Response
132
     */
133 10
    public function history(Application $app, Request $request, $id)
134
    {
135
        /* @var $softDeleteFilter \Eccube\Doctrine\Filter\SoftDeleteFilter */
136 10
        $softDeleteFilter = $app['orm.em']->getFilters()->getFilter('soft_delete');
137 10
        $softDeleteFilter->setExcludes(array(
138 10
            'Eccube\Entity\ProductClass',
139
        ));
140
141 10
        $app['orm.em']->getFilters()->enable('incomplete_order_status_hidden');
142 10
        $Order = $app['eccube.repository.order']->findOneBy(array(
143 10
            'id' => $id,
144 10
            'Customer' => $app->user(),
145
        ));
146
        
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
147 10
        $event = new EventArgs(
148
            array(
149 10
                'Order' => $Order,
150
            ),
151
            $request
152
        );
153 10
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_MYPAGE_MYPAGE_HISTORY_INITIALIZE, $event);
154
155 10
        $Order = $event->getArgument('Order');
156
157 10
        if (!$Order) {
158 2
            throw new NotFoundHttpException();
159
        }
160
161 8
        return $app->render('Mypage/history.twig', array(
162 8
            'Order' => $Order,
163
        ));
164
    }
165
166
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$id" missing
Loading history...
167
     * 再購入を行う.
168
     *
169
     * @param Application $app
170
     * @param Request $request
0 ignored issues
show
introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
171
     * @param $id
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
172
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
173
     */
174 8
    public function order(Application $app, Request $request, $id)
175
    {
176 8
        $this->isTokenValid($app);
177
178 8
        log_info('再注文開始', array($id));
179
180 8
        $Customer = $app->user();
181
182
        /* @var $Order \Eccube\Entity\Order */
183 8
        $Order = $app['eccube.repository.order']->findOneBy(array(
184 8
            'id' => $id,
185 8
            'Customer' => $Customer,
186
        ));
187
188 8
        $event = new EventArgs(
189
            array(
190 8
                'Order' => $Order,
191 8
                'Customer' => $Customer,
192
            ),
193
            $request
194
        );
195 8
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_MYPAGE_MYPAGE_ORDER_INITIALIZE, $event);
196
197 8
        if (!$Order) {
198
            log_info('対象の注文が見つかりません', array($id));
199
            throw new NotFoundHttpException();
200
        }
201
202 8
        foreach ($Order->getOrderDetails() as $OrderDetail) {
203
            try {
204 8
                if ($OrderDetail->getProduct() &&
205 8
                    $OrderDetail->getProductClass()) {
206 8
                    $app['eccube.service.cart']->addProduct($OrderDetail->getProductClass()->getId(), $OrderDetail->getQuantity())->save();
207
                } else {
208
                    log_info($app->trans('cart.product.delete'), array($id));
209 7
                    $app->addRequestError('cart.product.delete');
210
                }
211 2
            } catch (CartException $e) {
212 2
                log_info($e->getMessage(), array($id));
213 8
                $app->addRequestError($e->getMessage());
214
            }
215
        }
216
217 8
        $event = new EventArgs(
218
            array(
219 8
                'Order' => $Order,
220 8
                'Customer' => $Customer,
221
            ),
222
            $request
223
        );
224 8
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_MYPAGE_MYPAGE_ORDER_COMPLETE, $event);
225
226 8
        if ($event->getResponse() !== null) {
227
            return $event->getResponse();
228
        }
229
230 8
        log_info('再注文完了', array($id));
231
232 8
        return $app->redirect($app->url('cart'));
233
    }
234
235
    /**
236
     * お気に入り商品を表示する.
237
     *
238
     * @param Application $app
239
     * @param Request $request
0 ignored issues
show
introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
240
     * @return \Symfony\Component\HttpFoundation\Response
241
     */
242 3
    public function favorite(Application $app, Request $request)
243
    {
244 3
        $BaseInfo = $app['eccube.repository.base_info']->get();
245
246 3
        if ($BaseInfo->getOptionFavoriteProduct() == Constant::ENABLED) {
247 3
            $Customer = $app->user();
248
249
            // paginator
250 3
            $qb = $app['eccube.repository.customer_favorite_product']->getQueryBuilderByCustomer($Customer);
251
252 3
            $event = new EventArgs(
253
                array(
254 3
                    'qb' => $qb,
255 3
                    'Customer' => $Customer,
256
                ),
257
                $request
258
            );
259 3
            $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_MYPAGE_MYPAGE_FAVORITE_SEARCH, $event);
260
261 3
            $pagination = $app['paginator']()->paginate(
262
                $qb,
263 3
                $request->get('pageno', 1),
264 3
                $app['config']['search_pmax'],
265 3
                array('wrap-queries' => true)
266
            );
267
268 3
            return $app->render('Mypage/favorite.twig', array(
269 3
                'pagination' => $pagination,
270
            ));
271
        } else {
272
            throw new NotFoundHttpException();
273
        }
274
    }
275
276
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$id" missing
Loading history...
277
     * お気に入り商品を削除する.
278
     *
279
     * @param Application $app
280
     * @param Request $request
0 ignored issues
show
introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
281
     * @param $id
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
282
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
283
     */
284 2
    public function delete(Application $app, Request $request, $id)
285
    {
286 2
        $this->isTokenValid($app);
287
288 2
        $Customer = $app->user();
289
290 2
        $Product = $app['eccube.repository.product']->find($id);
291
292 2
        $event = new EventArgs(
293
            array(
294 2
                'Customer' => $Customer,
295 2
                'Product' => $Product,
296
            ), $request
297
        );
298 2
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_MYPAGE_MYPAGE_DELETE_INITIALIZE, $event);
299
300 2 View Code Duplication
        if ($Product) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
301 1
            log_info('お気に入り商品削除開始');
302
303 1
            $app['eccube.repository.customer_favorite_product']->deleteFavorite($Customer, $Product);
304
305 1
            $event = new EventArgs(
306
                array(
307 1
                    'Customer' => $Customer,
308 1
                    'Product' => $Product,
309
                ), $request
310
            );
311 1
            $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_MYPAGE_MYPAGE_DELETE_COMPLETE, $event);
312
313 1
            log_info('お気に入り商品削除完了');
314
        }
315
316 2
        return $app->redirect($app->url('mypage_favorite'));
317
    }
318
}
319