Completed
Push — master ( 475959...ea8330 )
by Kentaro
76:16
created

OrderController   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 282
Duplicated Lines 19.86 %

Coupling/Cohesion

Components 0
Dependencies 6

Test Coverage

Coverage 72.21%
Metric Value
wmc 26
lcom 0
cbo 6
dl 56
loc 282
ccs 26
cts 36
cp 0.7221
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
B delete() 0 26 3
B exportOrder() 27 62 4
B exportShipping() 0 69 6
D index() 29 103 13

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