Completed
Pull Request — experimental/3.1 (#2484)
by Kentaro
57:08 queued 35:11
created

MypageController::favorite()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 30
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 2.0009

Importance

Changes 0
Metric Value
cc 2
eloc 18
nc 2
nop 2
dl 0
loc 30
ccs 15
cts 16
cp 0.9375
crap 2.0009
rs 8.8571
c 0
b 0
f 0
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\Event\EccubeEvents;
35
use Eccube\Event\EventArgs;
36
use Eccube\Exception\CartException;
37
use Eccube\Form\Type\Front\CustomerLoginType;
38
use Eccube\Repository\CustomerFavoriteProductRepository;
39
use Eccube\Repository\OrderRepository;
40
use Eccube\Repository\ProductRepository;
41
use Eccube\Service\CartService;
42
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
43
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
44
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
45
use Symfony\Component\EventDispatcher\EventDispatcher;
46
use Symfony\Component\Form\FormFactory;
47
use Symfony\Component\HttpFoundation\Request;
48
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
49
50
/**
51
 * @Component
52
 * @Route(service=MypageController::class)
53
 */
54
class MypageController extends AbstractController
55
{
56
    /**
57
     * @Inject(ProductRepository::class)
58
     * @var ProductRepository
59
     */
60
    protected $productRepository;
61
62
    /**
63
     * @Inject(CustomerFavoriteProductRepository::class)
64
     * @var CustomerFavoriteProductRepository
65
     */
66
    protected $customerFavoriteProductRepository;
67
68
    /**
69
     * @Inject(BaseInfo::class)
70
     * @var BaseInfo
71
     */
72
    protected $BaseInfo;
73
74
    /**
75
     * @Inject(CartService::class)
76
     * @var CartService
77
     */
78
    protected $cartService;
79
80
    /**
81
     * @Inject("config")
82
     * @var array
83
     */
84
    protected $appConfig;
85
86
    /**
87
     * @Inject(OrderRepository::class)
88
     * @var OrderRepository
89
     */
90
    protected $orderRepository;
91
92
    /**
93
     * @Inject("orm.em")
94
     * @var EntityManager
95
     */
96
    protected $entityManager;
97
98
    /**
99
     * @Inject("eccube.event.dispatcher")
100
     * @var EventDispatcher
101
     */
102
    protected $eventDispatcher;
103
104
    /**
105
     * @Inject("form.factory")
106
     * @var FormFactory
107
     */
108
    protected $formFactory;
109
110
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
introduced by
Doc comment for parameter "$request" missing
Loading history...
111
     * ログイン画面.
112
     *
113
     * @Route("/mypage/login", name="mypage_login")
114
     * @Template("Mypage/login.twig")
115
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
116 2
    public function login(Application $app, Request $request)
117
    {
118 2
        if ($app->isGranted('IS_AUTHENTICATED_FULLY')) {
119 1
            log_info('認証済のためログイン処理をスキップ');
120
121 1
            return $app->redirect($app->url('mypage'));
122
        }
123
124
        /* @var $form \Symfony\Component\Form\FormInterface */
125 1
        $builder = $this->formFactory
126 1
            ->createNamedBuilder('', CustomerLoginType::class);
127
128 1 View Code Duplication
        if ($app->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
129
            $Customer = $app->user();
130
            if ($Customer) {
131
                $builder->get('login_email')->setData($Customer->getEmail());
132
            }
133
        }
134
135 1
        $event = new EventArgs(
136
            array(
137 1
                'builder' => $builder,
138
            ),
139 1
            $request
140
        );
141 1
        $this->eventDispatcher->dispatch(EccubeEvents::FRONT_MYPAGE_MYPAGE_LOGIN_INITIALIZE, $event);
142
143 1
        $form = $builder->getForm();
144
145
        return [
146 1
            'error' => $app['security.last_error']($request),
147 1
            'form' => $form->createView(),
148
        ];
149
    }
150
151
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
introduced by
Doc comment for parameter "$request" missing
Loading history...
152
     * マイページ.
153
     *
154
     * @Route("/mypage", name="mypage")
155
     * @Template("Mypage/index.twig")
156
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
157 1
    public function index(Application $app, Request $request)
158
    {
159 1
        $Customer = $app['user'];
160
161
        /* @var $softDeleteFilter \Eccube\Doctrine\Filter\SoftDeleteFilter */
162 1
        $softDeleteFilter = $this->entityManager->getFilters()->getFilter('soft_delete');
163 1
        $softDeleteFilter->setExcludes(
164
            array(
165 1
                'Eccube\Entity\ProductClass',
166
            )
167
        );
168
169
        // 購入処理中/決済処理中ステータスの受注を非表示にする.
170 1
        $this->entityManager
171 1
            ->getFilters()
172 1
            ->enable('incomplete_order_status_hidden');
173
174
        // paginator
175 1
        $qb = $this->orderRepository->getQueryBuilderByCustomer($Customer);
176
177 1
        $event = new EventArgs(
178
            array(
179 1
                'qb' => $qb,
180 1
                'Customer' => $Customer,
181
            ),
182 1
            $request
183
        );
184 1
        $this->eventDispatcher->dispatch(EccubeEvents::FRONT_MYPAGE_MYPAGE_INDEX_SEARCH, $event);
185
186 1
        $pagination = $app['paginator']()->paginate(
187 1
            $qb,
188 1
            $request->get('pageno', 1),
189 1
            $this->appConfig['search_pmax']
190
        );
191
192
        return [
193 1
            'pagination' => $pagination,
194
        ];
195
    }
196
197
    /**
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...
198
     * 購入履歴詳細を表示する.
199
     *
200
     * @Route("/mypage/history/{id}", name="mypage_history", requirements={"id" = "\d+"})
201
     * @Template("Mypage/history.twig")
202
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
203 2
    public function history(Application $app, Request $request, $id)
204
    {
205
        /* @var $softDeleteFilter \Eccube\Doctrine\Filter\SoftDeleteFilter */
206 2
        $softDeleteFilter = $this->entityManager->getFilters()->getFilter('soft_delete');
207 2
        $softDeleteFilter->setExcludes(
208
            array(
209 2
                'Eccube\Entity\ProductClass',
210
            )
211
        );
212
213 2
        $this->entityManager->getFilters()->enable('incomplete_order_status_hidden');
214 2
        $Order = $this->orderRepository->findOneBy(
215
            array(
216 2
                'id' => $id,
217 2
                'Customer' => $app->user(),
218
            )
219
        );
220
221 2
        $event = new EventArgs(
222
            array(
223 2
                'Order' => $Order,
224
            ),
225 2
            $request
226
        );
227 2
        $this->eventDispatcher->dispatch(EccubeEvents::FRONT_MYPAGE_MYPAGE_HISTORY_INITIALIZE, $event);
228
229 2
        $Order = $event->getArgument('Order');
230
231 2
        if (!$Order) {
232 2
            throw new NotFoundHttpException();
233
        }
234
235
        return [
236
            'Order' => $Order,
237
        ];
238
    }
239
240
    /**
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...
241
     * 再購入を行う.
242
     *
243
     * @Route("/mypage/order/{id}", name="mypage_order", requirements={"id" = "\d+"})
244
     * @Method("PUT")
245
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
246
    public function order(Application $app, Request $request, $id)
247
    {
248
        $this->isTokenValid($app);
249
250
        log_info('再注文開始', array($id));
251
252
        $Customer = $app->user();
253
254
        /* @var $Order \Eccube\Entity\Order */
255
        $Order = $this->orderRepository->findOneBy(
256
            array(
257
                'id' => $id,
258
                'Customer' => $Customer,
259
            )
260
        );
261
262
        $event = new EventArgs(
263
            array(
264
                'Order' => $Order,
265
                'Customer' => $Customer,
266
            ),
267
            $request
268
        );
269
        $this->eventDispatcher->dispatch(EccubeEvents::FRONT_MYPAGE_MYPAGE_ORDER_INITIALIZE, $event);
270
271
        if (!$Order) {
272
            log_info('対象の注文が見つかりません', array($id));
273
            throw new NotFoundHttpException();
274
        }
275
276
        foreach ($Order->getOrderDetails() as $OrderDetail) {
277
            try {
278
                if ($OrderDetail->getProduct() &&
279
                    $OrderDetail->getProductClass()
280
                ) {
281
                    $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...
282
                        $OrderDetail->getProductClass()->getId(),
283
                        $OrderDetail->getQuantity()
284
                    )->save();
285
                } else {
286
                    log_info($app->trans('cart.product.delete'), array($id));
287
                    $app->addRequestError('cart.product.delete');
288
                }
289
            } catch (CartException $e) {
290
                log_info($e->getMessage(), array($id));
291
                $app->addRequestError($e->getMessage());
292
            }
293
        }
294
295
        $event = new EventArgs(
296
            array(
297
                'Order' => $Order,
298
                'Customer' => $Customer,
299
            ),
300
            $request
301
        );
302
        $this->eventDispatcher->dispatch(EccubeEvents::FRONT_MYPAGE_MYPAGE_ORDER_COMPLETE, $event);
303
304
        if ($event->getResponse() !== null) {
305
            return $event->getResponse();
306
        }
307
308
        log_info('再注文完了', array($id));
309
310
        return $app->redirect($app->url('cart'));
311
    }
312
313
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
introduced by
Doc comment for parameter "$request" missing
Loading history...
314
     * お気に入り商品を表示する.
315
     *
316
     * @Route("/mypage/favorite", name="mypage_favorite")
317
     * @Template("Mypage/favorite.twig")
318
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
319 2
    public function favorite(Application $app, Request $request)
320
    {
321 2
        if ($this->BaseInfo->getOptionFavoriteProduct() == Constant::DISABLED) {
322
            throw new NotFoundHttpException();
323
        }
324 2
        $Customer = $app->user();
325
326
        // paginator
327 2
        $qb = $this->customerFavoriteProductRepository->getQueryBuilderByCustomer($Customer);
328
329 2
        $event = new EventArgs(
330
            array(
331 2
                'qb' => $qb,
332 2
                'Customer' => $Customer,
333
            ),
334 2
            $request
335
        );
336 2
        $this->eventDispatcher->dispatch(EccubeEvents::FRONT_MYPAGE_MYPAGE_FAVORITE_SEARCH, $event);
337
338 2
        $pagination = $app['paginator']()->paginate(
339 2
            $qb,
340 2
            $request->get('pageno', 1),
341 2
            $this->appConfig['search_pmax'],
342 2
            array('wrap-queries' => true)
343
        );
344
345
        return [
346 2
            'pagination' => $pagination,
347
        ];
348
    }
349
350
    /**
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...
351
     * お気に入り商品を削除する.
352
     *
353
     * @Route("/mypage/favorite/{id}/delete", name="mypage_favorite_delete", requirements={"id" = "\d+"})
354
     * @Method("DELETE")
355
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
356 1
    public function delete(Application $app, Request $request, $id)
357
    {
358 1
        $this->isTokenValid($app);
359
360 1
        $Customer = $app->user();
361
362 1
        $Product = $this->productRepository->find($id);
363
364 1
        $event = new EventArgs(
365
            array(
366 1
                'Customer' => $Customer,
367 1
                'Product' => $Product,
368 1
            ), $request
369
        );
370 1
        $this->eventDispatcher->dispatch(EccubeEvents::FRONT_MYPAGE_MYPAGE_DELETE_INITIALIZE, $event);
371
372 1
        if ($Product) {
373
            log_info('お気に入り商品削除開始');
374
375
            $this->customerFavoriteProductRepository->deleteFavorite($Customer, $Product);
376
377
            $event = new EventArgs(
378
                array(
379
                    'Customer' => $Customer,
380
                    'Product' => $Product,
381
                ), $request
382
            );
383
            $this->eventDispatcher->dispatch(EccubeEvents::FRONT_MYPAGE_MYPAGE_DELETE_COMPLETE, $event);
384
385
            log_info('お気に入り商品削除完了');
386
        }
387
388 1
        return $app->redirect($app->url('mypage_favorite'));
389
    }
390
}
391