Completed
Pull Request — master (#53)
by Joas
38:30
created

Notifier   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 14.63%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 85
ccs 6
cts 41
cp 0.1463
rs 10

2 Methods

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