nextcloud/lib/private/notification/manager.php

240 lines
6.0 KiB
PHP
Raw Normal View History

2015-08-31 13:24:37 +03:00
<?php
/**
* @author Joas Schilling <nickvergessen@owncloud.com>
*
2016-01-12 17:02:16 +03:00
* @copyright Copyright (c) 2016, ownCloud, Inc.
2015-08-31 13:24:37 +03:00
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OC\Notification;
use OCP\Notification\IApp;
use OCP\Notification\IManager;
use OCP\Notification\INotification;
use OCP\Notification\INotifier;
2015-08-31 13:24:37 +03:00
class Manager implements IManager {
2016-02-14 23:28:22 +03:00
/** @var IApp[] */
2015-08-31 13:24:37 +03:00
protected $apps;
/** @var INotifier */
protected $notifiers;
/** @var array[] */
protected $notifiersInfo;
/** @var \Closure[] */
2015-08-31 13:24:37 +03:00
protected $appsClosures;
/** @var \Closure[] */
2015-08-31 13:24:37 +03:00
protected $notifiersClosures;
/** @var \Closure[] */
protected $notifiersInfoClosures;
2015-09-03 16:24:33 +03:00
public function __construct() {
$this->apps = [];
$this->notifiers = [];
$this->notifiersInfo = [];
2015-09-03 16:24:33 +03:00
$this->appsClosures = [];
$this->notifiersClosures = [];
$this->notifiersInfoClosures = [];
2015-09-03 16:24:33 +03:00
}
2015-08-31 13:24:37 +03:00
/**
* @param \Closure $service The service must implement IApp, otherwise a
* \InvalidArgumentException is thrown later
* @return null
* @since 8.2.0
*/
public function registerApp(\Closure $service) {
$this->appsClosures[] = $service;
$this->apps = [];
}
/**
* @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
2015-08-31 13:24:37 +03:00
* @return null
* @since 8.2.0 - Parameter $info was added in 9.0.0
2015-08-31 13:24:37 +03:00
*/
public function registerNotifier(\Closure $service, \Closure $info) {
2015-08-31 13:24:37 +03:00
$this->notifiersClosures[] = $service;
$this->notifiersInfoClosures[] = $info;
2015-08-31 13:24:37 +03:00
$this->notifiers = [];
$this->notifiersInfo = [];
2015-08-31 13:24:37 +03:00
}
/**
* @return IApp[]
*/
protected function getApps() {
if (!empty($this->apps)) {
return $this->apps;
}
2015-09-03 16:24:33 +03:00
$this->apps = [];
2015-08-31 13:24:37 +03:00
foreach ($this->appsClosures as $closure) {
2015-09-01 18:58:23 +03:00
$app = $closure();
2015-08-31 13:24:37 +03:00
if (!($app instanceof IApp)) {
throw new \InvalidArgumentException('The given notification app does not implement the IApp interface');
}
$this->apps[] = $app;
}
return $this->apps;
}
/**
* @return INotifier[]
*/
protected function getNotifiers() {
if (!empty($this->notifiers)) {
return $this->notifiers;
}
2015-09-03 16:24:33 +03:00
$this->notifiers = [];
2015-08-31 13:24:37 +03:00
foreach ($this->notifiersClosures as $closure) {
2015-09-01 18:58:23 +03:00
$notifier = $closure();
2015-08-31 13:24:37 +03:00
if (!($notifier instanceof INotifier)) {
throw new \InvalidArgumentException('The given notifier does not implement the INotifier interface');
2015-08-31 13:24:37 +03:00
}
$this->notifiers[] = $notifier;
}
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;
}
2015-08-31 13:24:37 +03:00
/**
* @return INotification
* @since 8.2.0
*/
public function createNotification() {
return new Notification();
}
/**
* @return bool
* @since 8.2.0
*/
public function hasNotifiers() {
return !empty($this->notifiersClosures);
}
2015-08-31 13:24:37 +03:00
/**
* @param INotification $notification
* @return null
* @throws \InvalidArgumentException When the notification is not valid
* @since 8.2.0
*/
public function notify(INotification $notification) {
if (!$notification->isValid()) {
throw new \InvalidArgumentException('The given notification is invalid');
}
$apps = $this->getApps();
foreach ($apps as $app) {
try {
$app->notify($notification);
} catch (\InvalidArgumentException $e) {
}
2015-08-31 13:24:37 +03:00
}
}
/**
* @param INotification $notification
2015-09-01 11:20:54 +03:00
* @param string $languageCode The code of the language that should be used to prepare the notification
2015-08-31 13:24:37 +03:00
* @return INotification
* @throws \InvalidArgumentException When the notification was not prepared by a notifier
* @since 8.2.0
*/
2015-09-01 11:20:54 +03:00
public function prepare(INotification $notification, $languageCode) {
2015-08-31 13:24:37 +03:00
$notifiers = $this->getNotifiers();
foreach ($notifiers as $notifier) {
try {
2015-09-02 14:09:46 +03:00
$notification = $notifier->prepare($notification, $languageCode);
} catch (\InvalidArgumentException $e) {
continue;
}
2015-08-31 13:24:37 +03:00
2015-09-03 16:24:33 +03:00
if (!($notification instanceof INotification) || !$notification->isValidParsed()) {
throw new \InvalidArgumentException('The given notification has not been handled');
}
2015-08-31 13:24:37 +03:00
}
2015-09-03 16:24:33 +03:00
if (!($notification instanceof INotification) || !$notification->isValidParsed()) {
throw new \InvalidArgumentException('The given notification has not been handled');
}
2015-08-31 13:24:37 +03:00
return $notification;
}
/**
* @param INotification $notification
2015-08-31 13:24:37 +03:00
* @return null
*/
public function markProcessed(INotification $notification) {
2015-08-31 13:24:37 +03:00
$apps = $this->getApps();
foreach ($apps as $app) {
$app->markProcessed($notification);
2015-08-31 13:24:37 +03:00
}
}
/**
* @param INotification $notification
2015-08-31 13:24:37 +03:00
* @return int
*/
public function getCount(INotification $notification) {
2015-08-31 13:24:37 +03:00
$apps = $this->getApps();
$count = 0;
foreach ($apps as $app) {
2015-09-03 16:24:33 +03:00
$count += $app->getCount($notification);
2015-08-31 13:24:37 +03:00
}
return $count;
}
}