Completed
Pull Request — experimental/3.1 (#2484)
by Kentaro
57:08 queued 35:11
created

EntryController::index()   C

Complexity

Conditions 8
Paths 7

Size

Total Lines 103
Code Lines 59

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 47
CRAP Score 8.0569

Importance

Changes 0
Metric Value
cc 8
eloc 59
nc 7
nop 2
dl 0
loc 103
ccs 47
cts 52
cp 0.9038
crap 8.0569
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\Master\CustomerStatus;
33
use Eccube\Event\EccubeEvents;
34
use Eccube\Event\EventArgs;
35
use Eccube\Form\Type\Front\EntryType;
36
use Eccube\Repository\CustomerRepository;
37
use Eccube\Repository\Master\CustomerStatusRepository;
38
use Eccube\Service\MailService;
39
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
40
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
41
use Symfony\Component\EventDispatcher\EventDispatcher;
42
use Symfony\Component\Form\FormFactory;
43
use Symfony\Component\HttpFoundation\Request;
44
use Symfony\Component\HttpKernel\Exception as HttpException;
45
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
46
use Symfony\Component\Validator\Constraints as Assert;
47
use Symfony\Component\Validator\Validator\RecursiveValidator;
48
49
/**
50
 * @Component
51
 * @Route(service=EntryController::class)
52
 */
53
class EntryController extends AbstractController
54
{
55
    /**
56
     * @Inject(CustomerStatusRepository::class)
57
     * @var CustomerStatusRepository
58
     */
59
    protected $customerStatusRepository;
60
61
    /**
62
     * @Inject("validator")
63
     * @var RecursiveValidator
64
     */
65
    protected $recursiveValidator;
66
67
    /**
68
     * @Inject(MailService::class)
69
     * @var MailService
70
     */
71
    protected $mailService;
72
73
    /**
74
     * @Inject(BaseInfo::class)
75
     * @var BaseInfo
76
     */
77
    protected $BaseInfo;
78
79
    /**
80
     * @Inject("orm.em")
81
     * @var EntityManager
82
     */
83
    protected $entityManager;
84
85
    /**
86
     * @Inject("eccube.event.dispatcher")
87
     * @var EventDispatcher
88
     */
89
    protected $eventDispatcher;
90
91
    /**
92
     * @Inject("form.factory")
93
     * @var FormFactory
94
     */
95
    protected $formFactory;
96
97
    /**
98
     * @Inject(CustomerRepository::class)
99
     * @var CustomerRepository
100
     */
101
    protected $customerRepository;
102
103
104
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
introduced by
Doc comment for parameter "$request" missing
Loading history...
105
     * 会員登録画面.
106
     *
107
     * @Route("/entry", name="entry")
108
     * @Template("Entry/index.twig")
109
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
110 5
    public function index(Application $app, Request $request)
111
    {
112 5
        if ($app->isGranted('ROLE_USER')) {
113
            log_info('認証済のためログイン処理をスキップ');
114
115
            return $app->redirect($app->url('mypage'));
116
        }
117
118
        /** @var $Customer \Eccube\Entity\Customer */
119 5
        $Customer = $this->customerRepository->newCustomer();
120
121
        /* @var $builder \Symfony\Component\Form\FormBuilderInterface */
122 5
        $builder = $this->formFactory->createBuilder(EntryType::class, $Customer);
123
124 5
        $event = new EventArgs(
125
            array(
126 5
                'builder' => $builder,
127 5
                'Customer' => $Customer,
128
            ),
129 5
            $request
130
        );
131 5
        $this->eventDispatcher->dispatch(EccubeEvents::FRONT_ENTRY_INDEX_INITIALIZE, $event);
132
133
        /* @var $form \Symfony\Component\Form\FormInterface */
134 5
        $form = $builder->getForm();
135
136 5
        $form->handleRequest($request);
137
138 5
        if ($form->isSubmitted() && $form->isValid()) {
139 3
            switch ($request->get('mode')) {
140 3
                case 'confirm':
141 1
                    log_info('会員登録確認開始');
142 1
                    log_info('会員登録確認完了');
143
144 1
                    return $app->render(
145 1
                        'Entry/confirm.twig',
146
                        array(
147 1
                            'form' => $form->createView(),
148
                        )
149
                    );
150
151 2
                case 'complete':
152 1
                    log_info('会員登録開始');
153
                    $Customer
154 1
                        ->setSalt(
155 1
                            $this->customerRepository->createSalt(5)
156
                        )
157 1
                        ->setPassword(
158 1
                            $this->customerRepository->encryptPassword($app, $Customer)
159
                        )
160 1
                        ->setSecretKey(
161 1
                            $this->customerRepository->getUniqueSecretKey($app)
162
                        );
163
164 1
                    $CustomerAddress = new \Eccube\Entity\CustomerAddress();
165
                    $CustomerAddress
166 1
                        ->setFromCustomer($Customer);
167
168 1
                    $this->entityManager->persist($Customer);
169 1
                    $this->entityManager->persist($CustomerAddress);
170 1
                    $this->entityManager->flush();
171
172 1
                    log_info('会員登録完了');
173
174 1
                    $event = new EventArgs(
175
                        array(
176 1
                            'form' => $form,
177 1
                            'Customer' => $Customer,
178 1
                            'CustomerAddress' => $CustomerAddress,
179
                        ),
180 1
                        $request
181
                    );
182 1
                    $this->eventDispatcher->dispatch(EccubeEvents::FRONT_ENTRY_INDEX_COMPLETE, $event);
183
184 1
                    $activateUrl = $app->url('entry_activate', array('secret_key' => $Customer->getSecretKey()));
185
186 1
                    $activateFlg = $this->BaseInfo->getOptionCustomerActivate();
187
188
                    // 仮会員設定が有効な場合は、確認メールを送信し完了画面表示.
189 1
                    if ($activateFlg) {
190
                        // メール送信
191 1
                        $this->mailService->sendCustomerConfirmMail($Customer, $activateUrl);
192
193 1
                        if ($event->hasResponse()) {
194
                            return $event->getResponse();
195
                        }
196
197 1
                        log_info('仮会員登録完了画面へリダイレクト');
198
199 1
                        return $app->redirect($app->url('entry_complete'));
200
                        // 仮会員設定が無効な場合は認証URLへ遷移させ、会員登録を完了させる.
201
                    } else {
202
                        log_info('本会員登録画面へリダイレクト');
203
204
                        return $app->redirect($activateUrl);
205
                    }
206
            }
207
        }
208
209
        return [
210 3
            'form' => $form->createView(),
211
        ];
212
    }
213
214
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
introduced by
Doc comment for parameter "$request" missing
Loading history...
215
     * 会員登録完了画面.
216
     *
217
     * @Route("/entry/complete", name="entry_complete")
218
     * @Template("Entry/complete.twig")
219
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
220 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...
221
    {
222 1
        return [];
223
    }
224
225
    /**
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...
226
     * 会員のアクティベート(本会員化)を行う.
227
     *
228
     * @Route("/entry/activate/{secret_key}", name="entry_activate")
229
     * @Template("Entry/activate.twig")
230
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
231 3
    public function activate(Application $app, Request $request, $secret_key)
232
    {
233 3
        $errors = $this->recursiveValidator->validate(
234 3
            $secret_key,
235
            array(
236 3
                new Assert\NotBlank(),
237 3
                new Assert\Regex(
238
                    array(
239 3
                        'pattern' => '/^[a-zA-Z0-9]+$/',
240
                    )
241
                ),
242
            )
243
        );
244
245 3
        if ($request->getMethod() === 'GET' && count($errors) === 0) {
246 2
            log_info('本会員登録開始');
247
            try {
248 2
                $Customer = $this->customerRepository
249 2
                    ->getNonActiveCustomerBySecretKey($secret_key);
250 1
            } catch (\Exception $e) {
251 1
                throw new HttpException\NotFoundHttpException('※ 既に会員登録が完了しているか、無効なURLです。');
252
            }
253
254 1
            $CustomerStatus = $this->customerStatusRepository->find(CustomerStatus::ACTIVE);
255 1
            $Customer->setStatus($CustomerStatus);
256 1
            $this->entityManager->persist($Customer);
257 1
            $this->entityManager->flush();
258
259 1
            log_info('本会員登録完了');
260
261 1
            $event = new EventArgs(
262
                array(
263 1
                    'Customer' => $Customer,
264
                ),
265 1
                $request
266
            );
267 1
            $this->eventDispatcher->dispatch(EccubeEvents::FRONT_ENTRY_ACTIVATE_COMPLETE, $event);
268
269
            // メール送信
270 1
            $this->mailService->sendCustomerCompleteMail($Customer);
271
272
            // 本会員登録してログイン状態にする
273 1
            $token = new UsernamePasswordToken($Customer, null, 'customer', array('ROLE_USER'));
274 1
            $this->getSecurity($app)->setToken($token);
275
276 1
            log_info('ログイン済に変更', array($app->user()->getId()));
277
278 1
            return [];
279
        } else {
280 1
            throw new HttpException\AccessDeniedHttpException('不正なアクセスです。');
281
        }
282
    }
283
}
284