PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / trunk
Booking for Appointments and Events Calendar – Amelia vtrunk
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 / Booking / Event / DeleteEventBookingCommandHandler.php
ameliabooking / src / Application / Commands / Booking / Event Last commit date
Tag 2 months ago AddEventCommand.php 1 year ago AddEventCommandHandler.php 2 weeks ago DeleteEventBookingCommand.php 7 years ago DeleteEventBookingCommandHandler.php 3 months ago DeleteEventCommand.php 1 year ago DeleteEventCommandHandler.php 6 months ago DeleteEventsCommand.php 6 months ago DeleteEventsCommandHandler.php 3 months ago GetCalendarEventsCommand.php 1 year ago GetCalendarEventsCommandHandler.php 1 year ago GetEventBookingCommand.php 6 months ago GetEventBookingCommandHandler.php 2 weeks ago GetEventBookingsCommand.php 1 year ago GetEventBookingsCommandHandler.php 3 months ago GetEventCommand.php 7 years ago GetEventCommandHandler.php 3 months ago GetEventsCommand.php 1 year ago GetEventsCommandHandler.php 2 weeks ago UpdateEventBookingCommand.php 7 years ago UpdateEventBookingCommandHandler.php 6 months ago UpdateEventCommand.php 1 year ago UpdateEventCommandHandler.php 3 months ago UpdateEventStatusCommand.php 1 year ago UpdateEventStatusCommandHandler.php 6 months ago UpdateEventVisibilityCommand.php 6 months ago UpdateEventVisibilityCommandHandler.php 6 months ago
DeleteEventBookingCommandHandler.php
173 lines
1 <?php
2
3 namespace AmeliaBooking\Application\Commands\Booking\Event;
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\Booking\BookingApplicationService;
9 use AmeliaBooking\Application\Services\Booking\EventApplicationService;
10 use AmeliaBooking\Application\Services\User\UserApplicationService;
11 use AmeliaBooking\Domain\Collection\Collection;
12 use AmeliaBooking\Domain\Common\Exceptions\AuthorizationException;
13 use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException;
14 use AmeliaBooking\Domain\Entity\Booking\Appointment\CustomerBooking;
15 use AmeliaBooking\Domain\Entity\Booking\Event\Event;
16 use AmeliaBooking\Domain\Entity\Entities;
17 use AmeliaBooking\Domain\Entity\User\AbstractUser;
18 use AmeliaBooking\Domain\Services\Settings\SettingsService;
19 use AmeliaBooking\Domain\ValueObjects\BooleanValueObject;
20 use AmeliaBooking\Domain\ValueObjects\String\BookingStatus;
21 use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException;
22 use AmeliaBooking\Infrastructure\Repository\Booking\Appointment\CustomerBookingRepository;
23 use AmeliaBooking\Infrastructure\Repository\Booking\Event\EventRepository;
24 use Interop\Container\Exception\ContainerException;
25 use Slim\Exception\ContainerValueNotFoundException;
26
27 /**
28 * Class DeleteEventBookingCommandHandler
29 *
30 * @package AmeliaBooking\Application\Commands\Booking\Event
31 */
32 class DeleteEventBookingCommandHandler extends CommandHandler
33 {
34 /**
35 * @param DeleteEventBookingCommand $command
36 *
37 * @return CommandResult
38 * @throws ContainerValueNotFoundException
39 * @throws AccessDeniedException
40 * @throws QueryExecutionException
41 * @throws ContainerException
42 * @throws InvalidArgumentException
43 */
44 public function handle(DeleteEventBookingCommand $command)
45 {
46 $result = new CommandResult();
47
48 /** @var UserApplicationService $userAS */
49 $userAS = $this->container->get('application.user.service');
50
51 /** @var SettingsService $settingsDS */
52 $settingsDS = $this->container->get('domain.settings.service');
53
54
55 $user = null;
56 if (!$command->getPermissionService()->currentUserCanDelete(Entities::EVENTS)) {
57 try {
58 /** @var AbstractUser $user */
59 $user = $userAS->authorization(
60 $command->getToken(),
61 Entities::PROVIDER
62 );
63 } catch (AuthorizationException $e) {
64 $result = new CommandResult();
65
66 $result->setResult(CommandResult::RESULT_ERROR);
67 $result->setData(
68 [
69 'reauthorize' => true
70 ]
71 );
72
73 return $result;
74 }
75
76 if ($userAS->isCustomer($user)) {
77 throw new AccessDeniedException('You are not allowed to delete event bookings');
78 }
79 }
80
81 /** @var CustomerBookingRepository $customerBookingRepository */
82 $customerBookingRepository = $this->container->get('domain.booking.customerBooking.repository');
83
84 /** @var EventRepository $eventRepository */
85 $eventRepository = $this->container->get('domain.booking.event.repository');
86
87 /** @var EventApplicationService $eventApplicationService */
88 $eventApplicationService = $this->container->get('application.booking.event.service');
89
90 /** @var BookingApplicationService $bookingAS */
91 $bookingAS = $this->container->get('application.booking.booking.service');
92
93 /** @var CustomerBooking $customerBooking */
94 $customerBooking = $customerBookingRepository->getById((int)$command->getField('id'));
95
96 /** @var Event $event */
97 $event = $eventRepository->getByBookingId(
98 $customerBooking->getId()->getValue(),
99 [
100 'fetchEventsTags' => true,
101 'fetchEventsProviders' => true,
102 'fetchBookingsCoupons' => true,
103 'fetchBookingsPayments' => true,
104 'fetchBookingsUsers' => true,
105 ]
106 );
107
108 $event->getBookings()->addItem($customerBooking, $customerBooking->getId()->getValue());
109
110 if (
111 $user &&
112 $userAS->isProvider($user) &&
113 (
114 !$settingsDS->getSetting('roles', 'allowWriteEvents') ||
115 (!$event->getProviders()->keyExists($user->getId()->getValue()) &&
116 (!$event->getOrganizerId() || $event->getOrganizerId()->getValue() !== $user->getId()->getValue()))
117 )
118 ) {
119 throw new AccessDeniedException('You are not allowed to delete this booking');
120 }
121
122 $customerBookingRepository->beginTransaction();
123
124 do_action('amelia_before_event_booking_deleted', $customerBooking->toArray(), $event ? $event->toArray() : null);
125
126 if (!$eventApplicationService->deleteEventBooking($customerBooking)) {
127 $customerBookingRepository->rollback();
128
129 $result->setResult(CommandResult::RESULT_ERROR);
130 $result->setMessage('Could not delete booking');
131
132 return $result;
133 }
134
135 $customerBookingRepository->commit();
136
137 $event->setNotifyParticipants(
138 $bookingAS->isBookingApprovedOrPending($customerBooking->getStatus()->getValue())
139 );
140
141 $customerBooking->setChangedStatus(
142 new BooleanValueObject(
143 $bookingAS->isBookingApprovedOrPending($customerBooking->getStatus()->getValue())
144 )
145 );
146
147 $customerBooking->setStatus(new BookingStatus(BookingStatus::REJECTED));
148
149 /** @var Collection $payments */
150 $payments = $event->getBookings()->getItem($event->getBookings()->keys()[0])->getPayments();
151
152 if ($payments && count($payments->getItems())) {
153 $customerBooking->setPayments($payments);
154 }
155
156
157 do_action('amelia_after_event_booking_deleted', $customerBooking->toArray(), $event->toArray());
158
159 $result->setResult(CommandResult::RESULT_SUCCESS);
160 $result->setMessage('Successfully deleted event booking');
161 $result->setData(
162 [
163 'type' => Entities::EVENT,
164 Entities::EVENT => $event->toArray(),
165 Entities::BOOKING => $customerBooking->toArray(),
166 'appointmentStatusChanged' => false
167 ]
168 );
169
170 return $result;
171 }
172 }
173