PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 2.0.2
Booking for Appointments and Events Calendar – Amelia v2.0.2
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 6 months ago
ImportCustomersCommandHandler.php
197 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 Exception;
24 use Interop\Container\Exception\ContainerException;
25 use Slim\Exception\ContainerValueNotFoundException;
26
27 /**
28 * Class ImportCustomersCommandHandler
29 *
30 * @package AmeliaBooking\Application\Commands\Import
31 */
32 class ImportCustomersCommandHandler extends CommandHandler
33 {
34 /**
35 * @param ImportCustomersCommand $command
36 *
37 * @return CommandResult
38 * @throws ContainerValueNotFoundException
39 * @throws AccessDeniedException
40 * @throws InvalidArgumentException
41 * @throws QueryExecutionException
42 * @throws ContainerException
43 * @throws Exception
44 */
45 public function handle(ImportCustomersCommand $command)
46 {
47 if (!$command->getPermissionService()->currentUserCanWrite(Entities::CUSTOMERS)) {
48 throw new AccessDeniedException('You are not allowed to read customers.');
49 }
50
51 $result = new CommandResult();
52
53 $this->checkMandatoryFields($command);
54
55 $data = $command->getField('data');
56 $num = $command->getField('number');
57
58 $overwriteUsers = $command->getField('overwrite');
59
60 /** @var UserRepository $userRepository */
61 $userRepository = $this->getContainer()->get('domain.users.repository');
62 /** @var CustomerApplicationService $customerApplicationService */
63 $customerApplicationService = $this->container->get('application.user.customer.service');
64 /** @var SettingsService $settingsDS */
65 $settingsDS = $this->container->get('domain.settings.service');
66
67 $dateFormat = $settingsDS->getSetting('wordpress', 'dateFormat');
68
69 $addedUsers = [];
70 $failedUsers = [];
71 $userExists = [];
72
73 $userRepository->beginTransaction();
74
75 if ($overwriteUsers) {
76 foreach ($overwriteUsers as $overwriteUser) {
77 try {
78 if (!empty($overwriteUser['birthday']) && !empty($overwriteUser['birthday']['date'])) {
79 $overwriteUser['birthday'] = (new \DateTime($overwriteUser['birthday']['date']))->format('Y-m-d');
80 }
81 } catch (Exception $e) {
82 $failedUsers[] = $overwriteUser;
83 continue;
84 }
85 $removeKeys = array('index', 'id', 'externalId', 'status');
86 foreach (array_keys($overwriteUser) as $key) {
87 if (in_array($key, $removeKeys) || empty($overwriteUser[$key])) {
88 unset($overwriteUser[$key]);
89 }
90 }
91 $email = $overwriteUser['email'] ;
92 unset($overwriteUser['email']);
93 if (!$userRepository->updateFieldsByEmail($email, $overwriteUser)) {
94 $failedUsers[] = $overwriteUser;
95 continue;
96 }
97 $addedUsers[] = $overwriteUser;
98 }
99 $userRepository->commit();
100
101 $result->setResult(CommandResult::RESULT_SUCCESS);
102 $result->setMessage('Successfully overwritten users');
103 $result->setData(
104 [
105 'addedUsers' => $addedUsers,
106 'failedToAdd' => $failedUsers,
107 'existsUsers' => []
108 ]
109 );
110
111 return $result;
112 }
113
114 $existingEmails = $userRepository->getAllEmailsByType('customer');
115
116 for ($i = 0; $i < $num; $i++) {
117 try {
118 $customerData = [
119 'firstName' => $data['firstName'][$i],
120 'lastName' => $data['lastName'][$i],
121 'email' => $data['email'][$i],
122 'phone' => !empty($data['phone']) ? $data['phone'][$i] : null,
123 'gender' => !empty($data['gender']) ? $data['gender'][$i] : null,
124 'note' => !empty($data['note']) ? $data['note'][$i] : null,
125 'type' => Entities::CUSTOMER
126 ];
127
128 if ($customerData['email']) {
129 $customerData['email'] = preg_replace('/\s+/', '', $customerData['email']);
130 }
131
132 if (
133 in_array($customerData['email'], array_column($userExists, 'email')) ||
134 in_array($customerData['email'], array_column($addedUsers, 'email'))
135 ) {
136 $failedUsers[] = array_merge($customerData, ['index' => $i]);
137 continue;
138 }
139
140 if (!empty($data['birthday']) && $data['birthday'][$i]) {
141 $d = \DateTime::createFromFormat($dateFormat, $data['birthday'][$i]);
142 if ($d) {
143 $customerData['birthday'] = $d->format('Y-m-d');
144 } else {
145 throw new Exception();
146 }
147 }
148
149 /** @var Customer $newUser */
150 $newUser = UserFactory::create($customerData);
151 } catch (Exception $e) {
152 $failedUsers[] = array_merge($customerData, ['index' => $i]);
153 continue;
154 }
155
156 if (!($newUser instanceof AbstractUser) || empty($newUser->getFirstName()->getValue()) || empty($newUser->getLastName()->getValue())) {
157 $failedUsers[] = array_merge($customerData, ['index' => $i]);
158 continue;
159 }
160
161 if ($newUser->getEmail() && $newUser->getEmail()->getValue() && in_array($newUser->getEmail()->getValue(), $existingEmails)) {
162 $userExists[] = array_merge($newUser->toArray(), ['index' => $i]);
163 continue;
164 }
165
166 try {
167 if (!$id = $userRepository->add($newUser)) {
168 $failedUsers[] = array_merge($newUser->toArray(), ['index' => $i]);
169 continue;
170 }
171 } catch (QueryExecutionException $e) {
172 $failedUsers[] = array_merge($newUser->toArray(), ['index' => $i]);
173 continue;
174 }
175
176 $newUser->setId(new Id($id));
177 $customerApplicationService->setWPUserForCustomer($newUser, true);
178
179 $addedUsers[] = $newUser;
180 }
181
182 $userRepository->commit();
183
184 $result->setResult(CommandResult::RESULT_SUCCESS);
185 $result->setMessage('Successfully imported users');
186 $result->setData(
187 [
188 'addedUsers' => $addedUsers,
189 'failedToAdd' => $failedUsers,
190 'existsUsers' => $userExists
191 ]
192 );
193
194 return $result;
195 }
196 }
197