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