1
0

NotificationProviderManager.php 2.4 KB

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