From 3cca8498cbbd4f04fea3db06976707fbfa13c7aa Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Thu, 21 Jan 2016 09:49:37 +0100 Subject: Make it possible to get a list of notifiers for a potential settings page --- lib/private/notification/manager.php | 45 ++++++++++++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 5 deletions(-) (limited to 'lib/private/notification/manager.php') diff --git a/lib/private/notification/manager.php b/lib/private/notification/manager.php index a8f1ec349ce..239b5bfe5c7 100644 --- a/lib/private/notification/manager.php +++ b/lib/private/notification/manager.php @@ -29,17 +29,25 @@ class Manager implements IManager { /** @var INotifier */ protected $notifiers; - /** @var \Closure */ + /** @var array[] */ + protected $notifiersInfo; + + /** @var \Closure[] */ protected $appsClosures; - /** @var \Closure */ + /** @var \Closure[] */ protected $notifiersClosures; + /** @var \Closure[] */ + protected $notifiersInfoClosures; + public function __construct() { $this->apps = []; $this->notifiers = []; + $this->notifiersInfo = []; $this->appsClosures = []; $this->notifiersClosures = []; + $this->notifiersInfoClosures = []; } /** @@ -56,12 +64,16 @@ class Manager implements IManager { /** * @param \Closure $service The service must implement INotifier, otherwise a * \InvalidArgumentException is thrown later + * @param \Closure $info An array with the keys 'id' and 'name' containing + * the app id and the app name * @return null - * @since 8.2.0 + * @since 8.2.0 - Parameter $info was added in 9.0.0 */ - public function registerNotifier(\Closure $service) { + public function registerNotifier(\Closure $service, \Closure $info) { $this->notifiersClosures[] = $service; + $this->notifiersInfoClosures[] = $info; $this->notifiers = []; + $this->notifiersInfo = []; } /** @@ -96,7 +108,7 @@ class Manager implements IManager { foreach ($this->notifiersClosures as $closure) { $notifier = $closure(); if (!($notifier instanceof INotifier)) { - throw new \InvalidArgumentException('The given notification app does not implement the INotifier interface'); + throw new \InvalidArgumentException('The given notifier does not implement the INotifier interface'); } $this->notifiers[] = $notifier; } @@ -104,6 +116,29 @@ class Manager implements IManager { return $this->notifiers; } + /** + * @return array[] + */ + public function listNotifiers() { + if (!empty($this->notifiersInfo)) { + return $this->notifiersInfo; + } + + $this->notifiersInfo = []; + foreach ($this->notifiersInfoClosures as $closure) { + $notifier = $closure(); + if (!is_array($notifier) || sizeof($notifier) !== 2 || !isset($notifier['id']) || !isset($notifier['name'])) { + throw new \InvalidArgumentException('The given notifier information is invalid'); + } + if (isset($this->notifiersInfo[$notifier['id']])) { + throw new \InvalidArgumentException('The given notifier ID ' . $notifier['id'] . ' is already in use'); + } + $this->notifiersInfo[$notifier['id']] = $notifier['name']; + } + + return $this->notifiersInfo; + } + /** * @return INotification * @since 8.2.0 -- cgit v1.2.3