NotificationProviderManager.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\DAV\CalDAV\Reminder;
  8. /**
  9. * Class NotificationProviderManager
  10. *
  11. * @package OCA\DAV\CalDAV\Reminder
  12. */
  13. class NotificationProviderManager {
  14. /** @var INotificationProvider[] */
  15. private $providers = [];
  16. /**
  17. * Checks whether a provider for a given ACTION exists
  18. *
  19. * @param string $type
  20. * @return bool
  21. */
  22. public function hasProvider(string $type):bool {
  23. return (\in_array($type, ReminderService::REMINDER_TYPES, true)
  24. && isset($this->providers[$type]));
  25. }
  26. /**
  27. * Get provider for a given ACTION
  28. *
  29. * @param string $type
  30. * @return INotificationProvider
  31. * @throws NotificationProvider\ProviderNotAvailableException
  32. * @throws NotificationTypeDoesNotExistException
  33. */
  34. public function getProvider(string $type):INotificationProvider {
  35. if (in_array($type, ReminderService::REMINDER_TYPES, true)) {
  36. if (isset($this->providers[$type])) {
  37. return $this->providers[$type];
  38. }
  39. throw new NotificationProvider\ProviderNotAvailableException($type);
  40. }
  41. throw new NotificationTypeDoesNotExistException($type);
  42. }
  43. /**
  44. * Registers a new provider
  45. *
  46. * @param string $providerClassName
  47. * @throws \OCP\AppFramework\QueryException
  48. */
  49. public function registerProvider(string $providerClassName):void {
  50. $provider = \OC::$server->query($providerClassName);
  51. if (!$provider instanceof INotificationProvider) {
  52. throw new \InvalidArgumentException('Invalid notification provider registered');
  53. }
  54. $this->providers[$provider::NOTIFICATION_TYPE] = $provider;
  55. }
  56. }