ApplicationIntegrationService.php
300 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @copyright © Melograno Ventures. All rights reserved. |
| 5 | * @licence See LICENCE.md for license details. |
| 6 | */ |
| 7 | |
| 8 | namespace AmeliaBooking\Application\Services\Integration; |
| 9 | |
| 10 | use AmeliaBooking\Application\Services\Zoom\AbstractZoomApplicationService; |
| 11 | use AmeliaBooking\Domain\Collection\Collection; |
| 12 | use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException; |
| 13 | use AmeliaBooking\Domain\Entity\Booking\Appointment\Appointment; |
| 14 | use AmeliaBooking\Domain\Entity\Booking\Event\Event; |
| 15 | use AmeliaBooking\Domain\Entity\Entities; |
| 16 | use AmeliaBooking\Domain\ValueObjects\Number\Integer\Id; |
| 17 | use AmeliaBooking\Infrastructure\Common\Container; |
| 18 | use AmeliaBooking\Infrastructure\Common\Exceptions\NotFoundException; |
| 19 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 20 | use AmeliaBooking\Infrastructure\Services\Apple\AbstractAppleCalendarService; |
| 21 | use AmeliaBooking\Infrastructure\Services\Google\AbstractGoogleCalendarService; |
| 22 | use AmeliaBooking\Infrastructure\Services\LessonSpace\AbstractLessonSpaceService; |
| 23 | use AmeliaBooking\Infrastructure\Services\Outlook\AbstractOutlookCalendarService; |
| 24 | use Interop\Container\Exception\ContainerException; |
| 25 | use Microsoft\Graph\Exception\GraphException; |
| 26 | |
| 27 | /** |
| 28 | * Class ApplicationIntegrationService |
| 29 | * |
| 30 | * @package AmeliaBooking\Application\Services\Integration |
| 31 | */ |
| 32 | class ApplicationIntegrationService |
| 33 | { |
| 34 | public const SKIP_GOOGLE_CALENDAR = 'skipGoogleCalendar'; |
| 35 | public const SKIP_OUTLOOK_CALENDAR = 'skipOutlookCalendar'; |
| 36 | public const SKIP_APPLE_CALENDAR = 'skipAppleCalendar'; |
| 37 | public const SKIP_ZOOM_MEETING = 'skipZoomMeeting'; |
| 38 | public const SKIP_LESSON_SPACE = 'skipLessonSpace'; |
| 39 | public const APPOINTMENT_ADDED = 'appointmentAdded'; |
| 40 | public const APPOINTMENT_EDITED = 'appointmentEdited'; |
| 41 | public const APPOINTMENT_DELETED = 'appointmentDeleted'; |
| 42 | public const APPOINTMENT_STATUS_UPDATED = 'appointmentStatusUpdated'; |
| 43 | public const BOOKING_ADDED = 'bookingAdded'; |
| 44 | public const BOOKING_APPROVED = 'bookingApproved'; |
| 45 | public const BOOKING_CANCELED = 'bookingCanceled'; |
| 46 | public const BOOKING_REJECTED = 'bookingRejected'; |
| 47 | public const BOOKING_STATUS_UPDATED = 'bookingStatusUpdated'; |
| 48 | public const TIME_UPDATED = 'bookingTimeUpdated'; |
| 49 | public const EVENT_ADDED = 'eventAdded'; |
| 50 | public const EVENT_DELETED = 'eventDeleted'; |
| 51 | public const EVENT_PERIOD_ADDED = 'eventPeriodAdded'; |
| 52 | public const EVENT_PERIOD_DELETED = 'eventPeriodDeleted'; |
| 53 | public const EVENT_STATUS_UPDATED = 'eventStatusUpdated'; |
| 54 | public const PROVIDER_CHANGED = 'providerChanged'; |
| 55 | |
| 56 | /** @var Container $container */ |
| 57 | protected $container; |
| 58 | |
| 59 | /** |
| 60 | * ApplicationIntegrationService constructor. |
| 61 | * |
| 62 | * @param Container $container |
| 63 | */ |
| 64 | public function __construct(Container $container) |
| 65 | { |
| 66 | $this->container = $container; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * @param Appointment $appointment |
| 71 | * @param array $appointmentArray |
| 72 | * @param string $command |
| 73 | * @param array $skippedIntegrations |
| 74 | * |
| 75 | * @throws ContainerException |
| 76 | * @throws InvalidArgumentException |
| 77 | * @throws NotFoundException |
| 78 | * @throws QueryExecutionException |
| 79 | * @throws GraphException |
| 80 | */ |
| 81 | public function handleAppointment($appointment, &$appointmentArray, $command, $skippedIntegrations = []) |
| 82 | { |
| 83 | /** @var AbstractGoogleCalendarService $googleCalendarService */ |
| 84 | $googleCalendarService = $this->container->get('infrastructure.google.calendar.service'); |
| 85 | |
| 86 | /** @var AbstractOutlookCalendarService $outlookCalendarService */ |
| 87 | $outlookCalendarService = $this->container->get('infrastructure.outlook.calendar.service'); |
| 88 | |
| 89 | /** @var AbstractAppleCalendarService $appleCalendarService */ |
| 90 | $appleCalendarService = $this->container->get('infrastructure.apple.calendar.service'); |
| 91 | |
| 92 | /** @var AbstractZoomApplicationService $zoomService */ |
| 93 | $zoomService = $this->container->get('application.zoom.service'); |
| 94 | |
| 95 | /** @var AbstractLessonSpaceService $lessonSpaceService */ |
| 96 | $lessonSpaceService = $this->container->get('infrastructure.lesson.space.service'); |
| 97 | |
| 98 | if (empty($skippedIntegrations[self::SKIP_ZOOM_MEETING])) { |
| 99 | $zoomService->handleAppointmentMeeting($appointment, $command); |
| 100 | |
| 101 | if ($appointment->getZoomMeeting()) { |
| 102 | $appointmentArray['zoomMeeting'] = $appointment->getZoomMeeting()->toArray(); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | |
| 107 | if (empty($skippedIntegrations[self::SKIP_LESSON_SPACE])) { |
| 108 | $lessonSpaceService->handle($appointment, Entities::APPOINTMENT); |
| 109 | |
| 110 | if ($appointment->getLessonSpace()) { |
| 111 | $appointmentArray['lessonSpace'] = $appointment->getLessonSpace(); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | if (empty($skippedIntegrations[self::SKIP_GOOGLE_CALENDAR])) { |
| 116 | $googleCalendarService->handleEvent($appointment, $command); |
| 117 | |
| 118 | if ($appointment->getGoogleCalendarEventId() !== null) { |
| 119 | $appointmentArray['googleCalendarEventId'] = $appointment->getGoogleCalendarEventId()->getValue(); |
| 120 | } |
| 121 | |
| 122 | if ($appointment->getGoogleMeetUrl() !== null) { |
| 123 | $appointmentArray['googleMeetUrl'] = $appointment->getGoogleMeetUrl(); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | if (empty($skippedIntegrations[self::SKIP_OUTLOOK_CALENDAR])) { |
| 128 | $outlookCalendarService->handleEvent($appointment, $command); |
| 129 | |
| 130 | if ($appointment->getOutlookCalendarEventId() !== null) { |
| 131 | $appointmentArray['outlookCalendarEventId'] = $appointment->getOutlookCalendarEventId()->getValue(); |
| 132 | } |
| 133 | |
| 134 | if ($appointment->getMicrosoftTeamsUrl() !== null) { |
| 135 | $appointmentArray['microsoftTeamsUrl'] = $appointment->getMicrosoftTeamsUrl(); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | if (empty($skippedIntegrations[self::SKIP_APPLE_CALENDAR])) { |
| 140 | $appleCalendarService->handleEvent($appointment, $command); |
| 141 | |
| 142 | if ($appointment->getAppleCalendarEventId() !== null) { |
| 143 | $appointmentArray['appleCalendarEventId'] = $appointment->getAppleCalendarEventId()->getValue(); |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * @param Appointment $appointment |
| 150 | * @param array $appointmentArray |
| 151 | * @param int|null $oldProviderId |
| 152 | * |
| 153 | * @throws ContainerException |
| 154 | * @throws GraphException |
| 155 | * @throws InvalidArgumentException |
| 156 | * @throws NotFoundException |
| 157 | * @throws QueryExecutionException |
| 158 | */ |
| 159 | public function handleAppointmentEmployeeChange($appointment, &$appointmentArray, $oldProviderId) |
| 160 | { |
| 161 | /** @var AbstractGoogleCalendarService $googleCalendarService */ |
| 162 | $googleCalendarService = $this->container->get('infrastructure.google.calendar.service'); |
| 163 | |
| 164 | /** @var AbstractOutlookCalendarService $outlookCalendarService */ |
| 165 | $outlookCalendarService = $this->container->get('infrastructure.outlook.calendar.service'); |
| 166 | |
| 167 | /** @var AbstractAppleCalendarService $appleCalendarService */ |
| 168 | $appleCalendarService = $this->container->get('infrastructure.apple.calendar.service'); |
| 169 | |
| 170 | if ($oldProviderId) { |
| 171 | $newProviderId = $appointment->getProviderId()->getValue(); |
| 172 | |
| 173 | $appointment->setProviderId(new Id($oldProviderId)); |
| 174 | |
| 175 | $googleCalendarService->handleEvent($appointment, self::APPOINTMENT_DELETED); |
| 176 | |
| 177 | $appointment->setGoogleCalendarEventId(null); |
| 178 | |
| 179 | $outlookCalendarService->handleEvent($appointment, self::APPOINTMENT_DELETED); |
| 180 | |
| 181 | $appointment->setOutlookCalendarEventId(null); |
| 182 | |
| 183 | $appleCalendarService->handleEvent($appointment, self::APPOINTMENT_DELETED); |
| 184 | |
| 185 | $appointment->setAppleCalendarEventId(null); |
| 186 | |
| 187 | $appointment->setProviderId(new Id($newProviderId)); |
| 188 | |
| 189 | $googleCalendarService->handleEvent($appointment, self::APPOINTMENT_ADDED); |
| 190 | |
| 191 | $outlookCalendarService->handleEvent($appointment, self::APPOINTMENT_ADDED); |
| 192 | |
| 193 | $appleCalendarService->handleEvent($appointment, self::APPOINTMENT_ADDED); |
| 194 | } else { |
| 195 | $googleCalendarService->handleEvent($appointment, self::APPOINTMENT_EDITED); |
| 196 | |
| 197 | $outlookCalendarService->handleEvent($appointment, self::APPOINTMENT_EDITED); |
| 198 | |
| 199 | $appleCalendarService->handleEvent($appointment, self::APPOINTMENT_EDITED); |
| 200 | } |
| 201 | |
| 202 | if ($appointment->getGoogleCalendarEventId() !== null) { |
| 203 | $appointmentArray['googleCalendarEventId'] = $appointment->getGoogleCalendarEventId()->getValue(); |
| 204 | } |
| 205 | |
| 206 | if ($appointment->getGoogleMeetUrl() !== null) { |
| 207 | $appointmentArray['googleMeetUrl'] = $appointment->getGoogleMeetUrl(); |
| 208 | } |
| 209 | |
| 210 | if ($appointment->getOutlookCalendarEventId() !== null) { |
| 211 | $appointmentArray['outlookCalendarEventId'] = $appointment->getOutlookCalendarEventId()->getValue(); |
| 212 | } |
| 213 | |
| 214 | if ($appointment->getMicrosoftTeamsUrl() !== null) { |
| 215 | $appointmentArray['microsoftTeamsUrl'] = $appointment->getMicrosoftTeamsUrl(); |
| 216 | } |
| 217 | |
| 218 | if ($appointment->getAppleCalendarEventId() !== null) { |
| 219 | $appointmentArray['appleCalendarEventId'] = $appointment->getAppleCalendarEventId()->getValue(); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * @param Event $event |
| 225 | * @param Collection $eventPeriods |
| 226 | * @param array $eventArray |
| 227 | * @param string $command |
| 228 | * @param array $skippedIntegrations |
| 229 | * @param array $users |
| 230 | * |
| 231 | * @throws ContainerException |
| 232 | * @throws InvalidArgumentException |
| 233 | * @throws NotFoundException |
| 234 | * @throws QueryExecutionException |
| 235 | * @throws GraphException |
| 236 | */ |
| 237 | public function handleEvent($event, $eventPeriods, &$eventArray, $command, $skippedIntegrations = [], $users = []) |
| 238 | { |
| 239 | /** @var AbstractGoogleCalendarService $googleCalendarService */ |
| 240 | $googleCalendarService = $this->container->get('infrastructure.google.calendar.service'); |
| 241 | |
| 242 | /** @var AbstractOutlookCalendarService $outlookCalendarService */ |
| 243 | $outlookCalendarService = $this->container->get('infrastructure.outlook.calendar.service'); |
| 244 | |
| 245 | /** @var AbstractAppleCalendarService $appleCalendarService */ |
| 246 | $appleCalendarService = $this->container->get('infrastructure.apple.calendar.service'); |
| 247 | |
| 248 | /** @var AbstractZoomApplicationService $zoomService */ |
| 249 | $zoomService = $this->container->get('application.zoom.service'); |
| 250 | |
| 251 | /** @var AbstractLessonSpaceService $lessonSpaceService */ |
| 252 | $lessonSpaceService = $this->container->get('infrastructure.lesson.space.service'); |
| 253 | |
| 254 | if (empty($skippedIntegrations[self::SKIP_LESSON_SPACE])) { |
| 255 | $lessonSpaceService->handle($event, Entities::EVENT, $eventPeriods); |
| 256 | } |
| 257 | |
| 258 | if (empty($skippedIntegrations[self::SKIP_ZOOM_MEETING])) { |
| 259 | $zoomService->handleEventMeeting( |
| 260 | $event, |
| 261 | $eventPeriods, |
| 262 | $command, |
| 263 | !empty($users['zoomUserId']) ? $users['zoomUserId'] : null |
| 264 | ); |
| 265 | } |
| 266 | |
| 267 | if (empty($skippedIntegrations[self::SKIP_GOOGLE_CALENDAR])) { |
| 268 | $googleCalendarService->handleEventPeriodsChange( |
| 269 | $event, |
| 270 | $command, |
| 271 | $eventPeriods, |
| 272 | !empty($users['providersNew']) ? $users['providersNew'] : null, |
| 273 | !empty($users['providersRemove']) ? $users['providersRemove'] : null |
| 274 | ); |
| 275 | } |
| 276 | |
| 277 | if (empty($skippedIntegrations[self::SKIP_OUTLOOK_CALENDAR])) { |
| 278 | $outlookCalendarService->handleEventPeriod( |
| 279 | $event, |
| 280 | $command, |
| 281 | $eventPeriods, |
| 282 | !empty($users['providersNew']) ? $users['providersNew'] : null, |
| 283 | !empty($users['providersRemove']) ? $users['providersRemove'] : null |
| 284 | ); |
| 285 | } |
| 286 | |
| 287 | if (empty($skippedIntegrations[self::SKIP_APPLE_CALENDAR])) { |
| 288 | $appleCalendarService->handleEventPeriod( |
| 289 | $event, |
| 290 | $command, |
| 291 | $eventPeriods, |
| 292 | !empty($users['providersNew']) ? $users['providersNew'] : null, |
| 293 | !empty($users['providersRemove']) ? $users['providersRemove'] : null |
| 294 | ); |
| 295 | } |
| 296 | |
| 297 | $eventArray['periods'] = $event->getPeriods()->toArray(); |
| 298 | } |
| 299 | } |
| 300 |