Notifier   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 29.79%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 2
dl 0
loc 121
ccs 14
cts 47
cp 0.2979
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getID() 0 3 1
A getName() 0 3 1
A __construct() 0 11 1
B prepare() 0 65 7
1
<?php
2
declare(strict_types=1);
3
/**
4
 * @copyright Copyright (c) 2016, Joas Schilling <[email protected]>
5
 *
6
 * @author Joas Schilling <[email protected]>
7
 *
8
 * @license GNU AGPL version 3 or any later version
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License as
12
 * published by the Free Software Foundation, either version 3 of the
13
 * License, or (at your option) any later version.
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
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 *
23
 */
24
25
namespace OCA\AnnouncementCenter\Notification;
26
27
28
use OCA\AnnouncementCenter\Manager;
29
use OCA\AnnouncementCenter\Model\AnnouncementDoesNotExistException;
30
use OCP\IURLGenerator;
31
use OCP\IUser;
32
use OCP\IUserManager;
33
use OCP\L10N\IFactory;
34
use OCP\Notification\AlreadyProcessedException;
35
use OCP\Notification\IManager as INotificationManager;
36
use OCP\Notification\INotification;
37
use OCP\Notification\INotifier;
38
39
class Notifier implements INotifier {
40
41
	/** @var Manager */
42
	protected $manager;
43
44
	/** @var IFactory */
45
	protected $l10nFactory;
46
47
	/** @var INotificationManager */
48
	protected $notificationManager;
49
50
	/** @var IUserManager */
51
	protected $userManager;
52
53
	/** @var IURLGenerator */
54
	protected $urlGenerator;
55
56 3
	public function __construct(Manager $manager,
57
								IFactory $l10nFactory,
58
								INotificationManager $notificationManager,
59
								IUserManager $userManager,
60
								IURLGenerator $urlGenerator) {
61 3
		$this->manager = $manager;
62 3
		$this->l10nFactory = $l10nFactory;
63 3
		$this->notificationManager = $notificationManager;
64 3
		$this->userManager = $userManager;
65 3
		$this->urlGenerator = $urlGenerator;
66 3
	}
67
68
	/**
69
	 * Identifier of the notifier, only use [a-z0-9_]
70
	 *
71
	 * @return string
72
	 * @since 17.0.0
73
	 */
74
	public function getID(): string {
75
		return 'announcementcenter';
76
	}
77
78
	/**
79
	 * Human readable name describing the notifier
80
	 *
81
	 * @return string
82
	 * @since 17.0.0
83
	 */
84
	public function getName(): string {
85
		return $this->l10nFactory->get('announcementcenter')->t('Announcements');
86
	}
87
88
	/**
89
	 * @param INotification $notification
90
	 * @param string $languageCode The code of the language that should be used to prepare the notification
91
	 * @return INotification
92
	 * @throws \InvalidArgumentException When the notification was not prepared by a notifier
93
	 */
94 2
	public function prepare(INotification $notification, string $languageCode): INotification {
95 2
		if ($notification->getApp() !== 'announcementcenter') {
96
			// Not my app => throw
97 1
			throw new \InvalidArgumentException('Unknown app');
98
		}
99
100
		// Read the language from the notification
101 1
		$l = $this->l10nFactory->get('announcementcenter', $languageCode);
102
103 1
		$i = $notification->getSubject();
104 1
		if ($i !== 'announced') {
105
			// Unknown subject => Unknown notification => throw
106 1
			throw new \InvalidArgumentException('Unknown subject');
107
		}
108
109
		try {
110
			$announcement = $this->manager->getAnnouncement((int)$notification->getObjectId());
111
		} catch (AnnouncementDoesNotExistException $e) {
112
			throw new AlreadyProcessedException();
113
		}
114
115
		$params = $notification->getSubjectParameters();
116
		$user = $this->userManager->get($params[0]);
117
		if ($user instanceof IUser) {
0 ignored issues
show
Bug introduced by
The class OCP\IUser does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
118
			$displayName = $user->getDisplayName();
119
		} else {
120
			$displayName = $params[0];
121
		}
122
123
		$link = $this->urlGenerator->linkToRouteAbsolute('announcementcenter.page.index', [
124
			'announcement' => $notification->getObjectId(),
125
		]);
126
127
		if ($announcement->getMessage() !== '') {
128
			$notification->setParsedMessage($announcement->getMessage());
129
		}
130
		$notification->setRichSubject(
131
				$l->t('{user} announced “{announcement}”'),
132
				[
133
					'user' => [
134
						'type' => 'user',
135
						'id' => $params[0],
136
						'name' => $displayName,
137
					],
138
					'announcement' => [
139
						'type' => 'announcement',
140
						'id' => $notification->getObjectId(),
141
						'name' => $announcement->getSubject(),
142
						'link' => $link,
143
					],
144
				]
145
			)
146
			->setLink($link)
147
			->setIcon($this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('announcementcenter', 'announcementcenter-dark.svg')));
148
149
		$placeholders = $replacements = [];
150
		foreach ($notification->getRichSubjectParameters() as $placeholder => $parameter) {
151
			$placeholders[] = '{' . $placeholder . '}';
152
			$replacements[] = $parameter['name'];
153
		}
154
155
		$notification->setParsedSubject(str_replace($placeholders, $replacements, $notification->getRichSubject()));
156
157
		return $notification;
158
	}
159
}
160