PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 1.2.20
Booking for Appointments and Events Calendar – Amelia v1.2.20
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 4 years ago ImportCustomersCommandHandler.php 2 years ago
ImportCustomersCommandHandler.php
194 lines
1 <?php
2 /**
3 * @copyright © TMS-Plugins. All rights reserved.
4 * @licence See LICENCE.md for license details.
5 */
6
7 namespace AmeliaBooking\Application\Commands\Import;
8
9 use AmeliaBooking\Application\Commands\CommandHandler;
10 use AmeliaBooking\Application\Commands\CommandResult;
11 use AmeliaBooking\Application\Common\Exceptions\AccessDeniedException;
12 use AmeliaBooking\Application\Services\User\CustomerApplicationService;
13 use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException;
14 use AmeliaBooking\Domain\Entity\Entities;
15 use AmeliaBooking\Domain\Entity\User\AbstractUser;
16 use AmeliaBooking\Domain\Entity\User\Customer;
17 use AmeliaBooking\Domain\Factory\User\UserFactory;
18 use AmeliaBooking\Domain\Services\Settings\SettingsService;
19 use AmeliaBooking\Domain\ValueObjects\Number\Integer\Id;
20 use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException;
21 use AmeliaBooking\Infrastructure\Repository\User\UserRepository;
22 use Exception;
23 use Interop\Container\Exception\ContainerException;
24 use Slim\Exception\ContainerValueNotFoundException;
25
26 /**
27 * Class ImportCustomersCommandHandler
28 *
29 * @package AmeliaBooking\Application\Commands\Import
30 */
31 class ImportCustomersCommandHandler extends CommandHandler
32 {
33 /**
34 * @param ImportCustomersCommand $command
35 *
36 * @return CommandResult
37 * @throws ContainerValueNotFoundException
38 * @throws AccessDeniedException
39 * @throws InvalidArgumentException
40 * @throws QueryExecutionException
41 * @throws ContainerException
42 * @throws Exception
43 */
44 public function handle(ImportCustomersCommand $command)
45 {
46 if (!$command->getPermissionService()->currentUserCanWrite(Entities::CUSTOMERS)) {
47 throw new AccessDeniedException('You are not allowed to read customers.');
48 }
49
50 $result = new CommandResult();
51
52 $this->checkMandatoryFields($command);
53
54 $data = $command->getField('data');
55 $num = $command->getField('number');
56
57 $overwriteUsers = $command->getField('overwrite');
58
59 /** @var UserRepository $userRepository */
60 $userRepository = $this->getContainer()->get('domain.users.repository');
61 /** @var CustomerApplicationService $customerApplicationService */
62 $customerApplicationService = $this->container->get('application.user.customer.service');
63 /** @var SettingsService $settingsDS */
64 $settingsDS = $this->container->get('domain.settings.service');
65
66 $dateFormat = $settingsDS->getSetting('wordpress', 'dateFormat');
67
68 $addedUsers = [];
69 $failedUsers = [];
70 $userExists = [];
71
72 $userRepository->beginTransaction();
73
74 if ($overwriteUsers) {
75 foreach ($overwriteUsers as $overwriteUser) {
76 /** @var Customer $newUser */
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[] = $newUser;
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 (in_array($customerData['email'], array_column($userExists, 'email')) || in_array($customerData['email'], array_column($addedUsers, 'email'))) {
133 $failedUsers[] = array_merge($customerData, ['index' => $i]);
134 continue;
135 }
136
137 if (!empty($data['birthday']) && $data['birthday'][$i]) {
138 $d = \DateTime::createFromFormat($dateFormat, $data['birthday'][$i]);
139 if ($d) {
140 $customerData['birthday'] = $d->format('Y-m-d');
141 } else {
142 throw new Exception();
143 }
144 }
145
146 /** @var Customer $newUser */
147 $newUser = UserFactory::create($customerData);
148 } catch (Exception $e) {
149 $failedUsers[] = array_merge($customerData, ['index' => $i]);
150 continue;
151 }
152
153 if (!($newUser instanceof AbstractUser) || empty($newUser->getFirstName()->getValue()) || empty($newUser->getLastName()->getValue())) {
154 $failedUsers[] = array_merge($customerData, ['index' => $i]);
155 continue;
156 }
157
158 if ($newUser->getEmail() && $newUser->getEmail()->getValue() && in_array($newUser->getEmail()->getValue(), $existingEmails)) {
159 $userExists[] = array_merge($newUser->toArray(), ['index' => $i]);
160 continue;
161 }
162
163 try {
164 if (!$id = $userRepository->add($newUser)) {
165 $failedUsers[] = array_merge($newUser->toArray(), ['index' => $i]);
166 continue;
167 }
168 } catch (QueryExecutionException $e) {
169 $failedUsers[] = array_merge($newUser->toArray(), ['index' => $i]);
170 continue;
171 }
172
173 $newUser->setId(new Id($id));
174 $customerApplicationService->setWPUserForCustomer($newUser, true);
175
176 $addedUsers[] = $newUser;
177 }
178
179 $userRepository->commit();
180
181 $result->setResult(CommandResult::RESULT_SUCCESS);
182 $result->setMessage('Successfully imported users');
183 $result->setData(
184 [
185 'addedUsers' => $addedUsers,
186 'failedToAdd' => $failedUsers,
187 'existsUsers' => $userExists
188 ]
189 );
190
191 return $result;
192 }
193 }
194