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

Notifier   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 106
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 106
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 72 10
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\Comments\ICommentsManager;
29
use OCP\Comments\NotFoundException;
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 Manager */
39
	protected $manager;
40
41
	/** @var ICommentsManager */
42
	protected $commentsManager;
43
44
	/** @var IFactory */
45
	protected $l10nFactory;
46
47
	/** @var IUserManager */
48
	protected $userManager;
49
50 6
	/**
51 6
	 * @param Manager $manager
52 6
	 * @param ICommentsManager $commentsManager
53 6
	 * @param IFactory $l10nFactory
54 6
	 * @param IUserManager $userManager
55
	 */
56
	public function __construct(Manager $manager, ICommentsManager $commentsManager, IFactory $l10nFactory, IUserManager $userManager) {
57
		$this->manager = $manager;
58
		$this->commentsManager = $commentsManager;
59
		$this->userManager = $userManager;
60
		$this->l10nFactory = $l10nFactory;
61
	}
62 5
63 5
	/**
64
	 * @param INotification $notification
65 1
	 * @param string $languageCode The code of the language that should be used to prepare the notification
66
	 * @return INotification
67
	 * @throws \InvalidArgumentException When the notification was not prepared by a notifier
68
	 */
69 4
	public function prepare(INotification $notification, $languageCode) {
70
		if ($notification->getApp() !== 'announcementcenter') {
71 4
			// Not my app => throw
72
			throw new \InvalidArgumentException();
73 4
		}
74 3
75 3
		// Read the language from the notification
76 3
		$l = $this->l10nFactory->get('announcementcenter', $languageCode);
77 1
78 1
		switch ($notification->getSubject()) {
79
			// Deal with known subjects
80 3
			case 'announced':
81 3
				$params = $notification->getSubjectParameters();
82
				$user = $this->userManager->get($params[0]);
83 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...
84 3
					$params[0] = $user->getDisplayName();
85 3
				}
86 3
87 3
				$announcement = $this->manager->getAnnouncement((int) $notification->getObjectId(), false, false, false);
88
				$params[] = str_replace("\n", ' ', $announcement['subject']);
89 1
90
				$notification->setParsedMessage($announcement['message'])
91 1
					->setParsedSubject(
92 1
						(string) $l->t('%1$s announced “%2$s”', $params)
93
					);
94
				return $notification;
95
96
			case 'mention':
97
				try {
98
					$comment = $this->commentsManager->get($notification->getObjectId());
99
				} 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...
100
					// needs to be converted to InvalidArgumentException, otherwise none Notifications will be shown at all
101
					throw new \InvalidArgumentException('Comment not found', 0, $e);
102
				}
103
104
				$displayName = $comment->getActorId();
105
				$isDeletedActor = $comment->getActorType() === ICommentsManager::DELETED_USER;
106
				if ($comment->getActorType() === 'users') {
107
					$commenter = $this->userManager->get($comment->getActorId());
108
					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...
109
						$displayName = $commenter->getDisplayName();
110
					}
111
				}
112
113
				$parameters = $notification->getSubjectParameters();
114
				if ($parameters[0] !== 'announcement') {
115
					throw new \InvalidArgumentException('Unsupported comment object');
116
				}
117
118
				$announcement = $this->manager->getAnnouncement((int) $parameters[1], false, false, false);
119
				$announcementSubject = str_replace("\n", ' ', $announcement['subject']);
120
				if ($isDeletedActor) {
121
					$subject = $l->t(
122
						'A (now) deleted user mentioned you in a comment on “%1$s”.',
123
						[$announcementSubject]
124
					);
125
				} else {
126
					$subject = $l->t(
127
						'%1$s mentioned you in a comment on “%2$s”.',
128
						[$displayName, $announcementSubject]
129
					);
130
				}
131
				$notification->setParsedMessage($comment->getMessage())
132
					->setParsedSubject($subject);
133
134
				return $notification;
135
136
			default:
137
				// Unknown subject => Unknown notification => throw
138
				throw new \InvalidArgumentException();
139
		}
140
	}
141
}
142