1
0

Manager.php 6.2 KB

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