PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 2.4.6
Booking for Appointments and Events Calendar – Amelia v2.4.6
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 / Booking / Event / GetCalendarEventsCommandHandler.php
ameliabooking / src / Application / Commands / Booking / Event Last commit date
Tag 3 months ago AddEventCommand.php 1 year ago AddEventCommandHandler.php 1 week ago DeleteEventBookingCommand.php 7 years ago DeleteEventBookingCommandHandler.php 1 week ago DeleteEventCommand.php 1 year ago DeleteEventCommandHandler.php 7 months ago DeleteEventsCommand.php 7 months ago DeleteEventsCommandHandler.php 4 months ago GenerateEventWpaCustomPostCommand.php 4 weeks ago GenerateEventWpaCustomPostCommandHandler.php 4 weeks ago GetCalendarEventsCommand.php 1 year ago GetCalendarEventsCommandHandler.php 1 week ago GetEventBookingCommand.php 7 months ago GetEventBookingCommandHandler.php 1 week ago GetEventBookingsCommand.php 1 year ago GetEventBookingsCommandHandler.php 1 week ago GetEventCommand.php 7 years ago GetEventCommandHandler.php 1 week ago GetEventsCommand.php 1 year ago GetEventsCommandHandler.php 1 week ago UpdateEventBookingCommand.php 7 years ago UpdateEventBookingCommandHandler.php 7 months ago UpdateEventCommand.php 1 year ago UpdateEventCommandHandler.php 1 week ago UpdateEventStatusCommand.php 1 year ago UpdateEventStatusCommandHandler.php 7 months ago UpdateEventVisibilityCommand.php 7 months ago UpdateEventVisibilityCommandHandler.php 7 months ago
GetCalendarEventsCommandHandler.php
179 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\User\UserApplicationService;
9 use AmeliaBooking\Domain\Collection\Collection;
10 use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException;
11 use AmeliaBooking\Domain\Entity\Booking\Event\EventPeriod;
12 use AmeliaBooking\Domain\Entity\User\AbstractUser;
13 use AmeliaBooking\Domain\Entity\User\Provider;
14 use AmeliaBooking\Domain\Factory\Booking\Event\EventPeriodFactory;
15 use AmeliaBooking\Domain\Factory\Booking\Event\RecurringFactory;
16 use AmeliaBooking\Domain\Services\Booking\EventDomainService;
17 use AmeliaBooking\Domain\Services\DateTime\DateTimeService;
18 use AmeliaBooking\Domain\Services\Settings\SettingsService;
19 use AmeliaBooking\Domain\ValueObjects\Recurring;
20 use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException;
21 use AmeliaBooking\Infrastructure\Repository\Booking\Event\EventRepository;
22 use AmeliaBooking\Infrastructure\Repository\User\ProviderRepository;
23 use AmeliaBooking\Infrastructure\Services\Google\AbstractGoogleCalendarService;
24 use AmeliaBooking\Infrastructure\Services\Outlook\AbstractOutlookCalendarService;
25 use Exception;
26
27 /**
28 * Class GetCalendarEventsCommandHandler
29 *
30 * @package AmeliaBooking\Application\Commands\Booking\Event
31 */
32 class GetCalendarEventsCommandHandler extends CommandHandler
33 {
34 /**
35 * @var array
36 */
37 public $mandatoryFields = [
38 'periods'
39 ];
40
41 /**
42 * @param GetCalendarEventsCommand $command
43 *
44 * @return CommandResult
45 * @throws QueryExecutionException
46 * @throws InvalidArgumentException
47 * @throws Exception
48 */
49 public function handle(GetCalendarEventsCommand $command)
50 {
51 /** @var AbstractUser $user */
52 $user = $command->authorize();
53
54 $result = new CommandResult();
55
56 $this->checkMandatoryFields($command);
57
58 /** @var UserApplicationService $userAS */
59 $userAS = $this->getContainer()->get('application.user.service');
60 /** @var SettingsService $settingsDS */
61 $settingsDS = $this->container->get('domain.settings.service');
62 /** @var AbstractGoogleCalendarService $googleCalendarService */
63 $googleCalendarService = $this->container->get('infrastructure.google.calendar.service');
64 /** @var AbstractOutlookCalendarService $outlookCalendarService */
65 $outlookCalendarService = $this->container->get('infrastructure.outlook.calendar.service');
66 /** @var EventDomainService $eventDomainService */
67 $eventDomainService = $this->container->get('domain.booking.event.service');
68 /** @var EventRepository $eventRepository */
69 $eventRepository = $this->container->get('domain.booking.event.repository');
70 /** @var ProviderRepository $providerRepository */
71 $providerRepository = $this->container->get('domain.users.providers.repository');
72
73 if (
74 $userAS->isCustomer($user) ||
75 (
76 $userAS->isProvider($user) && !$settingsDS->getSetting('roles', 'allowWriteEvents')
77 )
78 ) {
79 throw new AccessDeniedException('You are not allowed to read an event');
80 }
81
82 $events = [];
83
84 $periodList = $command->getField('periods');
85
86 $eventParams = $command->getField('eventIds');
87
88 /** @var Collection $providers */
89 $providers = $providerRepository->getWithSchedule(
90 [
91 'fetchCalendars' => true,
92 'providers' => array_column($command->getField('providers'), 'id'),
93 ]
94 );
95
96 $eventIds = $eventRepository->getRecurringIds($eventParams[0], $eventParams[1]);
97
98 $eventIds[] = $eventParams[0];
99
100 $recurringData = $command->getField('recurring');
101
102 $periodList = array_map(
103 function ($val) {
104 return EventPeriodFactory::create($val);
105 },
106 $periodList
107 );
108
109 if ($recurringData['until']) {
110 /** @var Recurring $recurring */
111 $recurring = RecurringFactory::create($recurringData);
112
113 $recurringEventsPeriods = $eventDomainService->getRecurringEventsPeriods(
114 $recurring,
115 new Collection($periodList)
116 );
117
118 if (!empty($recurringEventsPeriods)) {
119 /** @var Collection $recurringPeriod */
120 foreach ($recurringEventsPeriods as $recurringPeriod) {
121 if (!empty($recurringPeriod['periods'])) {
122 $periodList = array_merge($periodList, $recurringPeriod['periods']->getItems());
123 }
124 }
125 }
126 }
127
128 /** @var Provider $provider */
129 foreach ($providers->getItems() as $provider) {
130 $providerArray = $provider->toArray();
131
132 /** @var EventPeriod $period */
133 foreach ($periodList as $period) {
134 $periodStart = DateTimeService::getCustomDateTimeRFC3339($period->getPeriodStart()->getValue()->format('Y-m-d H:i:s'));
135 $periodEnd = DateTimeService::getCustomDateTimeRFC3339($period->getPeriodEnd()->getValue()->format('Y-m-d H:i:s'));
136 $periodStartEnd = explode('T', $periodStart)[0] . 'T' . explode('T', $periodEnd)[1];
137
138 try {
139 $events = array_merge($events, $googleCalendarService->getEvents($providerArray, $periodStart, $periodStartEnd, $periodEnd, $eventIds));
140 } catch (Exception $e) {
141 }
142
143 try {
144 $events = array_merge($events, $outlookCalendarService->getEvents($providerArray, $periodStart, $periodStartEnd, $periodEnd, $eventIds));
145 } catch (Exception $e) {
146 }
147
148 $events = apply_filters('amelia_get_calendar_events_filter', $events, $period->toArray(), $providerArray);
149
150 do_action('amelia_get_calendar_events', $events, $period->toArray(), $providerArray);
151
152 if (count($events) > 0) {
153 $result->setResult(CommandResult::RESULT_CONFLICT);
154 $result->setMessage("Conflict with the event in employee's google/outlook calendar");
155 $result->setData(
156 [
157 'calendarConflict' => true,
158 'events' => $events
159 ]
160 );
161
162 return $result;
163 }
164 }
165 }
166
167
168 $result->setResult(CommandResult::RESULT_SUCCESS);
169 $result->setMessage('Successfully retrieved google events.');
170 $result->setData(
171 [
172 'events' => $events,
173 ]
174 );
175
176 return $result;
177 }
178 }
179