Completed
Pull Request — 4.0 (#4343)
by k-yamamura
04:39
created

EntryController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 8
dl 0
loc 19
ccs 9
cts 9
cp 1
crap 1
rs 9.6333
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
/*
4
 * This file is part of EC-CUBE
5
 *
6
 * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
7
 *
8
 * http://www.ec-cube.co.jp/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Eccube\Controller;
15
16
use Eccube\Entity\BaseInfo;
17
use Eccube\Entity\Master\CustomerStatus;
18
use Eccube\Event\EccubeEvents;
19
use Eccube\Event\EventArgs;
20
use Eccube\Form\Type\Front\EntryType;
21
use Eccube\Repository\BaseInfoRepository;
22
use Eccube\Repository\CustomerRepository;
23
use Eccube\Repository\Master\CustomerStatusRepository;
24
use Eccube\Service\MailService;
25
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
26
use Symfony\Component\HttpFoundation\Request;
27
use Symfony\Component\HttpKernel\Exception as HttpException;
28
use Symfony\Component\Routing\Annotation\Route;
29
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
30
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
31
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
32
use Symfony\Component\Validator\Constraints as Assert;
33
use Symfony\Component\Validator\Validator\ValidatorInterface;
34
use Eccube\Service\CartService;
35
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
36
37
class EntryController extends AbstractController
38
{
39
    /**
40
     * @var CustomerStatusRepository
41
     */
42
    protected $customerStatusRepository;
43
44
    /**
45
     * @var ValidatorInterface
46
     */
47
    protected $recursiveValidator;
48
49
    /**
50
     * @var MailService
51
     */
52
    protected $mailService;
53
54
    /**
55
     * @var BaseInfo
56
     */
57
    protected $BaseInfo;
58
59
    /**
60
     * @var CustomerRepository
61
     */
62
    protected $customerRepository;
63
64
    /**
65
     * @var EncoderFactoryInterface
66
     */
67
    protected $encoderFactory;
68
69
    /**
70
     * @var TokenStorageInterface
71
     */
72
    protected $tokenStorage;
73
74
    /**
75
     * @var \Eccube\Service\CartService
76
     */
77
    protected $cartService;
78
79
    /**
80
     * EntryController constructor.
81
     *
82
     * @param CartService $cartService
83 9
     * @param CustomerStatusRepository $customerStatusRepository
84
     * @param MailService $mailService
85
     * @param BaseInfoRepository $baseInfoRepository
86
     * @param CustomerRepository $customerRepository
87
     * @param EncoderFactoryInterface $encoderFactory
88
     * @param ValidatorInterface $validatorInterface
89
     * @param TokenStorageInterface $tokenStorage
90
     */
91
    public function __construct(
92 9
        CartService $cartService,
93 9
        CustomerStatusRepository $customerStatusRepository,
94 9
        MailService $mailService,
95 9
        BaseInfoRepository $baseInfoRepository,
96 9
        CustomerRepository $customerRepository,
97 9
        EncoderFactoryInterface $encoderFactory,
98 9
        ValidatorInterface $validatorInterface,
99
        TokenStorageInterface $tokenStorage
100
    ) {
101
        $this->customerStatusRepository = $customerStatusRepository;
102
        $this->mailService = $mailService;
103
        $this->BaseInfo = $baseInfoRepository->get();
104
        $this->customerRepository = $customerRepository;
105
        $this->encoderFactory = $encoderFactory;
106
        $this->recursiveValidator = $validatorInterface;
107 5
        $this->tokenStorage = $tokenStorage;
108
        $this->cartService = $cartService;
109 5
    }
110
111
    /**
112
     * 会員登録画面.
113
     *
114
     * @Route("/entry", name="entry")
115
     * @Template("Entry/index.twig")
116 5
     */
117
    public function index(Request $request)
118
    {
119 5
        if ($this->isGranted('ROLE_USER')) {
120
            log_info('認証済のためログイン処理をスキップ');
121 5
122
            return $this->redirectToRoute('mypage');
123 5
        }
124 5
125
        /** @var $Customer \Eccube\Entity\Customer */
126 5
        $Customer = $this->customerRepository->newCustomer();
127
128 5
        /* @var $builder \Symfony\Component\Form\FormBuilderInterface */
129
        $builder = $this->formFactory->createBuilder(EntryType::class, $Customer);
130
131 5
        $event = new EventArgs(
132
            [
133 5
                'builder' => $builder,
134
                'Customer' => $Customer,
135 5
            ],
136 3
            $request
137
        );
138 1
        $this->eventDispatcher->dispatch(EccubeEvents::FRONT_ENTRY_INDEX_INITIALIZE, $event);
139 1
140
        /* @var $form \Symfony\Component\Form\FormInterface */
141 1
        $form = $builder->getForm();
142 1
143
        $form->handleRequest($request);
144 1
145
        if ($form->isSubmitted() && $form->isValid()) {
146
            switch ($request->get('mode')) {
147
                case 'confirm':
148
                    log_info('会員登録確認開始');
149 1
                    log_info('会員登録確認完了');
150
151 1
                    return $this->render(
152 1
                        'Entry/confirm.twig',
153 1
                        [
154 1
                            'form' => $form->createView(),
155
                        ]
156
                    );
157 1
158 1
                case 'complete':
159 1
                    log_info('会員登録開始');
160 1
161
                    $encoder = $this->encoderFactory->getEncoder($Customer);
162 1
                    $salt = $encoder->createSalt();
163 1
                    $password = $encoder->encodePassword($Customer->getPassword(), $salt);
164
                    $secretKey = $this->customerRepository->getUniqueSecretKey();
165 1
166
                    $Customer
167 1
                        ->setSalt($salt)
168
                        ->setPassword($password)
169 1
                        ->setSecretKey($secretKey)
170 1
                        ->setPoint(0);
171
172 1
                    $this->entityManager->persist($Customer);
173
                    $this->entityManager->flush();
174 1
175
                    log_info('会員登録完了');
176 1
177
                    $event = new EventArgs(
178 1
                        [
179
                            'form' => $form,
180
                            'Customer' => $Customer,
181 1
                        ],
182
                        $request
183 1
                    );
184
                    $this->eventDispatcher->dispatch(EccubeEvents::FRONT_ENTRY_INDEX_COMPLETE, $event);
185 1
186
187
                    $activateFlg = $this->BaseInfo->isOptionCustomerActivate();
188
189 1
                    // 仮会員設定が有効な場合は、確認メールを送信し完了画面表示.
190
                    if ($activateFlg) {
191 1
                        $activateUrl = $this->generateUrl('entry_activate', ['secret_key' => $Customer->getSecretKey()], UrlGeneratorInterface::ABSOLUTE_URL);
192
193
                        // メール送信
194
                        $this->mailService->sendCustomerConfirmMail($Customer, $activateUrl);
195
196
                        if ($event->hasResponse()) {
197
                            return $event->getResponse();
198
                        }
199
200
                        log_info('仮会員登録完了画面へリダイレクト');
201
202 3
                        return $this->redirectToRoute('entry_complete');
203
204
                    } else {
205
                        // 仮会員設定が無効な場合は、会員登録を完了させる.
206
                        $qtyInCart = $this->entryActivate($request, $Customer->getSecretKey());
207
208
                        // URLを変更するため完了画面にリダイレクト
209
                        return $this->redirectToRoute('entry_activate', [
210
                            'secret_key' => $Customer->getSecretKey(),
211
                            'qtyInCart' => $qtyInCart,
212 1
                        ]);
213
214 1
                    }
215
            }
216
        }
217
218
        return [
219
            'form' => $form->createView(),
220
        ];
221
    }
222
223 3
    /**
224
     * 会員登録完了画面.
225 3
     *
226 3
     * @Route("/entry/complete", name="entry_complete")
227
     * @Template("Entry/complete.twig")
228 3
     */
229 3
    public function complete()
230
    {
231 3
        return [];
232
    }
233
234
    /**
235
     * 会員のアクティベート(本会員化)を行う.
236
     *
237 3
     * @Route("/entry/activate/{secret_key}/{qtyInCart}", name="entry_activate")
238 2
     * @Template("Entry/activate.twig")
239 2
     */
240 2
    public function activate(Request $request, $secret_key, $qtyInCart = null)
241 1
    {
242
        $errors = $this->recursiveValidator->validate(
243
            $secret_key,
244 1
            [
245 1
                new Assert\NotBlank(),
246 1
                new Assert\Regex(
247 1
                    [
248
                        'pattern' => '/^[a-zA-Z0-9]+$/',
249 1
                    ]
250
                ),
251 1
            ]
252
        );
253 1
254
        if(!is_null($qtyInCart)) {
255 1
256
            return [
257 1
                'qtyInCart' => $qtyInCart,
258
            ];
259
        } elseif ($request->getMethod() === 'GET' && count($errors) === 0) {
260 1
261
            // 会員登録処理を行う
262
            $qtyInCart = $this->entryActivate($request, $secret_key);
263 1
264 1
            return [
265
                'qtyInCart' => $qtyInCart,
266 1
            ];
267
        }
268 1
269
        throw new HttpException\NotFoundHttpException();
270 1
    }
271
272
273
    /**
274
     * 会員登録処理を行う
275
     *
276
     * @param Request $request
277
     * @param $secret_key
278
     * @return \Eccube\Entity\Cart|mixed
279
     */
280
    private function entryActivate(Request $request, $secret_key)
281
    {
282
        log_info('本会員登録開始');
283
        $Customer = $this->customerRepository->getProvisionalCustomerBySecretKey($secret_key);
284
        if (is_null($Customer)) {
285
            throw new HttpException\NotFoundHttpException();
286
        }
287
288
        $CustomerStatus = $this->customerStatusRepository->find(CustomerStatus::REGULAR);
289
        $Customer->setStatus($CustomerStatus);
290
        $this->entityManager->persist($Customer);
291
        $this->entityManager->flush();
292
293
        log_info('本会員登録完了');
294
295
        $event = new EventArgs(
296
            [
297
                'Customer' => $Customer,
298
            ],
299
            $request
300
        );
301
        $this->eventDispatcher->dispatch(EccubeEvents::FRONT_ENTRY_ACTIVATE_COMPLETE, $event);
302
303
        // メール送信
304
        $this->mailService->sendCustomerCompleteMail($Customer);
305
306
        // Assign session carts into customer carts
307
        $Carts = $this->cartService->getCarts();
308
        $qtyInCart = 0;
309
        foreach ($Carts as $Cart) {
310
            $qtyInCart += $Cart->getTotalQuantity();
311
        }
312
313
        // 本会員登録してログイン状態にする
314
        $token = new UsernamePasswordToken($Customer, null, 'customer', ['ROLE_USER']);
315
        $this->tokenStorage->setToken($token);
316
        $request->getSession()->migrate(true);
317
318
        if ($qtyInCart) {
319
            $this->cartService->save();
320
        }
321
322
        log_info('ログイン済に変更', [$this->getUser()->getId()]);
323
324
        return $qtyInCart;
325
326
    }
327
328
}
329