PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 1.2.31
Booking for Appointments and Events Calendar – Amelia v1.2.31
2.4.5 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 / User / Customer / GetCustomersCommandHandler.php
ameliabooking / src / Application / Commands / User / Customer Last commit date
AddCustomerCommand.php 1 year ago AddCustomerCommandHandler.php 1 year ago GetCustomerCommand.php 1 year ago GetCustomerCommandHandler.php 1 year ago GetCustomersCommand.php 1 year ago GetCustomersCommandHandler.php 1 year ago UpdateCustomerCommand.php 1 year ago UpdateCustomerCommandHandler.php 1 year ago UpdateCustomerStatusCommand.php 1 year ago UpdateCustomerStatusCommandHandler.php 1 year ago
GetCustomersCommandHandler.php
173 lines
1 <?php
2
3 namespace AmeliaBooking\Application\Commands\User\Customer;
4
5 use AmeliaBooking\Application\Commands\CommandHandler;
6 use AmeliaBooking\Application\Commands\CommandResult;
7 use AmeliaBooking\Application\Common\Exceptions\AccessDeniedException;
8 use AmeliaBooking\Application\Services\User\ProviderApplicationService;
9 use AmeliaBooking\Application\Services\User\UserApplicationService;
10 use AmeliaBooking\Domain\Collection\Collection;
11 use AmeliaBooking\Domain\Common\Exceptions\AuthorizationException;
12 use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException;
13 use AmeliaBooking\Domain\Entity\Entities;
14 use AmeliaBooking\Domain\Entity\User\AbstractUser;
15 use AmeliaBooking\Domain\Services\Settings\SettingsService;
16 use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException;
17 use AmeliaBooking\Infrastructure\Repository\Booking\Appointment\CustomerBookingRepository;
18 use AmeliaBooking\Infrastructure\Repository\User\CustomerRepository;
19 use Exception;
20 use Interop\Container\Exception\ContainerException;
21 use Slim\Exception\ContainerValueNotFoundException;
22
23 /**
24 * Class GetCustomersCommandHandler
25 *
26 * @package AmeliaBooking\Application\Commands\User\Customer
27 */
28 class GetCustomersCommandHandler extends CommandHandler
29 {
30 /**
31 * @param GetCustomersCommand $command
32 *
33 * @return CommandResult
34 * @throws InvalidArgumentException
35 * @throws ContainerValueNotFoundException
36 * @throws QueryExecutionException
37 * @throws ContainerException
38 * @throws Exception
39 * @throws AccessDeniedException
40 */
41 public function handle(GetCustomersCommand $command)
42 {
43 $result = new CommandResult();
44
45 /** @var AbstractUser $currentUser */
46 $currentUser = $this->container->get('logged.in.user');
47
48 if (
49 !$command->getPermissionService()->currentUserCanRead(Entities::CUSTOMERS) &&
50 !($currentUser && $currentUser->getType() === AbstractUser::USER_ROLE_PROVIDER)
51 ) {
52 if ($command->getToken()) {
53 /** @var UserApplicationService $userAS */
54 $userAS = $this->container->get('application.user.service');
55
56 try {
57 $currentUser = $userAS->authorization($command->getToken(), 'provider');
58 } catch (AuthorizationException $e) {
59 $result->setResult(CommandResult::RESULT_ERROR);
60 $result->setData(
61 [
62 'reauthorize' => true
63 ]
64 );
65
66 return $result;
67 }
68 } else {
69 throw new AccessDeniedException('You are not allowed to read customers.');
70 }
71 }
72
73 /** @var CustomerRepository $customerRepository */
74 $customerRepository = $this->getContainer()->get('domain.users.customers.repository');
75
76 /** @var SettingsService $settingsService */
77 $settingsService = $this->container->get('domain.settings.service');
78
79 /** @var ProviderApplicationService $providerAS */
80 $providerAS = $this->container->get('application.user.provider.service');
81
82 $params = $command->getField('params');
83
84 $countParams = [];
85
86 if (
87 empty($params['customers']) &&
88 !$command->getPermissionService()->currentUserCanReadOthers(Entities::CUSTOMERS)
89 ) {
90 /** @var Collection $providerCustomers */
91 $providerCustomers = $providerAS->getAllowedCustomers($currentUser);
92
93 $params['customers'] = array_column($providerCustomers->toArray(), 'id');
94
95 $countParams['customers'] = $params['customers'];
96 }
97
98 $itemsPerPage = !empty($params['limit']) ?
99 $params['limit'] : $settingsService->getSetting('general', 'itemsPerPageBackEnd');
100
101 $users = $customerRepository->getFiltered(
102 array_merge($params, ['ignoredBookings' => empty($params['noShow'])]),
103 $itemsPerPage
104 );
105
106 if (!empty($users)) {
107 $usersWithBookingsStats = $customerRepository->getFiltered(
108 array_merge($params, ['ignoredBookings' => false, 'customers' => array_keys($users)]),
109 null
110 );
111
112 foreach ($users as $key => $user) {
113 $users[$key] = $usersWithBookingsStats[$key];
114 }
115 }
116
117 $customersNoShowCount = [];
118
119 $noShowTagEnabled = $settingsService->getSetting('roles', 'enableNoShowTag');
120
121 if ($noShowTagEnabled && $users) {
122 /** @var CustomerBookingRepository $bookingRepository */
123 $bookingRepository = $this->container->get('domain.booking.customerBooking.repository');
124
125 $usersIds = array_map(
126 function ($user) {
127 return $user['id'];
128 },
129 $users
130 );
131
132 $customersNoShowCount = $usersIds ? $bookingRepository->countByNoShowStatus($usersIds) : [];
133 }
134
135 $users = array_values($users);
136
137 foreach ($users as $key => &$user) {
138 $user['wpUserPhotoUrl'] = $this->container->get('user.avatar')->getAvatar($user['externalId']);
139
140 if ($noShowTagEnabled) {
141 $user['noShowCount'] = $customersNoShowCount[$key]['count'];
142 }
143
144 $user = array_map(
145 function ($v) {
146 return (null === $v) ? '' : $v;
147 },
148 $user
149 );
150 }
151
152 $users = apply_filters('amelia_get_customers_filter', $users);
153
154 do_action('amelia_get_customers', $users);
155
156 $resultData = [
157 Entities::USER . 's' => $users,
158 ];
159
160 if (empty($params['skipCount'])) {
161 $resultData['filteredCount'] = (int)$customerRepository->getCount($params);
162
163 $resultData['totalCount'] = (int)$customerRepository->getCount($countParams);
164 }
165
166 $result->setResult(CommandResult::RESULT_SUCCESS);
167 $result->setMessage('Successfully retrieved users.');
168 $result->setData($resultData);
169
170 return $result;
171 }
172 }
173