Failed Conditions
Push — experimental/3.1 ( 3d2ede...2919b9 )
by Yangsin
28:59
created

MypageController   B

Complexity

Total Complexity 18

Size/Duplication

Total Lines 313
Duplicated Lines 10.86 %

Coupling/Cohesion

Components 1
Dependencies 16

Test Coverage

Coverage 62.5%

Importance

Changes 0
Metric Value
dl 34
loc 313
ccs 70
cts 112
cp 0.625
rs 8.4614
c 0
b 0
f 0
wmc 18
lcom 1
cbo 16

6 Methods

Rating   Name   Duplication   Size   Complexity  
B login() 6 34 4
B index() 0 31 1
B history() 28 28 2
B order() 0 66 7
B favorite() 0 30 2
B delete() 0 26 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 Doctrine\ORM\EntityManager;
28
use Eccube\Annotation\Component;
29
use Eccube\Annotation\Inject;
30
use Eccube\Application;
31
use Eccube\Common\Constant;
32
use Eccube\Controller\AbstractController;
33
use Eccube\Entity\BaseInfo;
34
use Eccube\Entity\CustomerFavoriteProduct;
35
use Eccube\Entity\Product;
36
use Eccube\Event\EccubeEvents;
37
use Eccube\Event\EventArgs;
38
use Eccube\Exception\CartException;
39
use Eccube\Form\Type\Front\CustomerLoginType;
40
use Eccube\Repository\CustomerFavoriteProductRepository;
41
use Eccube\Repository\OrderRepository;
42
use Eccube\Repository\ProductRepository;
43
use Eccube\Service\CartService;
44
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
45
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
46
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
47
use Symfony\Component\EventDispatcher\EventDispatcher;
48
use Symfony\Component\Form\FormFactory;
49
use Symfony\Component\HttpFoundation\Request;
50
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
51
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
52
53
/**
54
 * @Component
55
 * @Route(service=MypageController::class)
56
 */
57
class MypageController extends AbstractController
58
{
59
    /**
60
     * @Inject(ProductRepository::class)
61
     * @var ProductRepository
62
     */
63
    protected $productRepository;
64
65
    /**
66
     * @Inject(CustomerFavoriteProductRepository::class)
67
     * @var CustomerFavoriteProductRepository
68
     */
69
    protected $customerFavoriteProductRepository;
70
71
    /**
72
     * @Inject(BaseInfo::class)
73
     * @var BaseInfo
74
     */
75
    protected $BaseInfo;
76
77
    /**
78
     * @Inject(CartService::class)
79
     * @var CartService
80
     */
81
    protected $cartService;
82
83
    /**
84
     * @Inject("config")
85
     * @var array
86
     */
87
    protected $appConfig;
88
89
    /**
90
     * @Inject(OrderRepository::class)
91
     * @var OrderRepository
92
     */
93
    protected $orderRepository;
94
95
    /**
96
     * @Inject("orm.em")
97
     * @var EntityManager
98
     */
99
    protected $entityManager;
100
101
    /**
102
     * @Inject("eccube.event.dispatcher")
103
     * @var EventDispatcher
104
     */
105
    protected $eventDispatcher;
106
107
    /**
108
     * @Inject("form.factory")
109
     * @var FormFactory
110
     */
111
    protected $formFactory;
112
113
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
introduced by
Doc comment for parameter "$request" missing
Loading history...
114
     * ログイン画面.
115
     *
116
     * @Route("/mypage/login", name="mypage_login")
117
     * @Template("Mypage/login.twig")
118
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
119 2
    public function login(Application $app, Request $request)
120
    {
121 2
        if ($app->isGranted('IS_AUTHENTICATED_FULLY')) {
122 1
            log_info('認証済のためログイン処理をスキップ');
123
124 1
            return $app->redirect($app->url('mypage'));
125
        }
126
127
        /* @var $form \Symfony\Component\Form\FormInterface */
128 1
        $builder = $this->formFactory
129 1
            ->createNamedBuilder('', CustomerLoginType::class);
130
131 1 View Code Duplication
        if ($app->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
132
            $Customer = $app->user();
133
            if ($Customer) {
134
                $builder->get('login_email')->setData($Customer->getEmail());
135
            }
136
        }
137
138 1
        $event = new EventArgs(
139
            array(
140 1
                'builder' => $builder,
141
            ),
142 1
            $request
143
        );
144 1
        $this->eventDispatcher->dispatch(EccubeEvents::FRONT_MYPAGE_MYPAGE_LOGIN_INITIALIZE, $event);
145
146 1
        $form = $builder->getForm();
147
148
        return [
149 1
            'error' => $app['security.last_error']($request),
150 1
            'form' => $form->createView(),
151
        ];
152
    }
153
154
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
introduced by
Doc comment for parameter "$request" missing
Loading history...
155
     * マイページ.
156
     *
157
     * @Route("/mypage", name="mypage")
158
     * @Template("Mypage/index.twig")
159
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
160 1
    public function index(Application $app, Request $request)
161
    {
162 1
        $Customer = $app['user'];
163
164
        // 購入処理中/決済処理中ステータスの受注を非表示にする.
165 1
        $this->entityManager
166 1
            ->getFilters()
167 1
            ->enable('incomplete_order_status_hidden');
168
169
        // paginator
170 1
        $qb = $this->orderRepository->getQueryBuilderByCustomer($Customer);
171
172 1
        $event = new EventArgs(
173
            array(
174 1
                'qb' => $qb,
175 1
                'Customer' => $Customer,
176
            ),
177 1
            $request
178
        );
179 1
        $this->eventDispatcher->dispatch(EccubeEvents::FRONT_MYPAGE_MYPAGE_INDEX_SEARCH, $event);
180
181 1
        $pagination = $app['paginator']()->paginate(
182 1
            $qb,
183 1
            $request->get('pageno', 1),
184 1
            $this->appConfig['search_pmax']
185
        );
186
187
        return [
188 1
            'pagination' => $pagination,
189
        ];
190
    }
191
192
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
introduced by
Doc comment for parameter "$request" missing
Loading history...
introduced by
Doc comment for parameter "$id" missing
Loading history...
193
     * 購入履歴詳細を表示する.
194
     *
195
     * @Route("/mypage/history/{id}", name="mypage_history", requirements={"id" = "\d+"})
196
     * @Template("Mypage/history.twig")
197
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
198 2 View Code Duplication
    public function history(Application $app, Request $request, $id)
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...
199
    {
200 2
        $this->entityManager->getFilters()->enable('incomplete_order_status_hidden');
201 2
        $Order = $this->orderRepository->findOneBy(
202
            array(
203 2
                'id' => $id,
204 2
                'Customer' => $app->user(),
205
            )
206
        );
207
208 2
        $event = new EventArgs(
209
            array(
210 2
                'Order' => $Order,
211
            ),
212 2
            $request
213
        );
214 2
        $this->eventDispatcher->dispatch(EccubeEvents::FRONT_MYPAGE_MYPAGE_HISTORY_INITIALIZE, $event);
215
216 2
        $Order = $event->getArgument('Order');
217
218 2
        if (!$Order) {
219 2
            throw new NotFoundHttpException();
220
        }
221
222
        return [
223
            'Order' => $Order,
224
        ];
225
    }
226
227
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
introduced by
Doc comment for parameter "$request" missing
Loading history...
introduced by
Doc comment for parameter "$id" missing
Loading history...
228
     * 再購入を行う.
229
     *
230
     * @Route("/mypage/order/{id}", name="mypage_order", requirements={"id" = "\d+"})
231
     * @Method("PUT")
232
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
233
    public function order(Application $app, Request $request, $id)
234
    {
235
        $this->isTokenValid($app);
236
237
        log_info('再注文開始', array($id));
238
239
        $Customer = $app->user();
240
241
        /* @var $Order \Eccube\Entity\Order */
242
        $Order = $this->orderRepository->findOneBy(
243
            array(
244
                'id' => $id,
245
                'Customer' => $Customer,
246
            )
247
        );
248
249
        $event = new EventArgs(
250
            array(
251
                'Order' => $Order,
252
                'Customer' => $Customer,
253
            ),
254
            $request
255
        );
256
        $this->eventDispatcher->dispatch(EccubeEvents::FRONT_MYPAGE_MYPAGE_ORDER_INITIALIZE, $event);
257
258
        if (!$Order) {
259
            log_info('対象の注文が見つかりません', array($id));
260
            throw new NotFoundHttpException();
261
        }
262
263
        foreach ($Order->getOrderDetails() as $OrderDetail) {
264
            try {
265
                if ($OrderDetail->getProduct() &&
266
                    $OrderDetail->getProductClass()
267
                ) {
268
                    $this->cartService->addProduct(
0 ignored issues
show
Bug introduced by
The method save cannot be called on $this->cartService->addP...rDetail->getQuantity()) (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
269
                        $OrderDetail->getProductClass()->getId(),
270
                        $OrderDetail->getQuantity()
271
                    )->save();
272
                } else {
273
                    log_info($app->trans('cart.product.delete'), array($id));
274
                    $app->addRequestError('cart.product.delete');
275
                }
276
            } catch (CartException $e) {
277
                log_info($e->getMessage(), array($id));
278
                $app->addRequestError($e->getMessage());
279
            }
280
        }
281
282
        $event = new EventArgs(
283
            array(
284
                'Order' => $Order,
285
                'Customer' => $Customer,
286
            ),
287
            $request
288
        );
289
        $this->eventDispatcher->dispatch(EccubeEvents::FRONT_MYPAGE_MYPAGE_ORDER_COMPLETE, $event);
290
291
        if ($event->getResponse() !== null) {
292
            return $event->getResponse();
293
        }
294
295
        log_info('再注文完了', array($id));
296
297
        return $app->redirect($app->url('cart'));
298
    }
299
300
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
introduced by
Doc comment for parameter "$request" missing
Loading history...
301
     * お気に入り商品を表示する.
302
     *
303
     * @Route("/mypage/favorite", name="mypage_favorite")
304
     * @Template("Mypage/favorite.twig")
305
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
306 2
    public function favorite(Application $app, Request $request)
307
    {
308 2
        if ($this->BaseInfo->getOptionFavoriteProduct() == Constant::DISABLED) {
309
            throw new NotFoundHttpException();
310
        }
311 2
        $Customer = $app->user();
312
313
        // paginator
314 2
        $qb = $this->customerFavoriteProductRepository->getQueryBuilderByCustomer($Customer);
315
316 2
        $event = new EventArgs(
317
            array(
318 2
                'qb' => $qb,
319 2
                'Customer' => $Customer,
320
            ),
321 2
            $request
322
        );
323 2
        $this->eventDispatcher->dispatch(EccubeEvents::FRONT_MYPAGE_MYPAGE_FAVORITE_SEARCH, $event);
324
325 2
        $pagination = $app['paginator']()->paginate(
326 2
            $qb,
327 2
            $request->get('pageno', 1),
328 2
            $this->appConfig['search_pmax'],
329 2
            array('wrap-queries' => true)
330
        );
331
332
        return [
333 2
            'pagination' => $pagination,
334
        ];
335
    }
336
337
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
introduced by
Doc comment for parameter "$request" missing
Loading history...
introduced by
Doc comment for parameter "$CustomerFavoriteProduct" missing
Loading history...
338
     * お気に入り商品を削除する.
339
     *
340
     * @Method("DELETE")
341
     * @Route("/mypage/favorite/{id}/delete", name="mypage_favorite_delete", requirements={"id" = "\d+"})
342
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
343 1
    public function delete(Application $app, Request $request, CustomerFavoriteProduct $CustomerFavoriteProduct)
344
    {
345 1
        $this->isTokenValid($app);
346
347 1
        $Customer = $app->user();
348
349 1
        log_info('お気に入り商品削除開始', [$Customer->getId(), $CustomerFavoriteProduct->getId()]);
350
351 1
        if ($Customer->getId() !== $CustomerFavoriteProduct->getCustomer()->getId()) {
352
            throw new BadRequestHttpException();
353
        }
354
355 1
        $this->customerFavoriteProductRepository->delete($CustomerFavoriteProduct);
356
357 1
        $event = new EventArgs(
358
            array(
359 1
                'Customer' => $Customer,
360 1
                'CustomerFavoriteProduct' => $CustomerFavoriteProduct,
361 1
            ), $request
362
        );
363 1
        $this->eventDispatcher->dispatch(EccubeEvents::FRONT_MYPAGE_MYPAGE_DELETE_COMPLETE, $event);
364
365 1
        log_info('お気に入り商品削除完了', [$Customer->getId(), $CustomerFavoriteProduct->getId()]);
366
367 1
        return $app->redirect($app->url('mypage_favorite'));
368
    }
369
}
370