Completed
Pull Request — master (#1746)
by Kentaro
109:00 queued 102:48
created

OrderController   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 332
Duplicated Lines 20.18 %

Coupling/Cohesion

Components 0
Dependencies 6

Test Coverage

Coverage 94.3%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 29
c 1
b 0
f 0
lcom 0
cbo 6
dl 67
loc 332
ccs 182
cts 193
cp 0.943
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
D index() 40 140 15
B delete() 0 39 4
B exportOrder() 27 62 4
B exportShipping() 0 69 6

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
 * 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\Admin\Order;
26
27
use Eccube\Application;
28
use Eccube\Common\Constant;
29
use Eccube\Controller\AbstractController;
30
use Eccube\Entity\Master\CsvType;
31
use Eccube\Event\EccubeEvents;
32
use Eccube\Event\EventArgs;
33
use Symfony\Component\HttpFoundation\Request;
34
use Symfony\Component\HttpFoundation\StreamedResponse;
35
36
class OrderController extends AbstractController
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
37
{
38
39 10
    public function index(Application $app, Request $request, $page_no = null)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
40
    {
41
42 8
        $session = $request->getSession();
43
44 8
        $builder = $app['form.factory']
45 8
            ->createBuilder('admin_search_order');
46
47 8
        $event = new EventArgs(
48
            array(
49 8
                'builder' => $builder,
50 8
            ),
51
            $request
52 8
        );
53 8
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_INDEX_INITIALIZE, $event);
54
55 8
        $searchForm = $builder->getForm();
56
57 8
        $pagination = array();
58
59 8
        $disps = $app['eccube.repository.master.disp']->findAll();
60 8
        $pageMaxis = $app['eccube.repository.master.page_max']->findAll();
61 8
        $page_count = $app['config']['default_page_count'];
62 10
        $page_status = null;
63 8
        $active = false;
64
65 10
        if ('POST' === $request->getMethod()) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
66
67 6
            $searchForm->handleRequest($request);
68
69 6 View Code Duplication
            if ($searchForm->isValid()) {
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...
70 6
                $searchData = $searchForm->getData();
71
72
                // paginator
73 6
                $qb = $app['eccube.repository.order']->getQueryBuilderBySearchDataForAdmin($searchData);
74
75 6
                $event = new EventArgs(
76
                    array(
77 6
                        'form' => $searchForm,
78 6
                        'qb' => $qb,
79 6
                    ),
80
                    $request
81 6
                );
82 6
                $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_INDEX_SEARCH, $event);
83
84 6
                $page_no = 1;
85 6
                $pagination = $app['paginator']()->paginate(
86 6
                    $qb,
87 6
                    $page_no,
88
                    $page_count
89 6
                );
90
91
                // sessionのデータ保持
92 6
                $session->set('eccube.admin.order.search', $searchData);
93 6
                $session->set('eccube.admin.order.search.page_no', $page_no);
94 6
            }
95 6
        } else {
96 4
            if (is_null($page_no) && $request->get('resume') != Constant::ENABLED) {
97
                // sessionを削除
98 2
                $session->remove('eccube.admin.order.search');
99 2
                $session->remove('eccube.admin.order.search.page_no');
100 2
            } else {
101
                // pagingなどの処理
102 2
                $searchData = $session->get('eccube.admin.order.search');
103 2
                if (is_null($page_no)) {
104
                    $page_no = intval($session->get('eccube.admin.order.search.page_no'));
105
                } else {
106 2
                    $session->set('eccube.admin.order.search.page_no', $page_no);
107
                }
108
109 2
                if (!is_null($searchData)) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
110
111
                    // 公開ステータス
112 2
                    $status = $request->get('status');
113 2
                    if (!empty($status)) {
114
                        if ($status != $app['config']['admin_product_stock_status']) {
115
                            $searchData['status']->clear();
116
                            $searchData['status']->add($status);
117
                        } else {
118
                            $searchData['stock_status'] = $app['config']['disabled'];
119
                        }
120
                        $page_status = $status;
121
                    }
122
                    // 表示件数
123 2
                    $pcount = $request->get('page_count');
124
125 2
                    $page_count = empty($pcount) ? $page_count : $pcount;
126
127 2
                    $qb = $app['eccube.repository.order']->getQueryBuilderBySearchDataForAdmin($searchData);
128
129 2
                    $event = new EventArgs(
130
                        array(
131 2
                            'form' => $searchForm,
132 2
                            'qb' => $qb,
133 2
                        ),
134
                        $request
135 2
                    );
136 2
                    $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_INDEX_SEARCH, $event);
137
138 2
                    $pagination = $app['paginator']()->paginate(
139 2
                        $qb,
140 2
                        $page_no,
141
                        $page_count
142 2
                    );
143
144
                    // セッションから検索条件を復元
145 2
                    if (!empty($searchData['status'])) {
146 2
                        $searchData['status'] = $app['eccube.repository.master.order_status']->find($searchData['status']);
147 2
                    }
148 2 View Code Duplication
                    if (count($searchData['sex']) > 0) {
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...
149 2
                        $sex_ids = array();
150 2
                        foreach ($searchData['sex'] as $Sex) {
151 2
                            $sex_ids[] = $Sex->getId();
152 2
                        }
153 2
                        $searchData['sex'] = $app['eccube.repository.master.sex']->findBy(array('id' => $sex_ids));
154 2
                    }
155 2 View Code Duplication
                    if (count($searchData['payment']) > 0) {
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...
156 2
                        $payment_ids = array();
157 2
                        foreach ($searchData['payment'] as $Payment) {
158 2
                            $payment_ids[] = $Payment->getId();
159 2
                        }
160 2
                        $searchData['payment'] = $app['eccube.repository.payment']->findBy(array('id' => $payment_ids));
161 2
                    }
162 2
                    $searchForm->setData($searchData);
163 2
                }
164
            }
165
        }
166
167 8
        return $app->render('Order/index.twig', array(
168 8
            'searchForm' => $searchForm->createView(),
169 8
            'pagination' => $pagination,
170 8
            'disps' => $disps,
171 8
            'pageMaxis' => $pageMaxis,
172 8
            'page_no' => $page_no,
173 8
            'page_status' => $page_status,
174 8
            'page_count' => $page_count,
175 8
            'active' => $active,
176 8
        ));
177
178
    }
179
180 2
    public function delete(Application $app, Request $request, $id)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
181
    {
182 2
        $this->isTokenValid($app);
183 2
        $session = $request->getSession();
184 2
        $page_no = intval($session->get('eccube.admin.order.search.page_no'));
185 2
        $page_no = $page_no ? $page_no : Constant::ENABLED;
186
187 2
        $Order = $app['orm.em']->getRepository('Eccube\Entity\Order')
188 2
            ->find($id);
189
190 2
        if (!$Order) {
191
            $app->deleteMessage();
192
            return $app->redirect($app->url('admin_order_page', array('page_no' => $page_no)).'?resume='.Constant::ENABLED);
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
193
        }
194
195 2
        $Order->setDelFlg(Constant::ENABLED);
196
197 2
        $app['orm.em']->persist($Order);
198 2
        $app['orm.em']->flush();
199
200 2
        $Customer = $Order->getCustomer();
201 2
        if ($Customer) {
202
            // 会員の場合、購入回数、購入金額などを更新
203 2
            $app['eccube.repository.customer']->updateBuyData($app, $Customer, $Order->getOrderStatus()->getId());
204 2
        }
205
206 2
        $event = new EventArgs(
207
            array(
208 2
                'Order' => $Order,
209 2
                'Customer' => $Customer,
210 2
            ),
211
            $request
212 2
        );
213 2
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_DELETE_COMPLETE, $event);
214
215 2
        $app->addSuccess('admin.order.delete.complete', 'admin');
216
217 2
        return $app->redirect($app->url('admin_order_page', array('page_no' => $page_no)).'?resume='.Constant::ENABLED);
218
    }
219
220
221
    /**
222
     * 受注CSVの出力.
223
     *
224
     * @param Application $app
225
     * @param Request $request
0 ignored issues
show
introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
226
     * @return StreamedResponse
227
     */
228 1
    public function exportOrder(Application $app, Request $request)
229
    {
230
231
        // タイムアウトを無効にする.
232 1
        set_time_limit(0);
233
234
        // sql loggerを無効にする.
235 1
        $em = $app['orm.em'];
236 1
        $em->getConfiguration()->setSQLLogger(null);
237
238 1
        $response = new StreamedResponse();
239
        $response->setCallback(function () use ($app, $request) {
240
241
            // CSV種別を元に初期化.
242 1
            $app['eccube.service.csv.export']->initCsvType(CsvType::CSV_TYPE_ORDER);
243
244
            // ヘッダ行の出力.
245 1
            $app['eccube.service.csv.export']->exportHeader();
246
247
            // 受注データ検索用のクエリビルダを取得.
248 1
            $qb = $app['eccube.service.csv.export']
249 1
                ->getOrderQueryBuilder($request);
250
251
            // データ行の出力.
252 1
            $app['eccube.service.csv.export']->setExportQueryBuilder($qb);
253 View Code Duplication
            $app['eccube.service.csv.export']->exportData(function ($entity, $csvService) {
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...
254
255 1
                $Csvs = $csvService->getCsvs();
256
257 1
                $Order = $entity;
258 1
                $OrderDetails = $Order->getOrderDetails();
259
260 1
                foreach ($OrderDetails as $OrderDetail) {
261 1
                    $row = array();
262
263
                    // CSV出力項目と合致するデータを取得.
264 1
                    foreach ($Csvs as $Csv) {
265
                        // 受注データを検索.
266 1
                        $data = $csvService->getData($Csv, $Order);
267 1
                        if (is_null($data)) {
268
                            // 受注データにない場合は, 受注明細を検索.
269 1
                            $data = $csvService->getData($Csv, $OrderDetail);
270 1
                        }
271 1
                        $row[] = $data;
272
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
273 1
                    }
274
275
                    //$row[] = number_format(memory_get_usage(true));
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
276
                    // 出力.
277 1
                    $csvService->fputcsv($row);
278 1
                }
279 1
            });
280 1
        });
281
282 1
        $now = new \DateTime();
283 1
        $filename = 'order_' . $now->format('YmdHis') . '.csv';
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
284 1
        $response->headers->set('Content-Type', 'application/octet-stream');
285 1
        $response->headers->set('Content-Disposition', 'attachment; filename=' . $filename);
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
286 1
        $response->send();
287
288 1
        return $response;
289
    }
290
291
    /**
292
     * 配送CSVの出力.
293
     *
294
     * @param Application $app
295
     * @param Request $request
0 ignored issues
show
introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
296
     * @return StreamedResponse
297
     */
298 3
    public function exportShipping(Application $app, Request $request)
299
    {
300
        // タイムアウトを無効にする.
301 1
        set_time_limit(0);
302
303
        // sql loggerを無効にする.
304 1
        $em = $app['orm.em'];
305 1
        $em->getConfiguration()->setSQLLogger(null);
306
307 1
        $response = new StreamedResponse();
308
        $response->setCallback(function () use ($app, $request) {
309
310
            // CSV種別を元に初期化.
311 1
            $app['eccube.service.csv.export']->initCsvType(CsvType::CSV_TYPE_SHIPPING);
312
313
            // ヘッダ行の出力.
314 1
            $app['eccube.service.csv.export']->exportHeader();
315
316
            // 受注データ検索用のクエリビルダを取得.
317 1
            $qb = $app['eccube.service.csv.export']
318 1
                ->getOrderQueryBuilder($request);
319
320
            // データ行の出力.
321 1
            $app['eccube.service.csv.export']->setExportQueryBuilder($qb);
322 3
            $app['eccube.service.csv.export']->exportData(function ($entity, $csvService) {
323
324 1
                $Csvs = $csvService->getCsvs();
325
326
                /** @var $Order \Eccube\Entity\Order */
327 1
                $Order = $entity;
328
                /** @var $Shippings \Eccube\Entity\Shipping[] */
329 1
                $Shippings = $Order->getShippings();
330
331 1
                foreach ($Shippings as $Shipping) {
332
                    /** @var $ShipmentItems \Eccube\Entity\ShipmentItem */
333 1
                    $ShipmentItems = $Shipping->getShipmentItems();
334 1
                    foreach ($ShipmentItems as $ShipmentItem) {
335 1
                        $row = array();
336
337
                        // CSV出力項目と合致するデータを取得.
338 3
                        foreach ($Csvs as $Csv) {
339
                            // 受注データを検索.
340 1
                            $data = $csvService->getData($Csv, $Order);
341 1
                            if (is_null($data)) {
342
                                // 配送情報を検索.
343 1
                                $data = $csvService->getData($Csv, $Shipping);
344 1
                            }
345 1
                            if (is_null($data)) {
346
                                // 配送商品を検索.
347 1
                                $data = $csvService->getData($Csv, $ShipmentItem);
348 1
                            }
349 1
                            $row[] = $data;
350 1
                        }
351
                        //$row[] = number_format(memory_get_usage(true));
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
352
                        // 出力.
353 1
                        $csvService->fputcsv($row);
354 1
                    }
355 1
                }
356 1
            });
357 1
        });
358
359 1
        $now = new \DateTime();
360 1
        $filename = 'shipping_' . $now->format('YmdHis') . '.csv';
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
361 1
        $response->headers->set('Content-Type', 'application/octet-stream');
362 1
        $response->headers->set('Content-Disposition', 'attachment; filename=' . $filename);
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
363 1
        $response->send();
364
365 1
        return $response;
366
    }
367
}
368