PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 1.2.11
Booking for Appointments and Events Calendar – Amelia v1.2.11
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
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 2 years 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 2 years ago UpdateEventCommand.php 7 years ago UpdateEventCommandHandler.php 1 year ago UpdateEventStatusCommand.php 7 years ago UpdateEventStatusCommandHandler.php 2 years ago
GetCalendarEventsCommandHandler.php
172 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\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\Services\Google\AbstractGoogleCalendarService;
23 use AmeliaBooking\Infrastructure\Services\Outlook\AbstractOutlookCalendarService;
24 use Exception;
25 use Interop\Container\Exception\ContainerException;
26 use Slim\Exception\ContainerValueNotFoundException;
27
28 /**
29 * Class GetCalendarEventsCommandHandler
30 *
31 * @package AmeliaBooking\Application\Commands\Booking\Event
32 */
33 class GetCalendarEventsCommandHandler extends CommandHandler
34 {
35 /**
36 * @var array
37 */
38 public $mandatoryFields = [
39 'periods'
40 ];
41
42 /**
43 * @param GetCalendarEventsCommand $command
44 *
45 * @return CommandResult
46 * @throws QueryExecutionException
47 * @throws ContainerValueNotFoundException
48 * @throws InvalidArgumentException
49 * @throws ContainerException
50 * @throws Exception
51 */
52 public function handle(GetCalendarEventsCommand $command)
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
71 try {
72 /** @var AbstractUser $user */
73 $user = $command->getUserApplicationService()->authorization(
74 $command->getPage() === 'cabinet' ? $command->getToken() : null,
75 $command->getCabinetType()
76 );
77 } catch (AuthorizationException $e) {
78 $result->setResult(CommandResult::RESULT_ERROR);
79 $result->setData(
80 ['reauthorize' => true]
81 );
82
83 return $result;
84 }
85
86 if ($userAS->isCustomer($user) ||
87 (
88 $userAS->isProvider($user) && !$settingsDS->getSetting('roles', 'allowWriteEvents')
89 )
90 ) {
91 throw new AccessDeniedException('You are not allowed to read an event');
92 }
93
94 $events = [];
95 $providerList = $command->getField('providers');
96 $periodList = $command->getField('periods');
97 $eventParams = $command->getField('eventIds');
98
99 $eventIds = $eventRepository->getRecurringIds($eventParams[0], $eventParams[1]);
100
101 $eventIds[] = $eventParams[0];
102
103 $recurringData = $command->getField('recurring');
104
105 $periodList = array_map(
106 function ($val) {
107 return EventPeriodFactory::create($val);
108 },
109 $periodList
110 );
111
112 if ($recurringData['until']) {
113 /** @var Recurring $recurring */
114 $recurring = RecurringFactory::create($recurringData);
115
116 $recurringEventsPeriods = $eventDomainService->getRecurringEventsPeriods(
117 $recurring,
118 new Collection($periodList)
119 );
120
121 if (!empty($recurringEventsPeriods)) {
122 /** @var Collection $recurringPeriod */
123 foreach ($recurringEventsPeriods as $recurringPeriod) {
124 if (!empty($recurringPeriod['periods'])) {
125 $periodList = array_merge($periodList, $recurringPeriod['periods']->getItems());
126 }
127 }
128 }
129 }
130
131 foreach ($providerList as $provider) {
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 $events = array_merge($events, $googleCalendarService->getEvents($provider, $periodStart, $periodStartEnd, $periodEnd, $eventIds));
139 $events = array_merge($events, $outlookCalendarService->getEvents($provider, $periodStart, $periodStartEnd, $periodEnd, $eventIds));
140
141 $events = apply_filters('amelia_get_calendar_events_filter', $events, $period->toArray(), $provider);
142
143 do_action('amelia_get_calendar_events', $events, $period->toArray(), $provider);
144
145 if (count($events) > 0) {
146 $result->setResult(CommandResult::RESULT_CONFLICT);
147 $result->setMessage("Conflict with the event in employee's google/outlook calendar");
148 $result->setData(
149 [
150 'calendarConflict' => true,
151 'events' => $events
152 ]
153 );
154
155 return $result;
156 }
157 }
158 }
159
160
161 $result->setResult(CommandResult::RESULT_SUCCESS);
162 $result->setMessage('Successfully retrieved google events.');
163 $result->setData(
164 [
165 'events' => $events,
166 ]
167 );
168
169 return $result;
170 }
171 }
172