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 / GetCalendarEventsCommandHandler.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
GetCalendarEventsCommandHandler.php
196 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 (
91 $userAS->isCustomer($user) ||
92 (
93 $userAS->isProvider($user) && !$settingsDS->getSetting('roles', 'allowWriteEvents')
94 )
95 ) {
96 throw new AccessDeniedException('You are not allowed to read an event');
97 }
98
99 $events = [];
100
101 $periodList = $command->getField('periods');
102
103 $eventParams = $command->getField('eventIds');
104
105 /** @var Collection $providers */
106 $providers = $providerRepository->getWithSchedule(
107 [
108 'fetchCalendars' => true,
109 'providers' => array_column($command->getField('providers'), 'id'),
110 ]
111 );
112
113 $eventIds = $eventRepository->getRecurringIds($eventParams[0], $eventParams[1]);
114
115 $eventIds[] = $eventParams[0];
116
117 $recurringData = $command->getField('recurring');
118
119 $periodList = array_map(
120 function ($val) {
121 return EventPeriodFactory::create($val);
122 },
123 $periodList
124 );
125
126 if ($recurringData['until']) {
127 /** @var Recurring $recurring */
128 $recurring = RecurringFactory::create($recurringData);
129
130 $recurringEventsPeriods = $eventDomainService->getRecurringEventsPeriods(
131 $recurring,
132 new Collection($periodList)
133 );
134
135 if (!empty($recurringEventsPeriods)) {
136 /** @var Collection $recurringPeriod */
137 foreach ($recurringEventsPeriods as $recurringPeriod) {
138 if (!empty($recurringPeriod['periods'])) {
139 $periodList = array_merge($periodList, $recurringPeriod['periods']->getItems());
140 }
141 }
142 }
143 }
144
145 /** @var Provider $provider */
146 foreach ($providers->getItems() as $provider) {
147 $providerArray = $provider->toArray();
148
149 /** @var EventPeriod $period */
150 foreach ($periodList as $period) {
151 $periodStart = DateTimeService::getCustomDateTimeRFC3339($period->getPeriodStart()->getValue()->format('Y-m-d H:i:s'));
152 $periodEnd = DateTimeService::getCustomDateTimeRFC3339($period->getPeriodEnd()->getValue()->format('Y-m-d H:i:s'));
153 $periodStartEnd = explode('T', $periodStart)[0] . 'T' . explode('T', $periodEnd)[1];
154
155 try {
156 $events = array_merge($events, $googleCalendarService->getEvents($providerArray, $periodStart, $periodStartEnd, $periodEnd, $eventIds));
157 } catch (Exception $e) {
158 }
159
160 try {
161 $events = array_merge($events, $outlookCalendarService->getEvents($providerArray, $periodStart, $periodStartEnd, $periodEnd, $eventIds));
162 } catch (Exception $e) {
163 }
164
165 $events = apply_filters('amelia_get_calendar_events_filter', $events, $period->toArray(), $providerArray);
166
167 do_action('amelia_get_calendar_events', $events, $period->toArray(), $providerArray);
168
169 if (count($events) > 0) {
170 $result->setResult(CommandResult::RESULT_CONFLICT);
171 $result->setMessage("Conflict with the event in employee's google/outlook calendar");
172 $result->setData(
173 [
174 'calendarConflict' => true,
175 'events' => $events
176 ]
177 );
178
179 return $result;
180 }
181 }
182 }
183
184
185 $result->setResult(CommandResult::RESULT_SUCCESS);
186 $result->setMessage('Successfully retrieved google events.');
187 $result->setData(
188 [
189 'events' => $events,
190 ]
191 );
192
193 return $result;
194 }
195 }
196