Completed
Pull Request — master (#2113)
by chihiro
48:53
created

CustomerController::export()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 56
Code Lines 25

Duplication

Lines 19
Ratio 33.93 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 25
nc 1
nop 2
dl 19
loc 56
ccs 0
cts 26
cp 0
crap 6
rs 9.7251
c 0
b 0
f 0

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\Customer;
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
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
36
37
class CustomerController extends AbstractController
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
38
{
39 8
    public function index(Application $app, Request $request, $page_no = null)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
40
    {
41 8
        $session = $request->getSession();
42 8
        $pagination = array();
43 8
        $builder = $app['form.factory']
44 8
            ->createBuilder('admin_search_customer');
45
46 8
        $event = new EventArgs(
47
            array(
48 8
                'builder' => $builder,
49
            ),
50
            $request
51
        );
52 8
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_CUSTOMER_INDEX_INITIALIZE, $event);
53
54 8
        $searchForm = $builder->getForm();
55
56
        //アコーディオンの制御初期化( デフォルトでは閉じる )
57 8
        $active = false;
58
59 8
        $pageMaxis = $app['eccube.repository.master.page_max']->findAll();
60 8
        $page_count = $app['config']['default_page_count'];
61
62 8
        if ('POST' === $request->getMethod()) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
63
64 5
            $searchForm->handleRequest($request);
65
66 5 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...
67 5
                $searchData = $searchForm->getData();
68
69
                // paginator
70 5
                $qb = $app['eccube.repository.customer']->getQueryBuilderBySearchData($searchData);
71 5
                $page_no = 1;
72
73 5
                $event = new EventArgs(
74
                    array(
75 5
                        'form' => $searchForm,
76 5
                        'qb' => $qb,
77
                    ),
78
                    $request
79
                );
80 5
                $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_CUSTOMER_INDEX_SEARCH, $event);
81
82 5
                $pagination = $app['paginator']()->paginate(
83
                    $qb,
84
                    $page_no,
85
                    $page_count
86
                );
87
88
                // sessionに検索条件を保持.
89 5
                $viewData = \Eccube\Util\FormUtil::getViewData($searchForm);
90 5
                $session->set('eccube.admin.customer.search', $viewData);
91 5
                $session->set('eccube.admin.customer.search.page_no', $page_no);
92
            }
93
        } else {
94 3
            if (is_null($page_no) && $request->get('resume') != Constant::ENABLED) {
95
                // sessionを削除
96 2
                $session->remove('eccube.admin.customer.search');
97 2
                $session->remove('eccube.admin.customer.search.page_no');
98
            } else {
99
                // pagingなどの処理
100 1
                if (is_null($page_no)) {
101
                    $page_no = intval($session->get('eccube.admin.customer.search.page_no'));
102
                } else {
103 1
                    $session->set('eccube.admin.customer.search.page_no', $page_no);
104
                }
105 1
                $viewData = $session->get('eccube.admin.customer.search');
106 1
                if (!is_null($viewData)) {
107
                    // sessionに保持されている検索条件を復元.
108
                    $searchData = \Eccube\Util\FormUtil::submitAndGetData($searchForm, $viewData);
109
110
                    // 表示件数
111
                    $page_count = $request->get('page_count', $page_count);
112
113
                    $qb = $app['eccube.repository.customer']->getQueryBuilderBySearchData($searchData);
114
115
                    $event = new EventArgs(
116
                        array(
117
                            'form' => $searchForm,
118
                            'qb' => $qb,
119
                        ),
120
                        $request
121
                    );
122
                    $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_CUSTOMER_INDEX_SEARCH, $event);
123
124
                    $pagination = $app['paginator']()->paginate(
125
                        $qb,
126
                        $page_no,
127
                        $page_count
128
                    );
129
                }
130
            }
131
        }
132 8
        return $app->render('Customer/index.twig', array(
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
133 8
            'searchForm' => $searchForm->createView(),
134 8
            'pagination' => $pagination,
135 8
            'pageMaxis' => $pageMaxis,
136 8
            'page_no' => $page_no,
137 8
            'page_count' => $page_count,
138 8
            'active' => $active,
139
        ));
140
    }
141
142 2
    public function resend(Application $app, Request $request, $id)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
143
    {
144 2
        $this->isTokenValid($app);
145
146 2
        $Customer = $app['orm.em']
147 2
            ->getRepository('Eccube\Entity\Customer')
148 2
            ->find($id);
149
150 2
        if (is_null($Customer)) {
151
            throw new NotFoundHttpException();
152
        }
153
154 2
        $activateUrl = $app->url('entry_activate', array('secret_key' => $Customer->getSecretKey()));
155
156
        // メール送信
157 2
        $app['eccube.service.mail']->sendAdminCustomerConfirmMail($Customer, $activateUrl);
158
159 2
        $event = new EventArgs(
160
            array(
161 2
                'Customer' => $Customer,
162 2
                'activateUrl' => $activateUrl,
163
            ),
164
            $request
165
        );
166 2
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_CUSTOMER_RESEND_COMPLETE, $event);
167
168 2
        $app->addSuccess('admin.customer.resend.complete', 'admin');
169
170 2
        return $app->redirect($app->url('admin_customer'));
171
    }
172
173 2
    public function delete(Application $app, Request $request, $id)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
174
    {
175 2
        $this->isTokenValid($app);
176
177 2
        log_info('会員削除開始', array($id));
178
179 2
        $session = $request->getSession();
180 2
        $page_no = intval($session->get('eccube.admin.customer.search.page_no'));
181 2
        $page_no = $page_no ? $page_no : Constant::ENABLED;
182
183 2
        $Customer = $app['orm.em']
184 2
            ->getRepository('Eccube\Entity\Customer')
185 2
            ->find($id);
186
187 2
        if (!$Customer) {
188
            $app->deleteMessage();
189
            return $app->redirect($app->url('admin_customer_page', array('page_no' => $page_no)).'?resume='.Constant::ENABLED);
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
190
        }
191
192 2
        $Customer->setDelFlg(Constant::ENABLED);
193 2
        $app['orm.em']->persist($Customer);
194 2
        $app['orm.em']->flush();
195
196 2
        log_info('会員削除完了', array($id));
197
198 2
        $event = new EventArgs(
199
            array(
200 2
                'Customer' => $Customer,
201
            ),
202
            $request
203
        );
204 2
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_CUSTOMER_DELETE_COMPLETE, $event);
205
206 2
        $app->addSuccess('admin.customer.delete.complete', 'admin');
207
208 2
        return $app->redirect($app->url('admin_customer_page', array('page_no' => $page_no)).'?resume='.Constant::ENABLED);
209
    }
210
211
    /**
212
     * 会員CSVの出力.
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 export(Application $app, Request $request)
218
    {
219
        // タイムアウトを無効にする.
220
        set_time_limit(0);
221
222
        // sql loggerを無効にする.
223
        $em = $app['orm.em'];
224
        $em->getConfiguration()->setSQLLogger(null);
225
226
        $response = new StreamedResponse();
227
        $response->setCallback(function () use ($app, $request) {
228
229
            // CSV種別を元に初期化.
230
            $app['eccube.service.csv.export']->initCsvType(CsvType::CSV_TYPE_CUSTOMER);
231
232
            // ヘッダ行の出力.
233
            $app['eccube.service.csv.export']->exportHeader();
234
235
            // 会員データ検索用のクエリビルダを取得.
236
            $qb = $app['eccube.service.csv.export']
237
                ->getCustomerQueryBuilder($request);
238
239
            // データ行の出力.
240
            $app['eccube.service.csv.export']->setExportQueryBuilder($qb);
241 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...
242
243
                $Csvs = $csvService->getCsvs();
244
245
                /** @var $Customer \Eccube\Entity\Customer */
246
                $Customer = $entity;
247
248
                $row = array();
249
250
                // CSV出力項目と合致するデータを取得.
251
                foreach ($Csvs as $Csv) {
252
                    // 会員データを検索.
253
                    $row[] = $csvService->getData($Csv, $Customer);
254
                }
255
256
                //$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...
257
                // 出力.
258
                $csvService->fputcsv($row);
259
            });
260
        });
261
262
        $now = new \DateTime();
263
        $filename = 'customer_' . $now->format('YmdHis') . '.csv';
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
264
        $response->headers->set('Content-Type', 'application/octet-stream');
265
        $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...
266
267
        $response->send();
268
269
        log_info("会員CSVファイル名", array($filename));
270
271
        return $response;
272
    }
273
}
274