Failed Conditions
Pull Request — experimental/3.1 (#2273)
by
unknown
39:32
created

MypageController::history()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 32
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 18
nc 2
nop 3
dl 0
loc 32
ccs 16
cts 16
cp 1
crap 2
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 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 Eccube\Form\Type\Front\CustomerLoginType;
34
use Symfony\Component\HttpFoundation\Request;
35
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
36
37
class MypageController extends AbstractController
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
38
{
39
    /**
40
     * ログイン画面.
41
     *
42
     * @param Application $app
43
     * @param Request $request
0 ignored issues
show
introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
44
     * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
45
     */
46 4
    public function login(Application $app, Request $request)
47
    {
48 4
        if ($app->isGranted('IS_AUTHENTICATED_FULLY')) {
49 2
            log_info('認証済のためログイン処理をスキップ');
50
51 2
            return $app->redirect($app->url('mypage'));
52
        }
53
54
        /* @var $form \Symfony\Component\Form\FormInterface */
55 2
        $builder = $app['form.factory']
56 2
            ->createNamedBuilder('', CustomerLoginType::class);
57
58 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...
59
            $Customer = $app->user();
60
            if ($Customer) {
61
                $builder->get('login_email')->setData($Customer->getEmail());
62
            }
63
        }
64
65 2
        $event = new EventArgs(
66
            array(
67 2
                'builder' => $builder,
68
            ),
69
            $request
70
        );
71 2
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_MYPAGE_MYPAGE_LOGIN_INITIALIZE, $event);
72
73 2
        $form = $builder->getForm();
74
75 2
        return $app->render('Mypage/login.twig', array(
76 2
            'error' => $app['security.last_error']($request),
77 2
            'form' => $form->createView(),
78
        ));
79
    }
80
81
    /**
82
     * マイページ
83
     *
84
     * @param Application $app
85
     * @param Request $request
0 ignored issues
show
introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
86
     * @return \Symfony\Component\HttpFoundation\Response
87
     */
88 2
    public function index(Application $app, Request $request)
89
    {
90 2
        $Customer = $app['user'];
91
92
        /* @var $softDeleteFilter \Eccube\Doctrine\Filter\SoftDeleteFilter */
93 2
        $softDeleteFilter = $app['orm.em']->getFilters()->getFilter('soft_delete');
94 2
        $softDeleteFilter->setExcludes(array(
95 2
            'Eccube\Entity\ProductClass',
96
        ));
97
98
        // 購入処理中/決済処理中ステータスの受注を非表示にする.
99 2
        $app['orm.em']
100 2
            ->getFilters()
101 2
            ->enable('incomplete_order_status_hidden');
102
103
        // paginator
104 2
        $qb = $app['eccube.repository.order']->getQueryBuilderByCustomer($Customer);
105
106 2
        $event = new EventArgs(
107
            array(
108 2
                'qb' => $qb,
109 2
                'Customer' => $Customer,
110
            ),
111
            $request
112
        );
113 2
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_MYPAGE_MYPAGE_INDEX_SEARCH, $event);
114
115 2
        $pagination = $app['paginator']()->paginate(
116
            $qb,
117 2
            $request->get('pageno', 1),
118 2
            $app['config']['search_pmax']
119
        );
120
121 2
        return $app->render('Mypage/index.twig', array(
122 2
            'pagination' => $pagination,
123
        ));
124
    }
125
126
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$id" missing
Loading history...
127
     * 購入履歴詳細を表示する.
128
     *
129
     * @param Application $app
130
     * @param Request $request
0 ignored issues
show
introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
131
     * @param $id
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
132
     * @return \Symfony\Component\HttpFoundation\Response
133
     */
134 4
    public function history(Application $app, Request $request, $id)
135
    {
136
        /* @var $softDeleteFilter \Eccube\Doctrine\Filter\SoftDeleteFilter */
137 4
        $softDeleteFilter = $app['orm.em']->getFilters()->getFilter('soft_delete');
138 4
        $softDeleteFilter->setExcludes(array(
139 4
            'Eccube\Entity\ProductClass',
140
        ));
141
142 4
        $app['orm.em']->getFilters()->enable('incomplete_order_status_hidden');
143 4
        $Order = $app['eccube.repository.order']->findOneBy(array(
144 4
            'id' => $id,
145 4
            'Customer' => $app->user(),
146
        ));
147
        
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
148 4
        $event = new EventArgs(
149
            array(
150 4
                'Order' => $Order,
151
            ),
152
            $request
153
        );
154 4
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_MYPAGE_MYPAGE_HISTORY_INITIALIZE, $event);
155
156 4
        $Order = $event->getArgument('Order');
157
158 4
        if (!$Order) {
159 2
            throw new NotFoundHttpException();
160
        }
161
162 2
        return $app->render('Mypage/history.twig', array(
163 2
            'Order' => $Order,
164
        ));
165
    }
166
167
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$id" missing
Loading history...
168
     * 再購入を行う.
169
     *
170
     * @param Application $app
171
     * @param Request $request
0 ignored issues
show
introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
172
     * @param $id
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
173
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
174
     */
175 2
    public function order(Application $app, Request $request, $id)
176
    {
177 2
        $this->isTokenValid($app);
178
179 2
        log_info('再注文開始', array($id));
180
181 2
        $Customer = $app->user();
182
183
        /* @var $Order \Eccube\Entity\Order */
184 2
        $Order = $app['eccube.repository.order']->findOneBy(array(
185 2
            'id' => $id,
186 2
            'Customer' => $Customer,
187
        ));
188
189 2
        $event = new EventArgs(
190
            array(
191 2
                'Order' => $Order,
192 2
                'Customer' => $Customer,
193
            ),
194
            $request
195
        );
196 2
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_MYPAGE_MYPAGE_ORDER_INITIALIZE, $event);
197
198 2
        if (!$Order) {
199
            log_info('対象の注文が見つかりません', array($id));
200
            throw new NotFoundHttpException();
201
        }
202
203 2
        foreach ($Order->getOrderDetails() as $OrderDetail) {
204
            try {
205 2
                if ($OrderDetail->getProduct() &&
206 2
                    $OrderDetail->getProductClass()) {
207 2
                    $CartItem = $app['eccube.service.cart']->generateCartItem($OrderDetail->getProductClass());
208
                    $CartItem->setQuantity($OrderDetail->getQuantity());
209
210 2
                    $event = new EventArgs(
211
                        array(
212
                            'Order' => $Order,
213
                            'OrderDetail' => $OrderDetail,
214 2
                            'Customer' => $Customer,
215
                            'CartItem' => $CartItem,
216
                        ),
217
                        $request
218 2
                    );
219
                    $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_MYPAGE_MYPAGE_ORDER_ADD_CART, $event);
220 2
221 2
                    $app['eccube.service.cart']->addCartItem($CartItem)->save();
222
                } else {
223
                    log_info($app->trans('cart.product.delete'), array($id));
224
                    $app->addRequestError('cart.product.delete');
225 2
                }
226
            } catch (CartException $e) {
227 2
                log_info($e->getMessage(), array($id));
228
                $app->addRequestError($e->getMessage());
229
            }
230
        }
231 2
232
        $event = new EventArgs(
233 2
            array(
234
                'Order' => $Order,
235
                'Customer' => $Customer,
236
            ),
237
            $request
238
        );
239
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_MYPAGE_MYPAGE_ORDER_COMPLETE, $event);
240
241
        if ($event->getResponse() !== null) {
242
            return $event->getResponse();
243 3
        }
244
245 3
        log_info('再注文完了', array($id));
246
247 3
        return $app->redirect($app->url('cart'));
248 3
    }
249
250
    /**
251 3
     * お気に入り商品を表示する.
252
     *
253 3
     * @param Application $app
254
     * @param Request $request
0 ignored issues
show
introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
255 3
     * @return \Symfony\Component\HttpFoundation\Response
256 3
     */
257
    public function favorite(Application $app, Request $request)
258
    {
259
        $BaseInfo = $app['eccube.repository.base_info']->get();
260 3
261
        if ($BaseInfo->getOptionFavoriteProduct() == Constant::ENABLED) {
262 3
            $Customer = $app->user();
263
264 3
            // paginator
265 3
            $qb = $app['eccube.repository.customer_favorite_product']->getQueryBuilderByCustomer($Customer);
266 3
267
            $event = new EventArgs(
268
                array(
269 3
                    'qb' => $qb,
270 3
                    'Customer' => $Customer,
271
                ),
272
                $request
273
            );
274
            $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_MYPAGE_MYPAGE_FAVORITE_SEARCH, $event);
275
276
            $pagination = $app['paginator']()->paginate(
277
                $qb,
278
                $request->get('pageno', 1),
279
                $app['config']['search_pmax'],
280
                array('wrap-queries' => true)
281
            );
282
283
            return $app->render('Mypage/favorite.twig', array(
284
                'pagination' => $pagination,
285 2
            ));
286
        } else {
287 2
            throw new NotFoundHttpException();
288
        }
289 2
    }
290
291 2
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$id" missing
Loading history...
292
     * お気に入り商品を削除する.
293 2
     *
294
     * @param Application $app
295 2
     * @param Request $request
0 ignored issues
show
introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
296 2
     * @param $id
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
297
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
298
     */
299 2
    public function delete(Application $app, Request $request, $id)
300
    {
301 2
        $this->isTokenValid($app);
302 1
303
        $Customer = $app->user();
304 1
305
        $Product = $app['eccube.repository.product']->find($id);
306 1
307
        $event = new EventArgs(
308 1
            array(
309 1
                'Customer' => $Customer,
310
                'Product' => $Product,
311
            ), $request
312 1
        );
313
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_MYPAGE_MYPAGE_DELETE_INITIALIZE, $event);
314 1
315 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...
316
            log_info('お気に入り商品削除開始');
317 2
318
            $app['eccube.repository.customer_favorite_product']->deleteFavorite($Customer, $Product);
319
320
            $event = new EventArgs(
321
                array(
322
                    'Customer' => $Customer,
323
                    'Product' => $Product,
324
                ), $request
325
            );
326
            $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_MYPAGE_MYPAGE_DELETE_COMPLETE, $event);
327
328
            log_info('お気に入り商品削除完了');
329
        }
330
331
        return $app->redirect($app->url('mypage_favorite'));
332
    }
333
}
334