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