Completed
Push — master ( 5b1111...8ace28 )
by Joas
10s
created

Notifier   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 83
ccs 38
cts 38
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B prepare() 0 55 4
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 OCP\IURLGenerator;
30
use OCP\IUser;
31
use OCP\IUserManager;
32
use OCP\L10N\IFactory;
33
use OCP\Notification\INotification;
34
use OCP\Notification\INotifier;
35
36
class Notifier implements INotifier {
37
38
	/** @var IFactory */
39
	protected $l10nFactory;
40
41
	/** @var Manager */
42
	protected $manager;
43
44
	/** @var IUserManager */
45
	protected $userManager;
46
47
	/** @var IURLGenerator */
48
	protected $urlGenerator;
49
50 7
	public function __construct(Manager $manager, IFactory $l10nFactory, IUserManager $userManager, IURLGenerator $urlGenerator) {
51 7
		$this->manager = $manager;
52 7
		$this->l10nFactory = $l10nFactory;
53 7
		$this->userManager = $userManager;
54 7
		$this->urlGenerator = $urlGenerator;
55 7
	}
56
57
	/**
58
	 * @param INotification $notification
59
	 * @param string $languageCode The code of the language that should be used to prepare the notification
60
	 * @return INotification
61
	 * @throws \InvalidArgumentException When the notification was not prepared by a notifier
62
	 */
63 5
	public function prepare(INotification $notification, $languageCode): INotification {
64 5
		if ($notification->getApp() !== 'announcementcenter') {
65
			// Not my app => throw
66 1
			throw new \InvalidArgumentException('Unknown app');
67
		}
68
69
		// Read the language from the notification
70 4
		$l = $this->l10nFactory->get('announcementcenter', $languageCode);
71
72 4
		$i = $notification->getSubject();
73 4
		if ($i !== 'announced') {
74
			// Unknown subject => Unknown notification => throw
75 1
			throw new \InvalidArgumentException('Unknown subject');
76
		}
77
78 3
		$params = $notification->getSubjectParameters();
79 3
		$user = $this->userManager->get($params[0]);
80 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...
81 1
			$displayName = $user->getDisplayName();
82
		} else {
83 2
			$displayName = $params[0];
84
		}
85
86 3
		$announcement = $this->manager->getAnnouncement((int)$notification->getObjectId(), true, false, false);
87 3
		$subject = str_replace("\n", ' ', $announcement['subject']);
88 3
		$parsedParameters = [$displayName, $subject];
89
90 3
		$link = $this->urlGenerator->linkToRouteAbsolute('announcementcenter.page.index', [
91 3
			'announcement' => $notification->getObjectId(),
92
		]);
93
94 3
		$notification->setParsedMessage($announcement['message'])
95 3
			->setRichSubject(
96 3
				$l->t('{user} announced “{announcement}”'),
97
				[
98
					'user' => [
99 3
						'type' => 'user',
100 3
						'id' => $params[0],
101 3
						'name' => $displayName,
102
					],
103
					'announcement' => [
104 3
						'type' => 'announcement',
105 3
						'id' => $notification->getObjectId(),
106 3
						'name' => $subject,
107 3
						'link' => $link,
108
					],
109
				]
110
			)
111 3
			->setParsedSubject(
112 3
				$l->t('%1$s announced “%2$s”', $parsedParameters)
113
			)
114 3
			->setLink($link)
115 3
			->setIcon($this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('announcementcenter', 'announcementcenter-dark.svg')));
116 3
		return $notification;
117
	}
118
}
119