PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 1.2.25
Booking for Appointments and Events Calendar – Amelia v1.2.25
2.4.9 2.4.8 2.4.7 2.4.6 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 7 years ago AddCustomerCommandHandler.php 1 year ago GetCustomerCommand.php 7 years ago GetCustomerCommandHandler.php 1 year ago GetCustomersCommand.php 7 years ago GetCustomersCommandHandler.php 1 year ago UpdateCustomerCommand.php 7 years ago UpdateCustomerCommandHandler.php 1 year ago UpdateCustomerStatusCommand.php 1 year ago UpdateCustomerStatusCommandHandler.php 1 year ago
GetCustomersCommandHandler.php
166 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 (!$command->getPermissionService()->currentUserCanRead(Entities::CUSTOMERS) &&
49 !($currentUser && $currentUser->getType() === AbstractUser::USER_ROLE_PROVIDER)
50 ) {
51 if ($command->getToken()) {
52 /** @var UserApplicationService $userAS */
53 $userAS = $this->container->get('application.user.service');
54
55 try {
56 $currentUser = $userAS->authorization($command->getToken(), 'provider');
57 } catch (AuthorizationException $e) {
58 $result->setResult(CommandResult::RESULT_ERROR);
59 $result->setData(
60 [
61 'reauthorize' => true
62 ]
63 );
64
65 return $result;
66 }
67 } else {
68 throw new AccessDeniedException('You are not allowed to read customers.');
69 }
70 }
71
72 /** @var CustomerRepository $customerRepository */
73 $customerRepository = $this->getContainer()->get('domain.users.customers.repository');
74
75 /** @var SettingsService $settingsService */
76 $settingsService = $this->container->get('domain.settings.service');
77
78 /** @var ProviderApplicationService $providerAS */
79 $providerAS = $this->container->get('application.user.provider.service');
80
81 $params = $command->getField('params');
82
83 $countParams = [];
84
85 if (empty($params['customers']) &&
86 !$command->getPermissionService()->currentUserCanReadOthers(Entities::CUSTOMERS)
87 ) {
88 /** @var Collection $providerCustomers */
89 $providerCustomers = $providerAS->getAllowedCustomers($currentUser);
90
91 $params['customers'] = array_column($providerCustomers->toArray(), 'id');
92
93 $countParams['customers'] = $params['customers'];
94 }
95
96 $itemsPerPage = !empty($params['limit']) ?
97 $params['limit'] : $settingsService->getSetting('general', 'itemsPerPageBackEnd');
98
99 $users = $customerRepository->getFiltered(
100 array_merge($params, ['ignoredBookings' => empty($params['noShow'])]),
101 $itemsPerPage
102 );
103
104 if (!empty($users)) {
105 $usersWithBookingsStats = $customerRepository->getFiltered(
106 array_merge($params, ['ignoredBookings' => false, 'customers' => array_keys($users)]),
107 null
108 );
109
110 foreach ($users as $key => $user) {
111 $users[$key] = $usersWithBookingsStats[$key];
112 }
113 }
114
115 $customersNoShowCount = [];
116
117 $noShowTagEnabled = $settingsService->getSetting('roles', 'enableNoShowTag');
118
119 if ($noShowTagEnabled && $users) {
120 /** @var CustomerBookingRepository $bookingRepository */
121 $bookingRepository = $this->container->get('domain.booking.customerBooking.repository');
122
123 $usersIds = array_map(function ($user) { return $user['id']; }, $users);
124
125 $customersNoShowCount = $usersIds ? $bookingRepository->countByNoShowStatus($usersIds) : [];
126 }
127
128 $users = array_values($users);
129
130 foreach ($users as $key => &$user) {
131 $user['wpUserPhotoUrl'] = $this->container->get('user.avatar')->getAvatar($user['externalId']);
132
133 if ($noShowTagEnabled) {
134 $user['noShowCount'] = $customersNoShowCount[$key]['count'];
135 }
136
137 $user = array_map(
138 function ($v) {
139 return (null === $v) ? '' : $v;
140 },
141 $user
142 );
143 }
144
145 $users = apply_filters('amelia_get_customers_filter', $users);
146
147 do_action('amelia_get_customers', $users);
148
149 $resultData = [
150 Entities::USER . 's' => $users,
151 ];
152
153 if (empty($params['skipCount'])) {
154 $resultData['filteredCount'] = (int)$customerRepository->getCount($params);
155
156 $resultData['totalCount'] = (int)$customerRepository->getCount($countParams);
157 }
158
159 $result->setResult(CommandResult::RESULT_SUCCESS);
160 $result->setMessage('Successfully retrieved users.');
161 $result->setData($resultData);
162
163 return $result;
164 }
165 }
166