NotificationProviderManager.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2019, Thomas Citharel
  5. * @copyright Copyright (c) 2019, Georg Ehrke
  6. *
  7. * @author Thomas Citharel <tcit@tcit.fr>
  8. * @author Georg Ehrke <oc.list@georgehrke.com>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OCA\DAV\CalDAV\Reminder;
  26. /**
  27. * Class NotificationProviderManager
  28. *
  29. * @package OCA\DAV\CalDAV\Reminder
  30. */
  31. class NotificationProviderManager {
  32. /** @var INotificationProvider[] */
  33. private $providers = [];
  34. /**
  35. * Checks whether a provider for a given ACTION exists
  36. *
  37. * @param string $type
  38. * @return bool
  39. */
  40. public function hasProvider(string $type):bool {
  41. return (\in_array($type, ReminderService::REMINDER_TYPES, true)
  42. && isset($this->providers[$type]));
  43. }
  44. /**
  45. * Get provider for a given ACTION
  46. *
  47. * @param string $type
  48. * @return INotificationProvider
  49. * @throws NotificationProvider\ProviderNotAvailableException
  50. * @throws NotificationTypeDoesNotExistException
  51. */
  52. public function getProvider(string $type):INotificationProvider {
  53. if (in_array($type, ReminderService::REMINDER_TYPES, true)) {
  54. if (isset($this->providers[$type])) {
  55. return $this->providers[$type];
  56. }
  57. throw new NotificationProvider\ProviderNotAvailableException($type);
  58. }
  59. throw new NotificationTypeDoesNotExistException($type);
  60. }
  61. /**
  62. * Registers a new provider
  63. *
  64. * @param string $providerClassName
  65. * @throws \OCP\AppFramework\QueryException
  66. */
  67. public function registerProvider(string $providerClassName):void {
  68. $provider = \OC::$server->query($providerClassName);
  69. if (!$provider instanceof INotificationProvider) {
  70. throw new \InvalidArgumentException('Invalid notification provider registered');
  71. }
  72. $this->providers[$provider::NOTIFICATION_TYPE] = $provider;
  73. }
  74. }