Notifier::prepare()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 23
rs 9.0856
cc 3
eloc 13
nc 3
nop 2
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, Joas Schilling <[email protected]>
4
 *
5
 * @author Joas Schilling <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
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
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OCA\NextcloudAnnouncements\Notification;
25
26
27
use OCP\IURLGenerator;
28
use OCP\L10N\IFactory;
29
use OCP\Notification\INotification;
30
use OCP\Notification\INotifier;
31
32
class Notifier implements INotifier {
33
34
	const SUBJECT = 'announced';
35
36
	/** @var string */
37
	protected $appName;
38
39
	/** @var IFactory */
40
	protected $l10nFactory;
41
42
	/** @var IURLGenerator */
43
	protected $url;
44
45
	/**
46
	 * @param string $appName
47
	 * @param IFactory $l10nFactory
48
	 * @param IURLGenerator $url
49
	 */
50
	public function __construct($appName, IFactory $l10nFactory, IURLGenerator $url) {
51
		$this->appName = $appName;
52
		$this->l10nFactory = $l10nFactory;
53
		$this->url = $url;
54
	}
55
56
	/**
57
	 * @param INotification $notification
58
	 * @param string $languageCode The code of the language that should be used to prepare the notification
59
	 * @return INotification
60
	 * @throws \InvalidArgumentException When the notification was not prepared by a notifier
61
	 */
62
	public function prepare(INotification $notification, $languageCode) {
63
		if ($notification->getApp() !== $this->appName) {
64
			// Not my app => throw
65
			throw new \InvalidArgumentException();
66
		}
67
68
		// Read the language from the notification
69
		$l = $this->l10nFactory->get($this->appName, $languageCode);
70
71
		switch ($notification->getSubject()) {
72
			case self::SUBJECT:
73
				$parameters = $notification->getSubjectParameters();
74
				$notification->setParsedSubject($l->t('Nextcloud announcement'))
75
					->setParsedMessage($parameters[0])
76
					->setIcon($this->url->getAbsoluteURL($this->url->imagePath($this->appName, 'app-dark.svg')));
77
78
				return $notification;
79
80
			default:
81
				// Unknown subject => Unknown notification => throw
82
				throw new \InvalidArgumentException();
83
		}
84
	}
85
}
86