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\Admin\Customer; |
15
|
|
|
|
16
|
|
|
use Eccube\Controller\AbstractController; |
17
|
|
|
use Eccube\Event\EccubeEvents; |
18
|
|
|
use Eccube\Event\EventArgs; |
19
|
|
|
use Eccube\Entity\Master\CustomerStatus; |
20
|
|
|
use Eccube\Form\Type\Admin\CustomerType; |
21
|
|
|
use Eccube\Repository\CustomerRepository; |
22
|
|
|
use Eccube\Util\StringUtil; |
23
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
24
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; |
25
|
|
|
use Symfony\Component\HttpFoundation\Request; |
26
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
27
|
|
|
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface; |
28
|
|
|
|
29
|
|
|
class CustomerEditController extends AbstractController |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var CustomerRepository |
33
|
|
|
*/ |
34
|
|
|
protected $customerRepository; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var EncoderFactoryInterface |
38
|
|
|
*/ |
39
|
|
|
protected $encoderFactory; |
40
|
|
|
|
41
|
|
|
public function __construct( |
42
|
|
|
CustomerRepository $customerRepository, |
43
|
|
|
EncoderFactoryInterface $encoderFactory |
44
|
|
|
) { |
45
|
|
|
$this->customerRepository = $customerRepository; |
46
|
|
|
$this->encoderFactory = $encoderFactory; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @Route("/%eccube_admin_route%/customer/new", name="admin_customer_new") |
51
|
|
|
* @Route("/%eccube_admin_route%/customer/{id}/edit", requirements={"id" = "\d+"}, name="admin_customer_edit") |
52
|
|
|
* @Template("@admin/Customer/edit.twig") |
53
|
|
|
*/ |
54
|
|
|
public function index(Request $request, $id = null) |
55
|
|
|
{ |
56
|
|
|
$this->entityManager->getFilters()->enable('incomplete_order_status_hidden'); |
57
|
|
|
// 編集 |
58
|
|
|
if ($id) { |
59
|
|
|
$Customer = $this->customerRepository |
60
|
|
|
->find($id); |
61
|
|
|
|
62
|
|
|
if (is_null($Customer)) { |
63
|
|
|
throw new NotFoundHttpException(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$oldStatusId = $Customer->getStatus()->getId(); |
67
|
|
|
// 編集用にデフォルトパスワードをセット |
68
|
|
|
$previous_password = $Customer->getPassword(); |
69
|
|
|
$Customer->setPassword($this->eccubeConfig['eccube_default_password']); |
70
|
|
|
// 新規登録 |
71
|
|
|
} else { |
72
|
|
|
$Customer = $this->customerRepository->newCustomer(); |
73
|
|
|
$Customer->setBuyTimes(0); |
74
|
|
|
$Customer->setBuyTotal(0); |
75
|
|
|
$oldStatusId = null; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
// 会員登録フォーム |
79
|
|
|
$builder = $this->formFactory |
80
|
|
|
->createBuilder(CustomerType::class, $Customer); |
81
|
|
|
|
82
|
|
|
$event = new EventArgs( |
83
|
|
|
[ |
84
|
|
|
'builder' => $builder, |
85
|
|
|
'Customer' => $Customer, |
86
|
|
|
], |
87
|
|
|
$request |
88
|
|
|
); |
89
|
|
|
$this->eventDispatcher->dispatch(EccubeEvents::ADMIN_CUSTOMER_EDIT_INDEX_INITIALIZE, $event); |
90
|
|
|
|
91
|
|
|
$form = $builder->getForm(); |
92
|
|
|
|
93
|
|
|
$form->handleRequest($request); |
94
|
|
|
|
95
|
|
|
if ($form->isSubmitted()) { |
96
|
|
|
if ($form->isValid()) { |
97
|
|
|
log_info('会員登録開始', [$Customer->getId()]); |
98
|
|
|
|
99
|
|
|
$encoder = $this->encoderFactory->getEncoder($Customer); |
100
|
|
|
|
101
|
|
View Code Duplication |
if ($Customer->getPassword() === $this->eccubeConfig['eccube_default_password']) { |
|
|
|
|
102
|
|
|
$Customer->setPassword($previous_password); |
|
|
|
|
103
|
|
|
} else { |
104
|
|
|
if ($Customer->getSalt() === null) { |
105
|
|
|
$Customer->setSalt($encoder->createSalt()); |
106
|
|
|
$Customer->setSecretKey($this->customerRepository->getUniqueSecretKey()); |
107
|
|
|
} |
108
|
|
|
$Customer->setPassword($encoder->encodePassword($Customer->getPassword(), $Customer->getSalt())); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
// 退会ステータスに更新の場合、ダミーのアドレスに更新 |
112
|
|
|
$newStatusId = $Customer->getStatus()->getId(); |
113
|
|
|
if ($oldStatusId != $newStatusId && $newStatusId == CustomerStatus::WITHDRAWING) { |
114
|
|
|
$Customer->setEmail(StringUtil::random(60).'@dummy.dummy'); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$this->entityManager->persist($Customer); |
118
|
|
|
$this->entityManager->flush(); |
119
|
|
|
|
120
|
|
|
log_info('会員登録完了', [$Customer->getId()]); |
121
|
|
|
|
122
|
|
|
$event = new EventArgs( |
123
|
|
|
[ |
124
|
|
|
'form' => $form, |
125
|
|
|
'Customer' => $Customer, |
126
|
|
|
], |
127
|
|
|
$request |
128
|
|
|
); |
129
|
|
|
$this->eventDispatcher->dispatch(EccubeEvents::ADMIN_CUSTOMER_EDIT_INDEX_COMPLETE, $event); |
130
|
|
|
|
131
|
|
|
$this->addSuccess('admin.customer.save.complete', 'admin'); |
132
|
|
|
|
133
|
|
|
return $this->redirectToRoute('admin_customer_edit', [ |
134
|
|
|
'id' => $Customer->getId(), |
135
|
|
|
]); |
136
|
|
|
} else { |
137
|
|
|
$this->addError('admin.customer.save.failed', 'admin'); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return [ |
142
|
|
|
'form' => $form->createView(), |
143
|
|
|
'Customer' => $Customer, |
144
|
|
|
]; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.