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 / Bookable / Service / UpdateServiceCommandHandler.php
ameliabooking / src / Application / Commands / Bookable / Service Last commit date
AddServiceCommand.php 7 years ago AddServiceCommandHandler.php 2 years ago DeleteServiceCommand.php 7 years ago DeleteServiceCommandHandler.php 2 years ago GetServiceCommand.php 7 years ago GetServiceCommandHandler.php 2 years ago GetServiceDeleteEffectCommand.php 7 years ago GetServiceDeleteEffectCommandHandler.php 2 years ago GetServicesCommand.php 7 years ago GetServicesCommandHandler.php 2 years ago UpdateServiceCommand.php 7 years ago UpdateServiceCommandHandler.php 2 years ago UpdateServiceStatusCommand.php 7 years ago UpdateServiceStatusCommandHandler.php 2 years ago UpdateServicesPositionsCommand.php 7 years ago UpdateServicesPositionsCommandHandler.php 2 years ago
UpdateServiceCommandHandler.php
175 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\Bookable\Service;
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\Bookable\BookableApplicationService;
13 use AmeliaBooking\Application\Services\Extra\AbstractExtraApplicationService;
14 use AmeliaBooking\Application\Services\Gallery\GalleryApplicationService;
15 use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException;
16 use AmeliaBooking\Domain\Entity\Bookable\Service\Category;
17 use AmeliaBooking\Domain\Entity\Bookable\Service\Service;
18 use AmeliaBooking\Domain\Entity\Entities;
19 use AmeliaBooking\Domain\Factory\Bookable\Service\ServiceFactory;
20 use AmeliaBooking\Domain\Services\Settings\SettingsService;
21 use AmeliaBooking\Domain\ValueObjects\Json;
22 use AmeliaBooking\Infrastructure\Common\Exceptions\NotFoundException;
23 use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException;
24 use AmeliaBooking\Infrastructure\Repository\Bookable\Service\CategoryRepository;
25 use AmeliaBooking\Infrastructure\Repository\Bookable\Service\ProviderServiceRepository;
26 use AmeliaBooking\Infrastructure\Repository\Bookable\Service\ServiceRepository;
27 use Interop\Container\Exception\ContainerException;
28 use Slim\Exception\ContainerValueNotFoundException;
29
30 /**
31 * Class UpdateServiceCommandHandler
32 *
33 * @package AmeliaBooking\Application\Commands\Bookable\Service
34 */
35 class UpdateServiceCommandHandler extends CommandHandler
36 {
37 /** @var array */
38 public $mandatoryFields = [
39 'categoryId',
40 'duration',
41 'maxCapacity',
42 'minCapacity',
43 'name',
44 'price',
45 'applyGlobally',
46 'providers'
47 ];
48
49 /**
50 * @param UpdateServiceCommand $command
51 *
52 * @return CommandResult
53 * @throws ContainerValueNotFoundException
54 * @throws InvalidArgumentException
55 * @throws QueryExecutionException
56 * @throws AccessDeniedException
57 * @throws ContainerException
58 * @throws NotFoundException
59 */
60 public function handle(UpdateServiceCommand $command)
61 {
62 if (!$command->getPermissionService()->currentUserCanWrite(Entities::SERVICES)) {
63 throw new AccessDeniedException('You are not allowed to update service.');
64 }
65
66 $result = new CommandResult();
67
68 $this->checkMandatoryFields($command);
69
70 $serviceData = $command->getFields();
71
72 $entityService = $this->container->get('application.entity.service');
73
74 $entityService->removeMissingEntitiesForService($serviceData);
75
76 $serviceData = apply_filters('amelia_before_service_updated_filter', $serviceData);
77
78 do_action('amelia_before_service_updated', $serviceData);
79
80 /** @var Service $service */
81 $service = ServiceFactory::create($serviceData);
82
83 /** @var SettingsService $settingsService */
84 $settingsService = $this->container->get('domain.settings.service');
85
86 if ($service->getSettings()) {
87 $newSettings = new Json(
88 json_encode(
89 array_merge(
90 json_decode($service->getSettings()->getValue(), true),
91 ['activation' => ['version' => $settingsService->getSetting('activation', 'version')]]
92 )
93 )
94 );
95
96 $service->setSettings($newSettings);
97 }
98
99 if (!($service instanceof Service)) {
100 $result->setResult(CommandResult::RESULT_ERROR);
101 $result->setMessage('Unable to update service.');
102
103 return $result;
104 }
105
106 /** @var ServiceRepository $serviceRepository */
107 $serviceRepository = $this->container->get('domain.bookable.service.repository');
108 /** @var ProviderServiceRepository $providerServiceRepository */
109 $providerServiceRepository = $this->container->get('domain.bookable.service.providerService.repository');
110 /** @var CategoryRepository $categoryRepository */
111 $categoryRepository = $this->container->get('domain.bookable.category.repository');
112
113 /** @var Category $category */
114 $category = $categoryRepository->getById($service->getCategoryId()->getValue());
115
116 /** @var BookableApplicationService $bookableService */
117 $bookableService = $this->container->get('application.bookable.service');
118 /** @var AbstractExtraApplicationService $extraService */
119 $extraService = $this->container->get('application.extra.service');
120 /** @var GalleryApplicationService $galleryService */
121 $galleryService = $this->container->get('application.gallery.service');
122
123 $serviceRepository->beginTransaction();
124
125 if ($command->getField('applyGlobally') &&
126 !$providerServiceRepository->updateServiceForAllProviders($service, $command->getArg('id'))) {
127 $serviceRepository->rollback();
128
129 $result->setResult(CommandResult::RESULT_ERROR);
130 $result->setMessage('Unable to update service.');
131
132 return $result;
133 }
134
135 if (!$category || !$serviceRepository->update($command->getArg('id'), $service)) {
136 $serviceRepository->rollback();
137
138 $result->setResult(CommandResult::RESULT_ERROR);
139 $result->setMessage('Unable to update service.');
140
141 return $result;
142 }
143
144 $bookableService->manageProvidersForServiceUpdate(
145 $service,
146 $serviceData['providers'],
147 !$command->getField('applyGlobally')
148 );
149
150 $extraService->manageExtrasForServiceUpdate($service);
151
152 $bookableService->managePackagesForServiceUpdate($service);
153
154 $galleryService->manageGalleryForEntityUpdate(
155 $service->getGallery(),
156 $command->getArg('id'),
157 Entities::SERVICE
158 );
159
160 $serviceRepository->commit();
161
162 do_action('amelia_after_service_updated', $service->toArray());
163
164 $result->setResult(CommandResult::RESULT_SUCCESS);
165 $result->setMessage('Successfully updated service.');
166 $result->setData(
167 [
168 Entities::SERVICE => $service->toArray(),
169 ]
170 );
171
172 return $result;
173 }
174 }
175