Failed Conditions
Push — experimental/3.1 ( 3d2ede...2919b9 )
by Yangsin
28:59
created

EntryController::index()   C

Complexity

Conditions 8
Paths 7

Size

Total Lines 103
Code Lines 60

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 48
CRAP Score 8.0536

Importance

Changes 0
Metric Value
cc 8
eloc 60
nc 7
nop 2
dl 0
loc 103
ccs 48
cts 53
cp 0.9057
crap 8.0536
rs 5.2676
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;
26
27
use Doctrine\ORM\EntityManager;
28
use Eccube\Annotation\Component;
29
use Eccube\Annotation\Inject;
30
use Eccube\Application;
31
use Eccube\Entity\BaseInfo;
32
use Eccube\Entity\CustomerAddress;
33
use Eccube\Entity\Master\CustomerStatus;
34
use Eccube\Event\EccubeEvents;
35
use Eccube\Event\EventArgs;
36
use Eccube\Form\Type\Front\EntryType;
37
use Eccube\Repository\CustomerRepository;
38
use Eccube\Repository\Master\CustomerStatusRepository;
39
use Eccube\Service\MailService;
40
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
41
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
42
use Symfony\Component\EventDispatcher\EventDispatcher;
43
use Symfony\Component\Form\FormFactory;
44
use Symfony\Component\HttpFoundation\Request;
45
use Symfony\Component\HttpKernel\Exception as HttpException;
46
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
47
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
48
use Symfony\Component\Validator\Constraints as Assert;
49
use Symfony\Component\Validator\Validator\RecursiveValidator;
50
51
/**
52
 * @Component
53
 * @Route(service=EntryController::class)
54
 */
55
class EntryController extends AbstractController
56
{
57
    /**
58
     * @Inject(CustomerStatusRepository::class)
59
     * @var CustomerStatusRepository
60
     */
61
    protected $customerStatusRepository;
62
63
    /**
64
     * @Inject("validator")
65
     * @var RecursiveValidator
66
     */
67
    protected $recursiveValidator;
68
69
    /**
70
     * @Inject(MailService::class)
71
     * @var MailService
72
     */
73
    protected $mailService;
74
75
    /**
76
     * @Inject(BaseInfo::class)
77
     * @var BaseInfo
78
     */
79
    protected $BaseInfo;
80
81
    /**
82
     * @Inject("orm.em")
83
     * @var EntityManager
84
     */
85
    protected $entityManager;
86
87
    /**
88
     * @Inject("eccube.event.dispatcher")
89
     * @var EventDispatcher
90
     */
91
    protected $eventDispatcher;
92
93
    /**
94
     * @Inject("form.factory")
95
     * @var FormFactory
96
     */
97
    protected $formFactory;
98
99
    /**
100
     * @Inject(CustomerRepository::class)
101
     * @var CustomerRepository
102
     */
103
    protected $customerRepository;
104
105
    /**
106
     * @Inject("security.encoder_factory")
107
     * @var EncoderFactoryInterface
108
     */
109
    protected $encoderFactory;
110
111
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
introduced by
Doc comment for parameter "$request" missing
Loading history...
112
     * 会員登録画面.
113
     *
114
     * @Route("/entry", name="entry")
115
     * @Template("Entry/index.twig")
116
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
117 5
    public function index(Application $app, Request $request)
118
    {
119 5
        if ($app->isGranted('ROLE_USER')) {
120
            log_info('認証済のためログイン処理をスキップ');
121
122
            return $app->redirect($app->url('mypage'));
123
        }
124
125
        /** @var $Customer \Eccube\Entity\Customer */
126 5
        $Customer = $this->customerRepository->newCustomer();
127
128
        /* @var $builder \Symfony\Component\Form\FormBuilderInterface */
129 5
        $builder = $this->formFactory->createBuilder(EntryType::class, $Customer);
130
131 5
        $event = new EventArgs(
132
            array(
133 5
                'builder' => $builder,
134 5
                'Customer' => $Customer,
135
            ),
136 5
            $request
137
        );
138 5
        $this->eventDispatcher->dispatch(EccubeEvents::FRONT_ENTRY_INDEX_INITIALIZE, $event);
139
140
        /* @var $form \Symfony\Component\Form\FormInterface */
141 5
        $form = $builder->getForm();
142
143 5
        $form->handleRequest($request);
144
145 5
        if ($form->isSubmitted() && $form->isValid()) {
146 3
            switch ($request->get('mode')) {
147 3
                case 'confirm':
148 1
                    log_info('会員登録確認開始');
149 1
                    log_info('会員登録確認完了');
150
151 1
                    return $app->render(
152 1
                        'Entry/confirm.twig',
153
                        array(
154 1
                            'form' => $form->createView(),
155
                        )
156
                    );
157
158 2
                case 'complete':
159 1
                    log_info('会員登録開始');
160
161 1
                    $encoder = $this->encoderFactory->getEncoder($Customer);
162 1
                    $salt = $encoder->createSalt();
163 1
                    $password = $encoder->encodePassword($Customer->getPassword(), $salt);
164 1
                    $secretKey = $this->customerRepository->getUniqueSecretKey();
165
166
                    $Customer
167 1
                        ->setSalt($salt)
168 1
                        ->setPassword($password)
169 1
                        ->setSecretKey($secretKey);
170
171 1
                    $CustomerAddress = new CustomerAddress();
172
                    $CustomerAddress
173 1
                        ->setFromCustomer($Customer);
174
175 1
                    $this->entityManager->persist($Customer);
176 1
                    $this->entityManager->persist($CustomerAddress);
177 1
                    $this->entityManager->flush();
178
179 1
                    log_info('会員登録完了');
180
181 1
                    $event = new EventArgs(
182
                        array(
183 1
                            'form' => $form,
184 1
                            'Customer' => $Customer,
185 1
                            'CustomerAddress' => $CustomerAddress,
186
                        ),
187 1
                        $request
188
                    );
189 1
                    $this->eventDispatcher->dispatch(EccubeEvents::FRONT_ENTRY_INDEX_COMPLETE, $event);
190
191 1
                    $activateUrl = $app->url('entry_activate', array('secret_key' => $Customer->getSecretKey()));
192
193 1
                    $activateFlg = $this->BaseInfo->getOptionCustomerActivate();
194
195
                    // 仮会員設定が有効な場合は、確認メールを送信し完了画面表示.
196 1
                    if ($activateFlg) {
197
                        // メール送信
198 1
                        $this->mailService->sendCustomerConfirmMail($Customer, $activateUrl);
199
200 1
                        if ($event->hasResponse()) {
201
                            return $event->getResponse();
202
                        }
203
204 1
                        log_info('仮会員登録完了画面へリダイレクト');
205
206 1
                        return $app->redirect($app->url('entry_complete'));
207
                        // 仮会員設定が無効な場合は認証URLへ遷移させ、会員登録を完了させる.
208
                    } else {
209
                        log_info('本会員登録画面へリダイレクト');
210
211
                        return $app->redirect($activateUrl);
212
                    }
213
            }
214
        }
215
216
        return [
217 3
            'form' => $form->createView(),
218
        ];
219
    }
220
221
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
introduced by
Doc comment for parameter "$request" missing
Loading history...
222
     * 会員登録完了画面.
223
     *
224
     * @Route("/entry/complete", name="entry_complete")
225
     * @Template("Entry/complete.twig")
226
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
227 1
    public function complete(Application $app, Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $app is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
228
    {
229 1
        return [];
230
    }
231
232
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
introduced by
Doc comment for parameter "$request" missing
Loading history...
introduced by
Doc comment for parameter "$secret_key" missing
Loading history...
233
     * 会員のアクティベート(本会員化)を行う.
234
     *
235
     * @Route("/entry/activate/{secret_key}", name="entry_activate")
236
     * @Template("Entry/activate.twig")
237
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
238 3
    public function activate(Application $app, Request $request, $secret_key)
239
    {
240 3
        $errors = $this->recursiveValidator->validate(
241 3
            $secret_key,
242
            array(
243 3
                new Assert\NotBlank(),
244 3
                new Assert\Regex(
245
                    array(
246 3
                        'pattern' => '/^[a-zA-Z0-9]+$/',
247
                    )
248
                ),
249
            )
250
        );
251
252 3
        if ($request->getMethod() === 'GET' && count($errors) === 0) {
253 2
            log_info('本会員登録開始');
254 2
            $Customer = $this->customerRepository->getProvisionalCustomerBySecretKey($secret_key);
255 2
            if (is_null($Customer)) {
256 1
                throw new HttpException\NotFoundHttpException('※ 既に会員登録が完了しているか、無効なURLです。');
257
            }
258
259 1
            $CustomerStatus = $this->customerStatusRepository->find(CustomerStatus::REGULAR);
260 1
            $Customer->setStatus($CustomerStatus);
261 1
            $this->entityManager->persist($Customer);
262 1
            $this->entityManager->flush();
263
264 1
            log_info('本会員登録完了');
265
266 1
            $event = new EventArgs(
267
                array(
268 1
                    'Customer' => $Customer,
269
                ),
270 1
                $request
271
            );
272 1
            $this->eventDispatcher->dispatch(EccubeEvents::FRONT_ENTRY_ACTIVATE_COMPLETE, $event);
273
274
            // メール送信
275 1
            $this->mailService->sendCustomerCompleteMail($Customer);
276
277
            // 本会員登録してログイン状態にする
278 1
            $token = new UsernamePasswordToken($Customer, null, 'customer', array('ROLE_USER'));
279 1
            $this->getSecurity($app)->setToken($token);
280
281 1
            log_info('ログイン済に変更', array($app->user()->getId()));
282
283 1
            return [];
284
        } else {
285 1
            throw new HttpException\AccessDeniedHttpException('不正なアクセスです。');
286
        }
287
    }
288
}
289