Failed Conditions
Push — master ( 36ccc8...f8bd2f )
by Kentaro
34:47
created

OrderController::exportOrder()   B

Complexity

Conditions 4
Paths 1

Size

Total Lines 62
Code Lines 29

Duplication

Lines 27
Ratio 43.55 %

Code Coverage

Tests 2
CRAP Score 4

Importance

Changes 2
Bugs 0 Features 1
Metric Value
dl 27
loc 62
ccs 2
cts 2
cp 1
rs 8.9167
c 2
b 0
f 1
cc 4
eloc 29
nc 1
nop 2
crap 4

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 4
{
38
39
    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
        $session = $request->getSession();
43
44
        $builder = $app['form.factory']
45
            ->createBuilder('admin_search_order');
46 4
47
        $event = new EventArgs(
48
            array(
49
                'builder' => $builder,
50
            ),
51 4
            $request
52 4
        );
53
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_INDEX_INITIALIZE, $event);
54
55
        $searchForm = $builder->getForm();
56
57
        $pagination = array();
58
59
        $disps = $app['eccube.repository.master.disp']->findAll();
60
        $pageMaxis = $app['eccube.repository.master.page_max']->findAll();
61
        $page_count = $app['config']['default_page_count'];
62
        $page_status = null;
63 4
        $active = false;
64
65
        if ('POST' === $request->getMethod()) {
66
67
            $searchForm->handleRequest($request);
68
69 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
                $searchData = $searchForm->getData();
71
72
                // paginator
73
                $qb = $app['eccube.repository.order']->getQueryBuilderBySearchDataForAdmin($searchData);
74
75
                $event = new EventArgs(
76
                    array(
77
                        'form' => $searchForm,
78
                        'qb' => $qb,
79
                    ),
80
                    $request
81
                );
82
                $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_INDEX_SEARCH, $event);
83
84 1
                $page_no = 1;
85
                $pagination = $app['paginator']()->paginate(
86
                    $qb,
87
                    $page_no,
88
                    $page_count
89
                );
90
91
                // sessionのデータ保持
92
                $session->set('eccube.admin.order.search', $searchData);
93
            }
94
        } else {
95
            if (is_null($page_no)) {
96 1
                // sessionを削除
97
                $session->remove('eccube.admin.order.search');
98
            } else {
99
                // pagingなどの処理
100
                $searchData = $session->get('eccube.admin.order.search');
101
                if (!is_null($searchData)) {
102
103
                    // 公開ステータス
104
                    $status = $request->get('status');
105
                    if (!empty($status)) {
106 1
                        if ($status != $app['config']['admin_product_stock_status']) {
107
                            $searchData['status']->clear();
108
                            $searchData['status']->add($status);
109
                        } else {
110 1
                            $searchData['stock_status'] = $app['config']['disabled'];
111 1
                        }
112
                        $page_status = $status;
113
                    }
114
                    // 表示件数
115
                    $pcount = $request->get('page_count');
116
117 1
                    $page_count = empty($pcount) ? $page_count : $pcount;
118 1
119
                    $qb = $app['eccube.repository.order']->getQueryBuilderBySearchDataForAdmin($searchData);
120
121
                    $event = new EventArgs(
122
                        array(
123
                            'form' => $searchForm,
124
                            'qb' => $qb,
125 1
                        ),
126 4
                        $request
127
                    );
128 4
                    $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_INDEX_SEARCH, $event);
129 4
130
                    $pagination = $app['paginator']()->paginate(
131
                        $qb,
132
                        $page_no,
133
                        $page_count
134
                    );
135
136
                    // セッションから検索条件を復元
137
                    if (!empty($searchData['status'])) {
138
                        $searchData['status'] = $app['eccube.repository.master.order_status']->find($searchData['status']);
139 4
                    }
140 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...
141 1
                        $sex_ids = array();
142
                        foreach ($searchData['sex'] as $Sex) {
143
                            $sex_ids[] = $Sex->getId();
144
                        }
145
                        $searchData['sex'] = $app['eccube.repository.master.sex']->findBy(array('id' => $sex_ids));
146
                    }
147 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...
148 1
                        $payment_ids = array();
149
                        foreach ($searchData['payment'] as $Payment) {
150
                            $payment_ids[] = $Payment->getId();
151
                        }
152
                        $searchData['payment'] = $app['eccube.repository.payment']->findBy(array('id' => $payment_ids));
153
                    }
154
                    $searchForm->setData($searchData);
155
                }
156
            }
157
        }
158 1
159
        return $app->render('Order/index.twig', array(
160
            'searchForm' => $searchForm->createView(),
161
            'pagination' => $pagination,
162
            'disps' => $disps,
163
            'pageMaxis' => $pageMaxis,
164
            'page_no' => $page_no,
165
            'page_status' => $page_status,
166 1
            'page_count' => $page_count,
167
            'active' => $active,
168
        ));
169
170
    }
171
172
    public function delete(Application $app, Request $request, $id)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
173
    {
174
        $this->isTokenValid($app);
175
176 1
        $Order = $app['orm.em']->getRepository('Eccube\Entity\Order')
177
            ->find($id);
178
179
        if (!$Order) {
180
            $app->deleteMessage();
181
            return $app->redirect($app->url('admin_order'));
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
182
        }
183
184
        $Order->setDelFlg(Constant::ENABLED);
185
186
        $app['orm.em']->persist($Order);
187
        $app['orm.em']->flush();
188
189
        $Customer = $Order->getCustomer();
190
        if ($Customer) {
191
            // 会員の場合、購入回数、購入金額などを更新
192
            $app['eccube.repository.customer']->updateBuyData($app, $Customer, $Order->getOrderStatus()->getId());
193
        }
194
195
        $event = new EventArgs(
196
            array(
197
                'Order' => $Order,
198
                'Customer' => $Customer,
199
            ),
200
            $request
201
        );
202
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_DELETE_COMPLETE, $event);
203
204
        $app->addSuccess('admin.order.delete.complete', 'admin');
205
206
        return $app->redirect($app->url('admin_order'));
207
    }
208
209
210
    /**
211
     * 受注CSVの出力.
212
     *
213
     * @param Application $app
214
     * @param Request $request
0 ignored issues
show
introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
215
     * @return StreamedResponse
216
     */
217
    public function exportOrder(Application $app, Request $request)
218
    {
219
220
        // タイムアウトを無効にする.
221
        set_time_limit(0);
222
223
        // sql loggerを無効にする.
224
        $em = $app['orm.em'];
225
        $em->getConfiguration()->setSQLLogger(null);
226
227
        $response = new StreamedResponse();
228
        $response->setCallback(function () use ($app, $request) {
229
230
            // CSV種別を元に初期化.
231
            $app['eccube.service.csv.export']->initCsvType(CsvType::CSV_TYPE_ORDER);
232
233
            // ヘッダ行の出力.
234
            $app['eccube.service.csv.export']->exportHeader();
235
236 1
            // 受注データ検索用のクエリビルダを取得.
237 1
            $qb = $app['eccube.service.csv.export']
238
                ->getOrderQueryBuilder($request);
239
240
            // データ行の出力.
241
            $app['eccube.service.csv.export']->setExportQueryBuilder($qb);
242 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...
243
244
                $Csvs = $csvService->getCsvs();
245
246
                $Order = $entity;
247
                $OrderDetails = $Order->getOrderDetails();
248
249
                foreach ($OrderDetails as $OrderDetail) {
250
                    $row = array();
251
252
                    // CSV出力項目と合致するデータを取得.
253
                    foreach ($Csvs as $Csv) {
254
                        // 受注データを検索.
255
                        $data = $csvService->getData($Csv, $Order);
256
                        if (is_null($data)) {
257
                            // 受注データにない場合は, 受注明細を検索.
258
                            $data = $csvService->getData($Csv, $OrderDetail);
259
                        }
260
                        $row[] = $data;
261
262
                    }
263
264
                    //$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...
265
                    // 出力.
266
                    $csvService->fputcsv($row);
267
                }
268
            });
269
        });
270
271
        $now = new \DateTime();
272
        $filename = 'order_' . $now->format('YmdHis') . '.csv';
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
273
        $response->headers->set('Content-Type', 'application/octet-stream');
274
        $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...
275
        $response->send();
276
277
        return $response;
278
    }
279
280
    /**
281
     * 配送CSVの出力.
282
     *
283
     * @param Application $app
284
     * @param Request $request
0 ignored issues
show
introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
285
     * @return StreamedResponse
286
     */
287
    public function exportShipping(Application $app, Request $request)
288
    {
289
        // タイムアウトを無効にする.
290
        set_time_limit(0);
291
292
        // sql loggerを無効にする.
293
        $em = $app['orm.em'];
294
        $em->getConfiguration()->setSQLLogger(null);
295
296
        $response = new StreamedResponse();
297
        $response->setCallback(function () use ($app, $request) {
298
299
            // CSV種別を元に初期化.
300
            $app['eccube.service.csv.export']->initCsvType(CsvType::CSV_TYPE_SHIPPING);
301
302
            // ヘッダ行の出力.
303
            $app['eccube.service.csv.export']->exportHeader();
304
305
            // 受注データ検索用のクエリビルダを取得.
306
            $qb = $app['eccube.service.csv.export']
307
                ->getOrderQueryBuilder($request);
308
309
            // データ行の出力.
310
            $app['eccube.service.csv.export']->setExportQueryBuilder($qb);
311
            $app['eccube.service.csv.export']->exportData(function ($entity, $csvService) {
312
313 1
                $Csvs = $csvService->getCsvs();
314 1
315
                /** @var $Order \Eccube\Entity\Order */
316
                $Order = $entity;
317
                /** @var $Shippings \Eccube\Entity\Shipping[] */
318
                $Shippings = $Order->getShippings();
319
320
                foreach ($Shippings as $Shipping) {
321
                    /** @var $ShipmentItems \Eccube\Entity\ShipmentItem */
322
                    $ShipmentItems = $Shipping->getShipmentItems();
323
                    foreach ($ShipmentItems as $ShipmentItem) {
324
                        $row = array();
325
326
                        // CSV出力項目と合致するデータを取得.
327
                        foreach ($Csvs as $Csv) {
328
                            // 受注データを検索.
329
                            $data = $csvService->getData($Csv, $Order);
330
                            if (is_null($data)) {
331
                                // 配送情報を検索.
332
                                $data = $csvService->getData($Csv, $Shipping);
333
                            }
334
                            if (is_null($data)) {
335
                                // 配送商品を検索.
336
                                $data = $csvService->getData($Csv, $ShipmentItem);
337
                            }
338
                            $row[] = $data;
339
                        }
340
                        //$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...
341
                        // 出力.
342
                        $csvService->fputcsv($row);
343
                    }
344
                }
345
            });
346
        });
347
348
        $now = new \DateTime();
349
        $filename = 'shipping_' . $now->format('YmdHis') . '.csv';
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
350
        $response->headers->set('Content-Type', 'application/octet-stream');
351
        $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...
352
        $response->send();
353
354
        return $response;
355
    }
356
}
357