NotificationProviderManager.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. use OCA\DAV\CalDAV\Reminder\NotificationProvider\ProviderNotAvailableException;
  9. use OCP\AppFramework\QueryException;
  10. /**
  11. * Class NotificationProviderManager
  12. *
  13. * @package OCA\DAV\CalDAV\Reminder
  14. */
  15. class NotificationProviderManager {
  16. /** @var INotificationProvider[] */
  17. private $providers = [];
  18. /**
  19. * Checks whether a provider for a given ACTION exists
  20. *
  21. * @param string $type
  22. * @return bool
  23. */
  24. public function hasProvider(string $type):bool {
  25. return (\in_array($type, ReminderService::REMINDER_TYPES, true)
  26. && isset($this->providers[$type]));
  27. }
  28. /**
  29. * Get provider for a given ACTION
  30. *
  31. * @param string $type
  32. * @return INotificationProvider
  33. * @throws NotificationProvider\ProviderNotAvailableException
  34. * @throws NotificationTypeDoesNotExistException
  35. */
  36. public function getProvider(string $type):INotificationProvider {
  37. if (in_array($type, ReminderService::REMINDER_TYPES, true)) {
  38. if (isset($this->providers[$type])) {
  39. return $this->providers[$type];
  40. }
  41. throw new ProviderNotAvailableException($type);
  42. }
  43. throw new NotificationTypeDoesNotExistException($type);
  44. }
  45. /**
  46. * Registers a new provider
  47. *
  48. * @param string $providerClassName
  49. * @throws QueryException
  50. */
  51. public function registerProvider(string $providerClassName):void {
  52. $provider = \OC::$server->query($providerClassName);
  53. if (!$provider instanceof INotificationProvider) {
  54. throw new \InvalidArgumentException('Invalid notification provider registered');
  55. }
  56. $this->providers[$provider::NOTIFICATION_TYPE] = $provider;
  57. }
  58. }