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 / Bookable / Service / UpdateServicesPositionsCommandHandler.php
ameliabooking / src / Application / Commands / Bookable / Service Last commit date
AddServiceCommand.php 1 year ago AddServiceCommandHandler.php 1 year ago DeleteServiceCommand.php 1 year ago DeleteServiceCommandHandler.php 1 year ago GetServiceCommand.php 1 year ago GetServiceCommandHandler.php 1 year ago GetServiceDeleteEffectCommand.php 1 year ago GetServiceDeleteEffectCommandHandler.php 2 years ago GetServicesCommand.php 1 year ago GetServicesCommandHandler.php 1 year ago UpdateServiceCommand.php 1 year ago UpdateServiceCommandHandler.php 1 year ago UpdateServiceStatusCommand.php 1 year ago UpdateServiceStatusCommandHandler.php 1 year ago UpdateServicesPositionsCommand.php 1 year ago UpdateServicesPositionsCommandHandler.php 1 year ago
UpdateServicesPositionsCommandHandler.php
104 lines
1 <?php
2
3 namespace AmeliaBooking\Application\Commands\Bookable\Service;
4
5 use AmeliaBooking\Application\Commands\CommandHandler;
6 use AmeliaBooking\Application\Commands\CommandResult;
7 use AmeliaBooking\Application\Common\Exceptions\AccessDeniedException;
8 use AmeliaBooking\Domain\Collection\Collection;
9 use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException;
10 use AmeliaBooking\Domain\Entity\Entities;
11 use AmeliaBooking\Domain\Services\Settings\SettingsService;
12 use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException;
13 use AmeliaBooking\Infrastructure\Repository\Bookable\Service\ServiceRepository;
14 use Interop\Container\Exception\ContainerException;
15 use Slim\Exception\ContainerValueNotFoundException;
16
17 /**
18 * Class UpdateServicesPositionsCommandHandler
19 *
20 * @package AmeliaBooking\Application\Commands\Bookable\Category
21 */
22 class UpdateServicesPositionsCommandHandler extends CommandHandler
23 {
24 /**
25 * @param UpdateServicesPositionsCommand $command
26 *
27 * @return CommandResult
28 * @throws ContainerValueNotFoundException
29 * @throws AccessDeniedException
30 * @throws QueryExecutionException
31 * @throws ContainerException
32 * @throws InvalidArgumentException
33 */
34 public function handle(UpdateServicesPositionsCommand $command)
35 {
36 if (!$command->getPermissionService()->currentUserCanWrite(Entities::SERVICES)) {
37 throw new AccessDeniedException('You are not allowed to update bookable services positions.');
38 }
39
40 $result = new CommandResult();
41
42 /** @var ServiceRepository $serviceRepository */
43 $serviceRepository = $this->container->get('domain.bookable.service.repository');
44
45 /** @var Collection $services */
46 $services = $serviceRepository->getFiltered(['sort' => $command->getFields()['sorting']]);
47
48 $servicesArray = $services->toArray();
49
50 if (
51 $command->getFields()['sorting'] === 'custom' &&
52 $customSortedServicesArray = $command->getFields()['services']
53 ) {
54 $customSortedServicesIds = array_column($customSortedServicesArray, 'id');
55
56 $sortedServicesArray = [];
57
58 foreach ($servicesArray as $serviceArray) {
59 if (in_array($serviceArray['id'], $customSortedServicesIds, false)) {
60 $sortedServicesArray[] = null;
61 } else {
62 $sortedServicesArray[] = $serviceArray;
63 }
64 }
65
66 foreach ($sortedServicesArray as $index => $serviceArray) {
67 if ($serviceArray === null) {
68 $sortedServicesArray[$index] = array_shift($customSortedServicesArray);
69 }
70 }
71
72 $servicesArray = $sortedServicesArray;
73 }
74
75 $serviceRepository->beginTransaction();
76
77 $servicesArray = apply_filters('amelia_before_service_position_updated_filter', $servicesArray);
78
79 do_action('amelia_before_service_position_updated', $servicesArray);
80
81 foreach ($servicesArray as $index => $serviceArray) {
82 $serviceRepository->updateFieldById($serviceArray['id'], $index + 1, 'position');
83 }
84
85 $serviceRepository->commit();
86
87 do_action('amelia_after_service_position_updated', $servicesArray);
88
89 /** @var SettingsService $settingsService */
90 $settingsService = $this->getContainer()->get('domain.settings.service');
91
92 $settings = $settingsService->getAllSettingsCategorized();
93
94 $settings['general']['sortingServices'] = $command->getFields()['sorting'];
95
96 $settingsService->setAllSettings($settings);
97
98 $result->setResult(CommandResult::RESULT_SUCCESS);
99 $result->setMessage('Successfully updated bookable services positions.');
100
101 return $result;
102 }
103 }
104