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