Manager.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. *
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OC\Notification;
  25. use OCP\Notification\IApp;
  26. use OCP\Notification\IManager;
  27. use OCP\Notification\INotification;
  28. use OCP\Notification\INotifier;
  29. use OCP\RichObjectStrings\IValidator;
  30. class Manager implements IManager {
  31. /** @var IValidator */
  32. protected $validator;
  33. /** @var IApp[] */
  34. protected $apps;
  35. /** @var INotifier[] */
  36. protected $notifiers;
  37. /** @var array[] */
  38. protected $notifiersInfo;
  39. /** @var \Closure[] */
  40. protected $appsClosures;
  41. /** @var \Closure[] */
  42. protected $notifiersClosures;
  43. /** @var \Closure[] */
  44. protected $notifiersInfoClosures;
  45. /** @var bool */
  46. protected $preparingPushNotification;
  47. /**
  48. * Manager constructor.
  49. *
  50. * @param IValidator $validator
  51. */
  52. public function __construct(IValidator $validator) {
  53. $this->validator = $validator;
  54. $this->apps = [];
  55. $this->notifiers = [];
  56. $this->notifiersInfo = [];
  57. $this->appsClosures = [];
  58. $this->notifiersClosures = [];
  59. $this->notifiersInfoClosures = [];
  60. $this->preparingPushNotification = false;
  61. }
  62. /**
  63. * @param \Closure $service The service must implement IApp, otherwise a
  64. * \InvalidArgumentException is thrown later
  65. * @since 8.2.0
  66. */
  67. public function registerApp(\Closure $service) {
  68. $this->appsClosures[] = $service;
  69. $this->apps = [];
  70. }
  71. /**
  72. * @param \Closure $service The service must implement INotifier, otherwise a
  73. * \InvalidArgumentException is thrown later
  74. * @param \Closure $info An array with the keys 'id' and 'name' containing
  75. * the app id and the app name
  76. * @since 8.2.0 - Parameter $info was added in 9.0.0
  77. */
  78. public function registerNotifier(\Closure $service, \Closure $info) {
  79. $this->notifiersClosures[] = $service;
  80. $this->notifiersInfoClosures[] = $info;
  81. $this->notifiers = [];
  82. $this->notifiersInfo = [];
  83. }
  84. /**
  85. * @return IApp[]
  86. */
  87. protected function getApps(): array {
  88. if (!empty($this->apps)) {
  89. return $this->apps;
  90. }
  91. $this->apps = [];
  92. foreach ($this->appsClosures as $closure) {
  93. $app = $closure();
  94. if (!($app instanceof IApp)) {
  95. throw new \InvalidArgumentException('The given notification app does not implement the IApp interface');
  96. }
  97. $this->apps[] = $app;
  98. }
  99. return $this->apps;
  100. }
  101. /**
  102. * @return INotifier[]
  103. */
  104. protected function getNotifiers(): array {
  105. if (!empty($this->notifiers)) {
  106. return $this->notifiers;
  107. }
  108. $this->notifiers = [];
  109. foreach ($this->notifiersClosures as $closure) {
  110. $notifier = $closure();
  111. if (!($notifier instanceof INotifier)) {
  112. throw new \InvalidArgumentException('The given notifier does not implement the INotifier interface');
  113. }
  114. $this->notifiers[] = $notifier;
  115. }
  116. return $this->notifiers;
  117. }
  118. /**
  119. * @return array[]
  120. */
  121. public function listNotifiers(): array {
  122. if (!empty($this->notifiersInfo)) {
  123. return $this->notifiersInfo;
  124. }
  125. $this->notifiersInfo = [];
  126. foreach ($this->notifiersInfoClosures as $closure) {
  127. $notifier = $closure();
  128. if (!\is_array($notifier) || \count($notifier) !== 2 || !isset($notifier['id'], $notifier['name'])) {
  129. throw new \InvalidArgumentException('The given notifier information is invalid');
  130. }
  131. if (isset($this->notifiersInfo[$notifier['id']])) {
  132. throw new \InvalidArgumentException('The given notifier ID ' . $notifier['id'] . ' is already in use');
  133. }
  134. $this->notifiersInfo[$notifier['id']] = $notifier['name'];
  135. }
  136. return $this->notifiersInfo;
  137. }
  138. /**
  139. * @return INotification
  140. * @since 8.2.0
  141. */
  142. public function createNotification(): INotification {
  143. return new Notification($this->validator);
  144. }
  145. /**
  146. * @return bool
  147. * @since 8.2.0
  148. */
  149. public function hasNotifiers(): bool {
  150. return !empty($this->notifiersClosures);
  151. }
  152. /**
  153. * @param bool $preparingPushNotification
  154. * @since 14.0.0
  155. */
  156. public function setPreparingPushNotification($preparingPushNotification) {
  157. $this->preparingPushNotification = $preparingPushNotification;
  158. }
  159. /**
  160. * @return bool
  161. * @since 14.0.0
  162. */
  163. public function isPreparingPushNotification(): bool {
  164. return $this->preparingPushNotification;
  165. }
  166. /**
  167. * @param INotification $notification
  168. * @throws \InvalidArgumentException When the notification is not valid
  169. * @since 8.2.0
  170. */
  171. public function notify(INotification $notification) {
  172. if (!$notification->isValid()) {
  173. throw new \InvalidArgumentException('The given notification is invalid');
  174. }
  175. $apps = $this->getApps();
  176. foreach ($apps as $app) {
  177. try {
  178. $app->notify($notification);
  179. } catch (\InvalidArgumentException $e) {
  180. }
  181. }
  182. }
  183. /**
  184. * @param INotification $notification
  185. * @param string $languageCode The code of the language that should be used to prepare the notification
  186. * @return INotification
  187. * @throws \InvalidArgumentException When the notification was not prepared by a notifier
  188. * @since 8.2.0
  189. */
  190. public function prepare(INotification $notification, $languageCode): INotification {
  191. $notifiers = $this->getNotifiers();
  192. foreach ($notifiers as $notifier) {
  193. try {
  194. $notification = $notifier->prepare($notification, $languageCode);
  195. } catch (\InvalidArgumentException $e) {
  196. continue;
  197. }
  198. if (!($notification instanceof INotification) || !$notification->isValidParsed()) {
  199. throw new \InvalidArgumentException('The given notification has not been handled');
  200. }
  201. }
  202. if (!($notification instanceof INotification) || !$notification->isValidParsed()) {
  203. throw new \InvalidArgumentException('The given notification has not been handled');
  204. }
  205. return $notification;
  206. }
  207. /**
  208. * @param INotification $notification
  209. */
  210. public function markProcessed(INotification $notification) {
  211. $apps = $this->getApps();
  212. foreach ($apps as $app) {
  213. $app->markProcessed($notification);
  214. }
  215. }
  216. /**
  217. * @param INotification $notification
  218. * @return int
  219. */
  220. public function getCount(INotification $notification): int {
  221. $apps = $this->getApps();
  222. $count = 0;
  223. foreach ($apps as $app) {
  224. $count += $app->getCount($notification);
  225. }
  226. return $count;
  227. }
  228. }