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

EntryController::entryActivate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 46

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 2
dl 0
loc 46
ccs 0
cts 0
cp 0
crap 12
rs 9.1781
c 0
b 0
f 0
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
                    $activateUrl = $this->generateUrl('entry_activate', ['secret_key' => $Customer->getSecretKey()], UrlGeneratorInterface::ABSOLUTE_URL);
187
188
                    $activateFlg = $this->BaseInfo->isOptionCustomerActivate();
189 1
190
                    // 仮会員設定が有効な場合は、確認メールを送信し完了画面表示.
191 1
                    if ($activateFlg) {
192
                        // メール送信
193
                        $this->mailService->sendCustomerConfirmMail($Customer, $activateUrl);
194
195
                        if ($event->hasResponse()) {
196
                            return $event->getResponse();
197
                        }
198
199
                        log_info('仮会員登録完了画面へリダイレクト');
200
201
                        return $this->redirectToRoute('entry_complete');
202 3
203
                    } else {
204
                        // 仮会員設定が無効な場合は、会員登録を完了させる.
205
                        $qtyInCart = $this->entryActivate($request, $Customer->getSecretKey());
206
207
                        return $this->render('Entry/activate.twig', [
208
                            'qtyInCart' => $qtyInCart,
209
                        ]);
210
211
                    }
212 1
            }
213
        }
214 1
215
        return [
216
            'form' => $form->createView(),
217
        ];
218
    }
219
220
    /**
221
     * 会員登録完了画面.
222
     *
223 3
     * @Route("/entry/complete", name="entry_complete")
224
     * @Template("Entry/complete.twig")
225 3
     */
226 3
    public function complete()
227
    {
228 3
        return [];
229 3
    }
230
231 3
    /**
232
     * 会員のアクティベート(本会員化)を行う.
233
     *
234
     * @Route("/entry/activate/{secret_key}", name="entry_activate")
235
     * @Template("Entry/activate.twig")
236
     */
237 3
    public function activate(Request $request, $secret_key)
238 2
    {
239 2
        $errors = $this->recursiveValidator->validate(
240 2
            $secret_key,
241 1
            [
242
                new Assert\NotBlank(),
243
                new Assert\Regex(
244 1
                    [
245 1
                        'pattern' => '/^[a-zA-Z0-9]+$/',
246 1
                    ]
247 1
                ),
248
            ]
249 1
        );
250
251 1
        if ($request->getMethod() === 'GET' && count($errors) === 0) {
252
253 1
            // 会員登録処理を行う
254
            $qtyInCart = $this->entryActivate($request, $secret_key);
255 1
256
            return [
257 1
                'qtyInCart' => $qtyInCart,
258
            ];
259
        }
260 1
261
        throw new HttpException\NotFoundHttpException();
262
    }
263 1
264 1
265
    /**
266 1
     * 会員登録処理を行う
267
     *
268 1
     * @param Request $request
269
     * @param $secret_key
270 1
     * @return \Eccube\Entity\Cart|mixed
271
     */
272
    private function entryActivate(Request $request, $secret_key)
273
    {
274
        log_info('本会員登録開始');
275
        $Customer = $this->customerRepository->getProvisionalCustomerBySecretKey($secret_key);
276
        if (is_null($Customer)) {
277
            throw new HttpException\NotFoundHttpException();
278
        }
279
280
        $CustomerStatus = $this->customerStatusRepository->find(CustomerStatus::REGULAR);
281
        $Customer->setStatus($CustomerStatus);
282
        $this->entityManager->persist($Customer);
283
        $this->entityManager->flush();
284
285
        log_info('本会員登録完了');
286
287
        $event = new EventArgs(
288
            [
289
                'Customer' => $Customer,
290
            ],
291
            $request
292
        );
293
        $this->eventDispatcher->dispatch(EccubeEvents::FRONT_ENTRY_ACTIVATE_COMPLETE, $event);
294
295
        // メール送信
296
        $this->mailService->sendCustomerCompleteMail($Customer);
297
298
        // Assign session carts into customer carts
299
        $Carts = $this->cartService->getCarts();
300
        $qtyInCart = array_reduce($Carts, function ($qty, $Cart) {
301
            return $qty + $Cart->getTotalQuantity();
302
        });
303
304
        // 本会員登録してログイン状態にする
305
        $token = new UsernamePasswordToken($Customer, null, 'customer', ['ROLE_USER']);
306
        $this->tokenStorage->setToken($token);
307
        $request->getSession()->migrate(true);
308
309
        if ($qtyInCart) {
310
            $this->cartService->save();
311
        }
312
313
        log_info('ログイン済に変更', [$this->getUser()->getId()]);
314
315
        return $qtyInCart;
316
317
    }
318
319
}
320