Completed
Pull Request — master (#1922)
by chihiro
39:05
created

CustomerController::index()   C

Complexity

Conditions 11
Paths 21

Size

Total Lines 115
Code Lines 73

Duplication

Lines 34
Ratio 29.57 %

Code Coverage

Tests 40
CRAP Score 14.6151

Importance

Changes 0
Metric Value
cc 11
eloc 73
nc 21
nop 3
dl 34
loc 115
ccs 40
cts 58
cp 0.6897
crap 14.6151
rs 5.2653
c 0
b 0
f 0

How to fix   Long Method    Complexity   

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