Completed
Pull Request — master (#44)
by Joas
02:28
created

Notifier   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 1
dl 0
loc 109
ccs 26
cts 26
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
C prepare() 0 75 10
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016 Joas Schilling <[email protected]>
4
 * @copyright Copyright (c) 2016 Arthur Schiwon <[email protected]>
5
 *
6
 * @author Joas Schilling <[email protected]>
7
 * @author Arthur Schiwon <[email protected]>
8
 *
9
 * @license GNU AGPL version 3 or any later version
10
 *
11
 * This program is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License as
13
 * published by the Free Software Foundation, either version 3 of the
14
 * License, or (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License
22
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23
 *
24
 */
25
26
namespace OCA\AnnouncementCenter\Notification;
27
28
29
use OCA\AnnouncementCenter\Manager;
30
use OCP\Comments\ICommentsManager;
31
use OCP\Comments\NotFoundException;
32
use OCP\IUser;
33
use OCP\IUserManager;
34
use OCP\L10N\IFactory;
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 ICommentsManager */
44
	protected $commentsManager;
45
46
	/** @var IFactory */
47
	protected $l10nFactory;
48
49
	/** @var IUserManager */
50 6
	protected $userManager;
51 6
52 6
	/**
53 6
	 * @param Manager $manager
54 6
	 * @param ICommentsManager $commentsManager
55
	 * @param IFactory $l10nFactory
56
	 * @param IUserManager $userManager
57
	 */
58
	public function __construct(Manager $manager, ICommentsManager $commentsManager, IFactory $l10nFactory, IUserManager $userManager) {
59
		$this->manager = $manager;
60
		$this->commentsManager = $commentsManager;
61
		$this->userManager = $userManager;
62 5
		$this->l10nFactory = $l10nFactory;
63 5
	}
64
65 1
	/**
66
	 * @param INotification $notification
67
	 * @param string $languageCode The code of the language that should be used to prepare the notification
68
	 * @return INotification
69 4
	 * @throws \InvalidArgumentException When the notification was not prepared by a notifier
70
	 */
71 4
	public function prepare(INotification $notification, $languageCode) {
72
		if ($notification->getApp() !== 'announcementcenter') {
73 4
			// Not my app => throw
74 3
			throw new \InvalidArgumentException();
75 3
		}
76 3
77 1
		// Read the language from the notification
78 1
		$l = $this->l10nFactory->get('announcementcenter', $languageCode);
79
80 3
		switch ($notification->getSubject()) {
81 3
			// Deal with known subjects
82
			case 'announced':
83 3
				$params = $notification->getSubjectParameters();
84 3
				$user = $this->userManager->get($params[0]);
85 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...
86 3
					$params[0] = $user->getDisplayName();
87 3
				}
88
89 1
				$announcement = $this->manager->getAnnouncement((int) $notification->getObjectId(), false, false, false);
90
				$params[] = str_replace("\n", ' ', $announcement['subject']);
91 1
92 1
				$notification->setParsedMessage($announcement['message'])
93
					->setParsedSubject(
94
						(string) $l->t('%1$s announced “%2$s”', $params)
95
					);
96
				return $notification;
97
98
			/**
99
			 * @author Arthur Schiwon <[email protected]>
100
			 */
101
			case 'mention':
102
				try {
103
					$comment = $this->commentsManager->get($notification->getObjectId());
104
				} catch(NotFoundException $e) {
0 ignored issues
show
Bug introduced by
The class OCP\Comments\NotFoundException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
105
					// needs to be converted to InvalidArgumentException, otherwise none Notifications will be shown at all
106
					throw new \InvalidArgumentException('Comment not found', 0, $e);
107
				}
108
109
				$displayName = $comment->getActorId();
110
				$isDeletedActor = $comment->getActorType() === ICommentsManager::DELETED_USER;
111
				if ($comment->getActorType() === 'users') {
112
					$commenter = $this->userManager->get($comment->getActorId());
113
					if ($commenter 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...
114
						$displayName = $commenter->getDisplayName();
115
					}
116
				}
117
118
				$parameters = $notification->getSubjectParameters();
119
				if ($parameters[0] !== 'announcement') {
120
					throw new \InvalidArgumentException('Unsupported comment object');
121
				}
122
123
				$announcement = $this->manager->getAnnouncement((int) $parameters[1], false, false, false);
124
				$announcementSubject = str_replace("\n", ' ', $announcement['subject']);
125
				if ($isDeletedActor) {
126
					$subject = $l->t(
127
						'A (now) deleted user mentioned you in a comment on “%1$s”.',
128
						[$announcementSubject]
129
					);
130
				} else {
131
					$subject = $l->t(
132
						'%1$s mentioned you in a comment on “%2$s”.',
133
						[$displayName, $announcementSubject]
134
					);
135
				}
136
				$notification->setParsedMessage($comment->getMessage())
137
					->setParsedSubject($subject);
138
139
				return $notification;
140
141
			default:
142
				// Unknown subject => Unknown notification => throw
143
				throw new \InvalidArgumentException();
144
		}
145
	}
146
}
147