CustomerController::export()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 69
Code Lines 34

Duplication

Lines 32
Ratio 46.38 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 34
nc 1
nop 2
dl 32
loc 69
ccs 0
cts 33
cp 0
crap 6
rs 9.2083
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
                $session->set('eccube.admin.customer.search', $searchData);
90 5
                $session->set('eccube.admin.customer.search.page_no', $page_no);
91
            }
92
        } else {
93 3
            if (is_null($page_no) && $request->get('resume') != Constant::ENABLED) {
94
                // sessionを削除
95 2
                $session->remove('eccube.admin.customer.search');
96 2
                $session->remove('eccube.admin.customer.search.page_no');
97
            } else {
98
                // pagingなどの処理
99 1
                $searchData = $session->get('eccube.admin.customer.search');
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
                if (!is_null($searchData)) {
106
                    // 表示件数
107
                    $pcount = $request->get('page_count');
108
                    $page_count = empty($pcount) ? $page_count : $pcount;
109
110
                    $qb = $app['eccube.repository.customer']->getQueryBuilderBySearchData($searchData);
111
112
                    $event = new EventArgs(
113
                        array(
114
                            'form' => $searchForm,
115
                            'qb' => $qb,
116
                        ),
117
                        $request
118
                    );
119
                    $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_CUSTOMER_INDEX_SEARCH, $event);
120
121
                    $pagination = $app['paginator']()->paginate(
122
                        $qb,
123
                        $page_no,
124
                        $page_count
125
                    );
126
127
                    // セッションから検索条件を復元
128 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...
129
                        $sex_ids = array();
130
                        foreach ($searchData['sex'] as $Sex) {
131
                            $sex_ids[] = $Sex->getId();
132
                        }
133
                        $searchData['sex'] = $app['eccube.repository.master.sex']->findBy(array('id' => $sex_ids));
134
                    }
135
136
                    if (!is_null($searchData['pref'])) {
137
                        $searchData['pref'] = $app['eccube.repository.master.pref']->find($searchData['pref']->getId());
138
                    }
139
                    $searchForm->setData($searchData);
140
                }
141
            }
142
        }
143 8
        return $app->render('Customer/index.twig', array(
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
144 8
            'searchForm' => $searchForm->createView(),
145 8
            'pagination' => $pagination,
146 8
            'pageMaxis' => $pageMaxis,
147 8
            'page_no' => $page_no,
148 8
            'page_count' => $page_count,
149 8
            'active' => $active,
150
        ));
151
    }
152
153 2
    public function resend(Application $app, Request $request, $id)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
154
    {
155 2
        $this->isTokenValid($app);
156
157 2
        $Customer = $app['orm.em']
158 2
            ->getRepository('Eccube\Entity\Customer')
159 2
            ->find($id);
160
161 2
        if (is_null($Customer)) {
162
            throw new NotFoundHttpException();
163
        }
164
165 2
        $activateUrl = $app->url('entry_activate', array('secret_key' => $Customer->getSecretKey()));
166
167
        // メール送信
168 2
        $app['eccube.service.mail']->sendAdminCustomerConfirmMail($Customer, $activateUrl);
169
170 2
        $event = new EventArgs(
171
            array(
172 2
                'Customer' => $Customer,
173 2
                'activateUrl' => $activateUrl,
174
            ),
175
            $request
176
        );
177 2
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_CUSTOMER_RESEND_COMPLETE, $event);
178
179 2
        $app->addSuccess('admin.customer.resend.complete', 'admin');
180
181 2
        return $app->redirect($app->url('admin_customer'));
182
    }
183
184 2
    public function delete(Application $app, Request $request, $id)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
185
    {
186 2
        $this->isTokenValid($app);
187
188 2
        log_info('会員削除開始', array($id));
189
190 2
        $session = $request->getSession();
191 2
        $page_no = intval($session->get('eccube.admin.customer.search.page_no'));
192 2
        $page_no = $page_no ? $page_no : Constant::ENABLED;
193
194 2
        $Customer = $app['orm.em']
195 2
            ->getRepository('Eccube\Entity\Customer')
196 2
            ->find($id);
197
198 2
        if (!$Customer) {
199
            $app->deleteMessage();
200
            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...
201
        }
202
203 2
        $Customer->setDelFlg(Constant::ENABLED);
204 2
        $app['orm.em']->persist($Customer);
205 2
        $app['orm.em']->flush();
206
207 2
        log_info('会員削除完了', array($id));
208
209 2
        $event = new EventArgs(
210
            array(
211 2
                'Customer' => $Customer,
212
            ),
213
            $request
214
        );
215 2
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_CUSTOMER_DELETE_COMPLETE, $event);
216
217 2
        $app->addSuccess('admin.customer.delete.complete', 'admin');
218
219 2
        return $app->redirect($app->url('admin_customer_page', array('page_no' => $page_no)).'?resume='.Constant::ENABLED);
220
    }
221
222
    /**
223
     * 会員CSVの出力.
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
    public function export(Application $app, Request $request)
229
    {
230
        // タイムアウトを無効にする.
231
        set_time_limit(0);
232
233
        // sql loggerを無効にする.
234
        $em = $app['orm.em'];
235
        $em->getConfiguration()->setSQLLogger(null);
236
237
        $response = new StreamedResponse();
238
        $response->setCallback(function () use ($app, $request) {
239
240
            // CSV種別を元に初期化.
241
            $app['eccube.service.csv.export']->initCsvType(CsvType::CSV_TYPE_CUSTOMER);
242
243
            // ヘッダ行の出力.
244
            $app['eccube.service.csv.export']->exportHeader();
245
246
            // 会員データ検索用のクエリビルダを取得.
247
            $qb = $app['eccube.service.csv.export']
248
                ->getCustomerQueryBuilder($request);
249
250
            // データ行の出力.
251
            $app['eccube.service.csv.export']->setExportQueryBuilder($qb);
252 View Code Duplication
            $app['eccube.service.csv.export']->exportData(function ($entity, $csvService) use ($app, $request) {
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...
253
254
                $Csvs = $csvService->getCsvs();
255
256
                /** @var $Customer \Eccube\Entity\Customer */
257
                $Customer = $entity;
258
259
                $ExportCsvRow = new \Eccube\Entity\ExportCsvRow();
260
261
                // CSV出力項目と合致するデータを取得.
262
                foreach ($Csvs as $Csv) {
263
                    // 会員データを検索.
264
                    $ExportCsvRow->setData($csvService->getData($Csv, $Customer));
265
266
                    $event = new EventArgs(
267
                        array(
268
                            'csvService' => $csvService,
269
                            'Csv' => $Csv,
270
                            'Customer' => $Customer,
271
                            'ExportCsvRow' => $ExportCsvRow,
272
                        ),
273
                        $request
274
                    );
275
                    $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_CUSTOMER_CSV_EXPORT, $event);
276
277
                    $ExportCsvRow->pushData();
278
                }
279
280
                //$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...
281
                // 出力.
282
                $csvService->fputcsv($ExportCsvRow->getRow());
283
            });
284
        });
285
286
        $now = new \DateTime();
287
        $filename = 'customer_' . $now->format('YmdHis') . '.csv';
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
288
        $response->headers->set('Content-Type', 'application/octet-stream');
289
        $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...
290
291
        $response->send();
292
293
        log_info("会員CSVファイル名", array($filename));
294
295
        return $response;
296
    }
297
}
298