Failed Conditions
Push — master ( 5ac99f...ce75f7 )
by Kentaro
22s
created

CustomerController   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 242
Duplicated Lines 21.49 %

Coupling/Cohesion

Components 0
Dependencies 7

Test Coverage

Coverage 57.39%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 18
c 1
b 0
f 0
lcom 0
cbo 7
dl 52
loc 242
ccs 66
cts 115
cp 0.5739
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
C index() 33 113 11
B resend() 0 30 2
B delete() 0 33 3
A export() 19 54 2

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\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 4
    public function index(Application $app, Request $request, $page_no = null)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
40
    {
41 4
        $session = $request->getSession();
42 4
        $pagination = array();
43 4
        $builder = $app['form.factory']
44 4
            ->createBuilder('admin_search_customer');
45
46 4
        $event = new EventArgs(
47
            array(
48 4
                'builder' => $builder,
49
            ),
50
            $request
51
        );
52 4
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_CUSTOMER_INDEX_INITIALIZE, $event);
53
54 4
        $searchForm = $builder->getForm();
55
56
        //アコーディオンの制御初期化( デフォルトでは閉じる )
57 4
        $active = false;
58
59 4
        $pageMaxis = $app['eccube.repository.master.page_max']->findAll();
60 4
        $page_count = $app['config']['default_page_count'];
61
62 4
        if ('POST' === $request->getMethod()) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
63
64 2
            $searchForm->handleRequest($request);
65
66 2 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 2
                $searchData = $searchForm->getData();
68
69
                // paginator
70 2
                $qb = $app['eccube.repository.customer']->getQueryBuilderBySearchData($searchData);
71 2
                $page_no = 1;
72
73 2
                $event = new EventArgs(
74
                    array(
75 2
                        'form' => $searchForm,
76 2
                        'qb' => $qb,
77
                    ),
78
                    $request
79
                );
80 2
                $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_CUSTOMER_INDEX_SEARCH, $event);
81
82 2
                $pagination = $app['paginator']()->paginate(
83
                    $qb,
84
                    $page_no,
85
                    $page_count
86
                );
87
88
                // sessionのデータ保持
89 2
                $session->set('eccube.admin.customer.search', $searchData);
90 2
                $session->set('eccube.admin.customer.search.page_no', $page_no);
91
            }
92
        } else {
93 2
            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
                $searchData = $session->get('eccube.admin.customer.search');
100
                if (is_null($page_no)) {
101
                    $page_no = intval($session->get('eccube.admin.customer.search.page_no'));
102
                } else {
103
                    $session->set('eccube.admin.customer.search.page_no', $page_no);
104
                }
105
                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 4
        return $app->render('Customer/index.twig', array(
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
144 4
            'searchForm' => $searchForm->createView(),
145 4
            'pagination' => $pagination,
146 4
            'pageMaxis' => $pageMaxis,
147 4
            'page_no' => $page_no,
148 4
            'page_count' => $page_count,
149 4
            '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
        $session = $request->getSession();
189 2
        $page_no = intval($session->get('eccube.admin.customer.search.page_no'));
190 2
        $page_no = $page_no ? $page_no : Constant::ENABLED;
191
192 2
        $Customer = $app['orm.em']
193 2
            ->getRepository('Eccube\Entity\Customer')
194 2
            ->find($id);
195
196 2
        if (!$Customer) {
197
            $app->deleteMessage();
198
            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...
199
        }
200
201 2
        $Customer->setDelFlg(Constant::ENABLED);
202 2
        $app['orm.em']->persist($Customer);
203 2
        $app['orm.em']->flush();
204
205 2
        $event = new EventArgs(
206
            array(
207 2
                'Customer' => $Customer,
208
            ),
209
            $request
210
        );
211 2
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_CUSTOMER_DELETE_COMPLETE, $event);
212
213 2
        $app->addSuccess('admin.customer.delete.complete', 'admin');
214
215 2
        return $app->redirect($app->url('admin_customer_page', array('page_no' => $page_no)).'?resume='.Constant::ENABLED);
216
    }
217
218
    /**
219
     * 会員CSVの出力.
220
     * @param Application $app
221
     * @param Request $request
0 ignored issues
show
introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
222
     * @return StreamedResponse
223
     */
224
    public function export(Application $app, Request $request)
225
    {
226
        // タイムアウトを無効にする.
227
        set_time_limit(0);
228
229
        // sql loggerを無効にする.
230
        $em = $app['orm.em'];
231
        $em->getConfiguration()->setSQLLogger(null);
232
233
        $response = new StreamedResponse();
234
        $response->setCallback(function () use ($app, $request) {
235
236
            // CSV種別を元に初期化.
237
            $app['eccube.service.csv.export']->initCsvType(CsvType::CSV_TYPE_CUSTOMER);
238
239
            // ヘッダ行の出力.
240
            $app['eccube.service.csv.export']->exportHeader();
241
242
            // 会員データ検索用のクエリビルダを取得.
243
            $qb = $app['eccube.service.csv.export']
244
                ->getCustomerQueryBuilder($request);
245
246
            // データ行の出力.
247
            $app['eccube.service.csv.export']->setExportQueryBuilder($qb);
248 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...
249
250
                $Csvs = $csvService->getCsvs();
251
252
                /** @var $Customer \Eccube\Entity\Customer */
253
                $Customer = $entity;
254
255
                $row = array();
256
257
                // CSV出力項目と合致するデータを取得.
258
                foreach ($Csvs as $Csv) {
259
                    // 会員データを検索.
260
                    $row[] = $csvService->getData($Csv, $Customer);
261
                }
262
263
                //$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...
264
                // 出力.
265
                $csvService->fputcsv($row);
266
            });
267
        });
268
269
        $now = new \DateTime();
270
        $filename = 'customer_' . $now->format('YmdHis') . '.csv';
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
271
        $response->headers->set('Content-Type', 'application/octet-stream');
272
        $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...
273
274
        $response->send();
275
276
        return $response;
277
    }
278
}
279