Completed
Push — 4.0 ( a86355...f62b05 )
by Ryo
06:52 queued 10s
created

EntryController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 8
dl 0
loc 19
ccs 8
cts 8
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) LOCKON CO.,LTD. All Rights Reserved.
7
 *
8
 * http://www.lockon.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\Route;
26
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
27
use Symfony\Component\HttpFoundation\Request;
28
use Symfony\Component\HttpKernel\Exception as HttpException;
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
36
class EntryController extends AbstractController
37
{
38
    /**
39
     * @var CustomerStatusRepository
40
     */
41
    protected $customerStatusRepository;
42
43
    /**
44
     * @var ValidatorInterface
45
     */
46
    protected $recursiveValidator;
47
48
    /**
49
     * @var MailService
50
     */
51
    protected $mailService;
52
53
    /**
54
     * @var BaseInfo
55
     */
56
    protected $BaseInfo;
57
58
    /**
59
     * @var CustomerRepository
60
     */
61
    protected $customerRepository;
62
63
    /**
64
     * @var EncoderFactoryInterface
65
     */
66
    protected $encoderFactory;
67
68
    /**
69
     * @var TokenStorageInterface
70
     */
71
    protected $tokenStorage;
72
73
    /**
74
     * @var \Eccube\Service\CartService
75
     */
76
    protected $cartService;
77
78
    /**
79
     * EntryController constructor.
80
     *
81
     * @param CartService $cartService
82
     * @param CustomerStatusRepository $customerStatusRepository
83 9
     * @param MailService $mailService
84
     * @param BaseInfoRepository $baseInfoRepository
85
     * @param CustomerRepository $customerRepository
86
     * @param EncoderFactoryInterface $encoderFactory
87
     * @param ValidatorInterface $validatorInterface
88
     * @param TokenStorageInterface $tokenStorage
89
     */
90
    public function __construct(
91
        CartService $cartService,
92 9
        CustomerStatusRepository $customerStatusRepository,
93 9
        MailService $mailService,
94 9
        BaseInfoRepository $baseInfoRepository,
95 9
        CustomerRepository $customerRepository,
96 9
        EncoderFactoryInterface $encoderFactory,
97 9
        ValidatorInterface $validatorInterface,
98 9
        TokenStorageInterface $tokenStorage
99
    ) {
100
        $this->customerStatusRepository = $customerStatusRepository;
101
        $this->mailService = $mailService;
102
        $this->BaseInfo = $baseInfoRepository->get();
103
        $this->customerRepository = $customerRepository;
104
        $this->encoderFactory = $encoderFactory;
105
        $this->recursiveValidator = $validatorInterface;
106
        $this->tokenStorage = $tokenStorage;
107 5
        $this->cartService = $cartService;
108
    }
109 5
110
    /**
111
     * 会員登録画面.
112
     *
113
     * @Route("/entry", name="entry")
114
     * @Template("Entry/index.twig")
115
     */
116 5
    public function index(Request $request)
117
    {
118
        if ($this->isGranted('ROLE_USER')) {
119 5
            log_info('認証済のためログイン処理をスキップ');
120
121 5
            return $this->redirectToRoute('mypage');
122
        }
123 5
124 5
        /** @var $Customer \Eccube\Entity\Customer */
125
        $Customer = $this->customerRepository->newCustomer();
126 5
127
        /* @var $builder \Symfony\Component\Form\FormBuilderInterface */
128 5
        $builder = $this->formFactory->createBuilder(EntryType::class, $Customer);
129
130
        $event = new EventArgs(
131 5
            [
132
                'builder' => $builder,
133 5
                'Customer' => $Customer,
134
            ],
135 5
            $request
136 3
        );
137
        $this->eventDispatcher->dispatch(EccubeEvents::FRONT_ENTRY_INDEX_INITIALIZE, $event);
138 1
139 1
        /* @var $form \Symfony\Component\Form\FormInterface */
140
        $form = $builder->getForm();
141 1
142 1
        $form->handleRequest($request);
143
144 1
        if ($form->isSubmitted() && $form->isValid()) {
145
            switch ($request->get('mode')) {
146
                case 'confirm':
147
                    log_info('会員登録確認開始');
148
                    log_info('会員登録確認完了');
149 1
150
                    return $this->render(
151 1
                        'Entry/confirm.twig',
152 1
                        [
153 1
                            'form' => $form->createView(),
154 1
                        ]
155
                    );
156
157 1
                case 'complete':
158 1
                    log_info('会員登録開始');
159 1
160 1
                    $encoder = $this->encoderFactory->getEncoder($Customer);
161
                    $salt = $encoder->createSalt();
162 1
                    $password = $encoder->encodePassword($Customer->getPassword(), $salt);
163 1
                    $secretKey = $this->customerRepository->getUniqueSecretKey();
164
165 1
                    $Customer
166
                        ->setSalt($salt)
167 1
                        ->setPassword($password)
168
                        ->setSecretKey($secretKey)
169 1
                        ->setPoint(0);
170 1
171
                    $this->entityManager->persist($Customer);
172 1
                    $this->entityManager->flush();
173
174 1
                    log_info('会員登録完了');
175
176 1
                    $event = new EventArgs(
177
                        [
178 1
                            'form' => $form,
179
                            'Customer' => $Customer,
180
                        ],
181 1
                        $request
182
                    );
183 1
                    $this->eventDispatcher->dispatch(EccubeEvents::FRONT_ENTRY_INDEX_COMPLETE, $event);
184
185 1
                    $activateUrl = $this->generateUrl('entry_activate', ['secret_key' => $Customer->getSecretKey()]);
186
187
                    $activateFlg = $this->BaseInfo->isOptionCustomerActivate();
188
189 1
                    // 仮会員設定が有効な場合は、確認メールを送信し完了画面表示.
190
                    if ($activateFlg) {
191 1
                        // メール送信
192
                        $this->mailService->sendCustomerConfirmMail($Customer, $activateUrl);
193
194
                        if ($event->hasResponse()) {
195
                            return $event->getResponse();
196
                        }
197
198
                        log_info('仮会員登録完了画面へリダイレクト');
199
200
                        return $this->redirectToRoute('entry_complete');
201
                    // 仮会員設定が無効な場合は認証URLへ遷移させ、会員登録を完了させる.
202 3
                    } else {
203
                        log_info('本会員登録画面へリダイレクト');
204
205
                        return $this->redirect($activateUrl);
206
                    }
207
            }
208
        }
209
210
        return [
211
            'form' => $form->createView(),
212 1
        ];
213
    }
214 1
215
    /**
216
     * 会員登録完了画面.
217
     *
218
     * @Route("/entry/complete", name="entry_complete")
219
     * @Template("Entry/complete.twig")
220
     */
221
    public function complete()
222
    {
223 3
        return [];
224
    }
225 3
226 3
    /**
227
     * 会員のアクティベート(本会員化)を行う.
228 3
     *
229 3
     * @Route("/entry/activate/{secret_key}", name="entry_activate")
230
     * @Template("Entry/activate.twig")
231 3
     */
232
    public function activate(Request $request, $secret_key)
233
    {
234
        $errors = $this->recursiveValidator->validate(
235
            $secret_key,
236
            [
237 3
                new Assert\NotBlank(),
238 2
                new Assert\Regex(
239 2
                    [
240 2
                        'pattern' => '/^[a-zA-Z0-9]+$/',
241 1
                    ]
242
                ),
243
            ]
244 1
        );
245 1
246 1
        if ($request->getMethod() === 'GET' && count($errors) === 0) {
247 1
            log_info('本会員登録開始');
248
            $Customer = $this->customerRepository->getProvisionalCustomerBySecretKey($secret_key);
249 1
            if (is_null($Customer)) {
250
                throw new HttpException\NotFoundHttpException(trans('entrycontroller.text.error.registration'));
251 1
            }
252
253 1
            $CustomerStatus = $this->customerStatusRepository->find(CustomerStatus::REGULAR);
254
            $Customer->setStatus($CustomerStatus);
255 1
            $this->entityManager->persist($Customer);
256
            $this->entityManager->flush();
257 1
258
            log_info('本会員登録完了');
259
260 1
            $event = new EventArgs(
261
                [
262
                    'Customer' => $Customer,
263 1
                ],
264 1
                $request
265
            );
266 1
            $this->eventDispatcher->dispatch(EccubeEvents::FRONT_ENTRY_ACTIVATE_COMPLETE, $event);
267
268 1
            // メール送信
269
            $this->mailService->sendCustomerCompleteMail($Customer);
270 1
271
            // Assign session carts into customer carts
272
            $Carts = $this->cartService->getCarts();
273
            $qtyInCart = array_reduce($Carts, function($qty, $Cart) {
274
                return $qty + $Cart->getTotalQuantity();
275
            });
276
277
            // 本会員登録してログイン状態にする
278
            $token = new UsernamePasswordToken($Customer, null, 'customer', ['ROLE_USER']);
279
            $this->tokenStorage->setToken($token);
280
            $request->getSession()->migrate(true);
281
282
            if ($qtyInCart) {
283
                $this->cartService->save();
284
            }
285
286
            log_info('ログイン済に変更', [$this->getUser()->getId()]);
287
288
289
            return [
290
                'qtyInCart' => $qtyInCart
291
            ];
292
        } else {
293
            throw new HttpException\AccessDeniedHttpException(trans('entrycontroller.text.error.authorization'));
294
        }
295
    }
296
}
297