Failed Conditions
Pull Request — 3.0.9-dev (#1468)
by chihiro
59:35 queued 23:18
created

EntryController::index()   C

Complexity

Conditions 7
Paths 6

Size

Total Lines 88
Code Lines 51

Duplication

Lines 8
Ratio 9.09 %

Code Coverage

Tests 12
CRAP Score 8.8142

Importance

Changes 5
Bugs 0 Features 3
Metric Value
c 5
b 0
f 3
dl 8
loc 88
ccs 12
cts 18
cp 0.6667
rs 6.5184
cc 7
eloc 51
nc 6
nop 2
crap 8.8142

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;
26
27
use Eccube\Application;
28
use Eccube\Entity\Master\CustomerStatus;
29
use Eccube\Event\EccubeEvents;
30
use Eccube\Event\EventArgs;
31
use Symfony\Component\HttpFoundation\Request;
32
use Symfony\Component\HttpKernel\Exception as HttpException;
33
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
34
use Symfony\Component\Validator\Constraints as Assert;
35
36
class EntryController extends AbstractController
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
37
{
38
39
    /**
40
     * 会員登録画面.
41
     *
42
     * @param  Application $app
43
     * @param  Request $request
0 ignored issues
show
introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
44 5
     * @return \Symfony\Component\HttpFoundation\Response
45
     */
46
    public function index(Application $app, Request $request)
47
    {
48
        /** @var $Customer \Eccube\Entity\Customer */
49
        $Customer = $app['eccube.repository.customer']->newCustomer();
50
51
        /* @var $builder \Symfony\Component\Form\FormBuilderInterface */
52
        $builder = $app['form.factory']->createBuilder('entry', $Customer);
53
54
        $event = new EventArgs(
55
            array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
56 3
                'builder' => $builder,
57 3
                'Customer' => $Customer
58
            ),
59
            $request
60
        );
61
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_ENTRY_INDEX_INITIALIZE, $event);
62 1
63 1
        /* @var $form \Symfony\Component\Form\FormInterface */
64
        $form = $builder->getForm();
65
66 2
        $form->handleRequest($request);
67
68
        if ($form->isSubmitted() && $form->isValid()) {
69
            switch ($request->get('mode')) {
70 View Code Duplication
                case 'confirm':
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...
71
                    $builder->setAttribute('freeze', true);
72
                    $form = $builder->getForm();
73
                    $form->handleRequest($request);
74
75
                    return $app->render('Entry/confirm.twig', array(
76
                        'form' => $form->createView(),
77
                    ));
78
79
                case 'complete':
80
                    $Customer
81
                        ->setSalt(
82
                            $app['eccube.repository.customer']->createSalt(5)
83
                        )
84
                        ->setPassword(
85
                            $app['eccube.repository.customer']->encryptPassword($app, $Customer)
86
                        )
87
                        ->setSecretKey(
88
                            $app['eccube.repository.customer']->getUniqueSecretKey($app)
89
                        );
90
91
                    $CustomerAddress = new \Eccube\Entity\CustomerAddress();
92
                    $CustomerAddress
93 1
                        ->setFromCustomer($Customer);
94
95
                    $app['orm.em']->persist($Customer);
96
                    $app['orm.em']->persist($CustomerAddress);
97
                    $app['orm.em']->flush();
98
99
                    $event = new EventArgs(array(
100
                            'form' => $form,
101
                            'Customer' => $Customer,
102
                            'CustomerAddress' => $CustomerAddress,
103
                        ), $request
104
                    );
105 3
                    $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_ENTRY_INDEX_COMPLETE, $event);
106 3
107
                    $activateUrl = $app->url('entry_activate', array('secret_key' => $Customer->getSecretKey()));
108 5
109
                    /** @var $BaseInfo \Eccube\Entity\BaseInfo */
110
                    $BaseInfo = $app['eccube.repository.base_info']->get();
111
                    $activateFlg = $BaseInfo->getOptionCustomerActivate();
112
113
                    // 仮会員設定が有効な場合は、確認メールを送信し完了画面表示.
114
                    if ($activateFlg) {
115
                        // メール送信
116 1
                        $app['eccube.service.mail']->sendCustomerConfirmMail($Customer, $activateUrl);
117
118
                        if ($event->hasResponse()) {
119 1
                            return $event->getResponse();
120
                        }
121
122
                        return $app->redirect($app->url('entry_complete'));
123
                    // 仮会員設定が無効な場合は認証URLへ遷移させ、会員登録を完了させる.
124
                    } else {
125
                        return $app->redirect($activateUrl);
126
                    }
127
            }
128
        }
129 3
130
        return $app->render('Entry/index.twig', array(
131
            'form' => $form->createView(),
132
        ));
133
    }
134
135
    /**
136 3
     * 会員登録完了画面.
137
     *
138
     * @param Application $app
139
     * @return \Symfony\Component\HttpFoundation\Response
140
     */
141
    public function complete(Application $app)
142
    {
143
        return $app->render('Entry/complete.twig', array());
144
    }
145 2
146
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$secret_key" missing
Loading history...
147
     * 会員のアクティベート(本会員化)を行う.
148
     *
149
     * @param Application $app
150
     * @param Request $request
0 ignored issues
show
introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
151
     * @param $secret_key
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
152
     * @return \Symfony\Component\HttpFoundation\Response
153
     */
154
    public function activate(Application $app, Request $request, $secret_key)
155
    {
156
        $errors = $app['validator']->validateValue($secret_key, array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
157
                new Assert\NotBlank(),
158
                new Assert\Regex(array(
159
                    'pattern' => '/^[a-zA-Z0-9]+$/',
160
                ))
161
            )
162
        );
163 3
164
        if ($request->getMethod() === 'GET' && count($errors) === 0) {
165
            try {
166
                $Customer = $app['eccube.repository.customer']
167
                    ->getNonActiveCustomerBySecretKey($secret_key);
168
            } catch (\Exception $e) {
169
                throw new HttpException\NotFoundHttpException('※ 既に会員登録が完了しているか、無効なURLです。');
170
            }
171
172
            $CustomerStatus = $app['eccube.repository.customer_status']->find(CustomerStatus::ACTIVE);
173
            $Customer->setStatus($CustomerStatus);
174
            $app['orm.em']->persist($Customer);
175
            $app['orm.em']->flush();
176
177
            $event = new EventArgs(
178
                array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
179
                    'Customer' => $Customer
180
                ),
181
                $request
182
            );
183
            $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_ENTRY_ACTIVATE_COMPLETE, $event);
184
185
            // メール送信
186
            $app['eccube.service.mail']->sendCustomerCompleteMail($Customer);
187
188
            // 本会員登録してログイン状態にする
189
            $token = new UsernamePasswordToken($Customer, null, 'customer', array('ROLE_USER'));
190
            $this->getSecurity($app)->setToken($token);
191
192
            return $app->render('Entry/activate.twig');
193
        } else {
194
            throw new HttpException\AccessDeniedHttpException('不正なアクセスです。');
195
        }
196
    }
197
}
198