PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / trunk
Booking for Appointments and Events Calendar – Amelia vtrunk
2.4.4 2.4.3 2.4.2 2.4.1 2.4 trunk 1.2.1 1.2.10 1.2.11 1.2.12 1.2.13 1.2.14 1.2.15 1.2.16 1.2.17 1.2.18 1.2.19 1.2.2 1.2.20 1.2.21 1.2.22 1.2.23 1.2.24 1.2.25 1.2.26 1.2.27 1.2.28 1.2.29 1.2.3 1.2.30 1.2.31 1.2.32 1.2.33 1.2.34 1.2.35 1.2.36 1.2.37 1.2.38 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 2.0 2.0.1 2.0.2 2.1 2.1.1 2.1.2 2.1.3 2.2 2.2.1 2.3
ameliabooking / src / Application / Commands / Import / ImportCustomersCommandHandler.php
ameliabooking / src / Application / Commands / Import Last commit date
ImportCustomersCommand.php 6 months ago ImportCustomersCommandHandler.php 3 months ago
ImportCustomersCommandHandler.php
201 lines
1 <?php
2
3 /**
4 * @copyright © Melograno Ventures. All rights reserved.
5 * @licence See LICENCE.md for license details.
6 */
7
8 namespace AmeliaBooking\Application\Commands\Import;
9
10 use AmeliaBooking\Application\Commands\CommandHandler;
11 use AmeliaBooking\Application\Commands\CommandResult;
12 use AmeliaBooking\Application\Common\Exceptions\AccessDeniedException;
13 use AmeliaBooking\Application\Services\User\CustomerApplicationService;
14 use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException;
15 use AmeliaBooking\Domain\Entity\Entities;
16 use AmeliaBooking\Domain\Entity\User\AbstractUser;
17 use AmeliaBooking\Domain\Entity\User\Customer;
18 use AmeliaBooking\Domain\Factory\User\UserFactory;
19 use AmeliaBooking\Domain\Services\Settings\SettingsService;
20 use AmeliaBooking\Domain\ValueObjects\Number\Integer\Id;
21 use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException;
22 use AmeliaBooking\Infrastructure\Repository\User\UserRepository;
23 use AmeliaBooking\Infrastructure\WP\Translations\BackendStrings;
24 use Exception;
25 use Interop\Container\Exception\ContainerException;
26 use Slim\Exception\ContainerValueNotFoundException;
27
28 /**
29 * Class ImportCustomersCommandHandler
30 *
31 * @package AmeliaBooking\Application\Commands\Import
32 */
33 class ImportCustomersCommandHandler extends CommandHandler
34 {
35 /**
36 * @param ImportCustomersCommand $command
37 *
38 * @return CommandResult
39 * @throws ContainerValueNotFoundException
40 * @throws AccessDeniedException
41 * @throws InvalidArgumentException
42 * @throws QueryExecutionException
43 * @throws ContainerException
44 * @throws Exception
45 */
46 public function handle(ImportCustomersCommand $command)
47 {
48 if (!$command->getPermissionService()->currentUserCanWrite(Entities::CUSTOMERS)) {
49 throw new AccessDeniedException('You are not allowed to read customers.');
50 }
51
52 $result = new CommandResult();
53
54 $this->checkMandatoryFields($command);
55
56 $data = $command->getField('data');
57 $num = $command->getField('number');
58
59 $overwriteUsers = $command->getField('overwrite');
60
61 /** @var UserRepository $userRepository */
62 $userRepository = $this->getContainer()->get('domain.users.repository');
63 /** @var CustomerApplicationService $customerApplicationService */
64 $customerApplicationService = $this->container->get('application.user.customer.service');
65 /** @var SettingsService $settingsDS */
66 $settingsDS = $this->container->get('domain.settings.service');
67
68 $dateFormat = $settingsDS->getSetting('wordpress', 'dateFormat');
69
70 $addedUsers = [];
71 $failedUsers = [];
72 $userExists = [];
73
74 $userRepository->beginTransaction();
75
76 if ($overwriteUsers) {
77 foreach ($overwriteUsers as $overwriteUser) {
78 try {
79 if (!empty($overwriteUser['birthday']) && !empty($overwriteUser['birthday']['date'])) {
80 $overwriteUser['birthday'] = (new \DateTime($overwriteUser['birthday']['date']))->format('Y-m-d');
81 }
82 } catch (Exception $e) {
83 $failedUsers[] = $overwriteUser;
84 continue;
85 }
86 $removeKeys = array('index', 'id', 'externalId', 'status');
87 foreach (array_keys($overwriteUser) as $key) {
88 if (in_array($key, $removeKeys) || empty($overwriteUser[$key])) {
89 unset($overwriteUser[$key]);
90 }
91 }
92 $email = $overwriteUser['email'] ;
93 unset($overwriteUser['email']);
94 if (!$userRepository->updateFieldsByEmail($email, $overwriteUser)) {
95 $failedUsers[] = $overwriteUser;
96 continue;
97 }
98 $addedUsers[] = $overwriteUser;
99 }
100 $userRepository->commit();
101
102 $result->setResult(CommandResult::RESULT_SUCCESS);
103 $result->setMessage('Successfully overwritten users');
104 $result->setData(
105 [
106 'addedUsers' => $addedUsers,
107 'failedToAdd' => $failedUsers,
108 'existsUsers' => []
109 ]
110 );
111
112 return $result;
113 }
114
115 $existingEmails = $userRepository->getAllEmailsByType('customer');
116
117 for ($i = 0; $i < $num; $i++) {
118 if ($data['firstName'][$i] === BackendStrings::get('first_name')) {
119 continue;
120 }
121 try {
122 $customerData = [
123 'firstName' => $data['firstName'][$i],
124 'lastName' => $data['lastName'][$i],
125 'email' => $data['email'][$i],
126 'phone' => !empty($data['phone']) ? $data['phone'][$i] : null,
127 'gender' => !empty($data['gender']) ? $data['gender'][$i] : null,
128 'note' => !empty($data['note']) ? $data['note'][$i] : null,
129 'type' => Entities::CUSTOMER
130 ];
131
132 if ($customerData['email']) {
133 $customerData['email'] = preg_replace('/\s+/', '', $customerData['email']);
134 }
135
136 if (
137 in_array($customerData['email'], array_column($userExists, 'email')) ||
138 in_array($customerData['email'], array_column($addedUsers, 'email'))
139 ) {
140 $failedUsers[] = array_merge($customerData, ['index' => $i]);
141 continue;
142 }
143
144 if (!empty($data['birthday']) && $data['birthday'][$i]) {
145 $d = \DateTime::createFromFormat($dateFormat, $data['birthday'][$i]);
146 if ($d) {
147 $customerData['birthday'] = $d->format('Y-m-d');
148 } else {
149 throw new Exception();
150 }
151 }
152
153 /** @var Customer $newUser */
154 $newUser = UserFactory::create($customerData);
155 } catch (Exception $e) {
156 $failedUsers[] = array_merge($customerData, ['index' => $i]);
157 continue;
158 }
159
160 if (!($newUser instanceof AbstractUser) || empty($newUser->getFirstName()->getValue()) || empty($newUser->getLastName()->getValue())) {
161 $failedUsers[] = array_merge($customerData, ['index' => $i]);
162 continue;
163 }
164
165 if ($newUser->getEmail() && $newUser->getEmail()->getValue() && in_array($newUser->getEmail()->getValue(), $existingEmails)) {
166 $userExists[] = array_merge($newUser->toArray(), ['index' => $i]);
167 continue;
168 }
169
170 try {
171 if (!$id = $userRepository->add($newUser)) {
172 $failedUsers[] = array_merge($newUser->toArray(), ['index' => $i]);
173 continue;
174 }
175 } catch (QueryExecutionException $e) {
176 $failedUsers[] = array_merge($newUser->toArray(), ['index' => $i]);
177 continue;
178 }
179
180 $newUser->setId(new Id($id));
181 $customerApplicationService->setWPUserForCustomer($newUser, true);
182
183 $addedUsers[] = $newUser;
184 }
185
186 $userRepository->commit();
187
188 $result->setResult(CommandResult::RESULT_SUCCESS);
189 $result->setMessage('Successfully imported users');
190 $result->setData(
191 [
192 'addedUsers' => $addedUsers,
193 'failedToAdd' => $failedUsers,
194 'existsUsers' => $userExists
195 ]
196 );
197
198 return $result;
199 }
200 }
201