Completed
Pull Request — master (#156)
by Roeland
105:29 queued 104:30
created

Notifier::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
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\IManager as INotificationManager;
35
use OCP\Notification\INotification;
36
use OCP\Notification\INotifier;
37
38
class Notifier implements INotifier {
39
40
	/** @var Manager */
41
	protected $manager;
42
43
	/** @var IFactory */
44
	protected $l10nFactory;
45
46
	/** @var INotificationManager */
47
	protected $notificationManager;
48
49
	/** @var IUserManager */
50
	protected $userManager;
51
52
	/** @var IURLGenerator */
53
	protected $urlGenerator;
54
55 7 View Code Duplication
	public function __construct(Manager $manager,
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
56
								IFactory $l10nFactory,
57
								INotificationManager $notificationManager,
58
								IUserManager $userManager,
59
								IURLGenerator $urlGenerator) {
60 7
		$this->manager = $manager;
61 7
		$this->l10nFactory = $l10nFactory;
62 7
		$this->notificationManager = $notificationManager;
63 7
		$this->userManager = $userManager;
64 7
		$this->urlGenerator = $urlGenerator;
65 7
	}
66
67
	/**
68
	 * Identifier of the notifier, only use [a-z0-9_]
69
	 *
70
	 * @return string
71
	 * @since 17.0.0
72
	 */
73
	public function getID(): string {
74
		return 'announcementcenter';
75
	}
76
77
	/**
78
	 * Human readable name describing the notifier
79
	 *
80
	 * @return string
81
	 * @since 17.0.0
82
	 */
83
	public function getName(): string {
84
		return $this->l10nFactory->get('announcementcenter')->t('Announcements');
85
	}
86
87
	/**
88
	 * @param INotification $notification
89
	 * @param string $languageCode The code of the language that should be used to prepare the notification
90
	 * @return INotification
91
	 * @throws \InvalidArgumentException When the notification was not prepared by a notifier
92
	 */
93 6
	public function prepare(INotification $notification, string $languageCode): INotification {
94 6
		if ($notification->getApp() !== 'announcementcenter') {
95
			// Not my app => throw
96 1
			throw new \InvalidArgumentException('Unknown app');
97
		}
98
99
		// Read the language from the notification
100 5
		$l = $this->l10nFactory->get('announcementcenter', $languageCode);
101
102 5
		$i = $notification->getSubject();
103 5
		if ($i !== 'announced') {
104
			// Unknown subject => Unknown notification => throw
105 1
			throw new \InvalidArgumentException('Unknown subject');
106
		}
107
108
		try {
109 4
			$announcement = $this->manager->getAnnouncement((int)$notification->getObjectId());
110 1
		} catch (AnnouncementDoesNotExistException $e) {
111 1
			$this->notificationManager->markProcessed($notification);
112 1
			throw new \InvalidArgumentException('Announcement was deleted');
113
		}
114
115 3
		$params = $notification->getSubjectParameters();
116 3
		$user = $this->userManager->get($params[0]);
117 3
		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 1
			$displayName = $user->getDisplayName();
119
		} else {
120 2
			$displayName = $params[0];
121
		}
122
123 3
		$link = $this->urlGenerator->linkToRouteAbsolute('announcementcenter.page.index', [
124 3
			'announcement' => $notification->getObjectId(),
125
		]);
126
127 3
		$notification->setParsedMessage($announcement->getMessage())
128 3
			->setRichSubject(
129 3
				$l->t('{user} announced “{announcement}”'),
130
				[
131
					'user' => [
132 3
						'type' => 'user',
133 3
						'id' => $params[0],
134 3
						'name' => $displayName,
135
					],
136
					'announcement' => [
137 3
						'type' => 'announcement',
138 3
						'id' => $notification->getObjectId(),
139 3
						'name' => $announcement->getSubject(),
140 3
						'link' => $link,
141
					],
142
				]
143
			)
144 3
			->setLink($link)
145 3
			->setIcon($this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('announcementcenter', 'announcementcenter-dark.svg')));
146
147 3
		$placeholders = $replacements = [];
148 3
		foreach ($notification->getRichSubjectParameters() as $placeholder => $parameter) {
149 3
			$placeholders[] = '{' . $placeholder . '}';
150 3
			$replacements[] = $parameter['name'];
151
		}
152
153 3
		$notification->setParsedSubject(str_replace($placeholders, $replacements, $notification->getRichSubject()));
154
155 3
		return $notification;
156
	}
157
}
158