Completed
Push — master ( dffc9e...8b08ca )
by Maxence
05:51 queued 03:24
created

NotificationService::notificationRequested()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 52

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 52
rs 9.0472
c 0
b 0
f 0
cc 4
nc 3
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
6
/**
7
 * Circles - Bring cloud-users closer together.
8
 *
9
 * This file is licensed under the Affero General Public License version 3 or
10
 * later. See the COPYING file.
11
 *
12
 * @author Maxence Lange <[email protected]>
13
 * @author Vinicius Cubas Brand <[email protected]>
14
 * @author Daniel Tygel <[email protected]>
15
 *
16
 * @copyright 2017
17
 * @license GNU AGPL version 3 or any later version
18
 *
19
 * This program is free software: you can redistribute it and/or modify
20
 * it under the terms of the GNU Affero General Public License as
21
 * published by the Free Software Foundation, either version 3 of the
22
 * License, or (at your option) any later version.
23
 *
24
 * This program is distributed in the hope that it will be useful,
25
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27
 * GNU Affero General Public License for more details.
28
 *
29
 * You should have received a copy of the GNU Affero General Public License
30
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31
 *
32
 */
33
34
35
namespace OCA\Circles\Service;
36
37
38
use daita\MySmallPhpTools\Traits\Nextcloud\nc22\TNC22Logger;
39
use OCA\Circles\AppInfo\Application;
40
use OCA\Circles\Db\MemberRequest;
41
use OCA\Circles\Exceptions\RequestBuilderException;
42
use OCA\Circles\Model\Member;
43
use OCP\IURLGenerator;
44
use OCP\Notification\IManager as INotificationManager;
45
use OCP\Notification\INotification;
46
47
48
/**
49
 * Class NotificationService
50
 *
51
 * @package OCA\Circles\Service
52
 */
53
class NotificationService {
54
55
56
	use TNC22Logger;
57
58
59
	/** @var IURLGenerator */
60
	private $urlGenerator;
61
62
	/** @var INotificationManager */
63
	private $notificationManager;
64
65
	/** @var MemberRequest */
66
	private $memberRequest;
67
68
	/** @var TimezoneService */
69
	private $timezoneService;
70
71
72
	/**
73
	 * NotificationService constructor.
74
	 *
75
	 * @param IURLGenerator $urlGenerator
76
	 * @param INotificationManager $notificationManager
77
	 * @param MemberRequest $memberRequest
78
	 * @param TimezoneService $timezoneService
79
	 */
80
	public function __construct(
81
		IURLGenerator $urlGenerator,
82
		INotificationManager $notificationManager,
83
		MemberRequest $memberRequest,
84
		TimezoneService $timezoneService
85
	) {
86
		$this->urlGenerator = $urlGenerator;
87
		$this->notificationManager = $notificationManager;
88
		$this->memberRequest = $memberRequest;
89
		$this->timezoneService = $timezoneService;
90
91
		$this->setup('app', Application::APP_ID);
92
	}
93
94
95
	/**
96
	 * @param Member $member
97
	 */
98 View Code Duplication
	public function notificationInvited(Member $member): void {
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...
99
		if ($member->getUserType() !== Member::TYPE_USER || !$member->isLocal()) {
100
			return;
101
		}
102
103
		$this->deleteNotification('member', $member->getId());
104
		$notification = $this->createMemberNotification(
105
			$member->getUserId(),
106
			$member->getId(),
107
			'invitation'
108
		);
109
110
		$declineAction = $notification->createAction();
111
		$declineUrl = $this->linkToOCS('circles.Local.circleLeave', ['circleId' => $member->getCircleId()]);
112
		$declineAction->setLabel('refuse')
113
					  ->setLink($this->urlGenerator->getAbsoluteURL($declineUrl), 'PUT');
114
		$notification->addAction($declineAction);
115
116
		$acceptAction = $notification->createAction();
117
		$acceptUrl = $this->linkToOCS('circles.Local.circleJoin', ['circleId' => $member->getCircleId()]);
118
		$acceptAction->setLabel('accept')
119
					 ->setLink($this->urlGenerator->getAbsoluteURL($acceptUrl), 'PUT');
120
		$notification->addAction($acceptAction);
121
122
		$this->notificationManager->notify($notification);
123
	}
124
125
126
	/**
127
	 * @param Member $member
128
	 *
129
	 * @throws RequestBuilderException
130
	 */
131
	public function notificationRequested(Member $member): void {
132
//		if ($member->getUserType() !== Member::TYPE_USER || !$member->isLocal()) {
133
//			return;
134
//		}
135
136
		$this->deleteNotification('member', $member->getId());
137
138
		$moderators = $this->memberRequest->getInheritedMembers(
139
			$member->getCircleId(),
140
			false,
141
			Member::LEVEL_MODERATOR
142
		);
143
144
		foreach ($moderators as $moderator) {
145
			if ($moderator->getUserType() !== Member::TYPE_USER || !$moderator->isLocal()) {
146
				continue;
147
			}
148
149
			$notification = $this->createMemberNotification(
150
				$moderator->getUserId(),
151
				$member->getId(),
152
				'joinRequest'
153
			);
154
155
			$declineAction = $notification->createAction();
156
			$declineUrl = $this->linkToOCS(
157
				'circles.Local.memberRemove',
158
				[
159
					'circleId' => $member->getCircleId(),
160
					'memberId' => $member->getId()
161
				]
162
			);
163
			$declineAction->setLabel('refuse')
164
						  ->setLink($this->urlGenerator->getAbsoluteURL($declineUrl), 'DELETE');
165
			$notification->addAction($declineAction);
166
167
			$acceptAction = $notification->createAction();
168
			$acceptUrl = $this->linkToOCS(
169
				'circles.Local.memberConfirm',
170
				[
171
					'circleId' => $member->getCircleId(),
172
					'memberId' => $member->getId()
173
				]
174
			);
175
			$acceptAction->setLabel('accept')
176
						 ->setLink($this->urlGenerator->getAbsoluteURL($acceptUrl), 'PUT');
177
			$notification->addAction($acceptAction);
178
179
			$this->notificationManager->notify($notification);
180
		}
181
182
	}
183
184
185
	/**
186
	 * @param string $object
187
	 * @param string $objectId
188
	 */
189
	public function deleteNotification(string $object, string $objectId) {
0 ignored issues
show
Unused Code introduced by
The parameter $objectId is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $object is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
190
//		if ($objectId === '') {
191
//			return;
192
//		}
193
//
194
//		$notification = $this->notificationManager->createNotification();
195
//		$notification->setApp('circles')
196
//					 ->setObject($object, $objectId);
197
//
198
//		$this->notificationManager->markProcessed($notification);
199
	}
200
201
202
	/**
203
	 * @param string $userId
204
	 * @param string $memberId
205
	 * @param string $subject
206
	 *
207
	 * @return INotification
208
	 */
209
	private function createMemberNotification(
210
		string $userId,
211
		string $memberId,
212
		string $subject
213
	): INotification {
214
		$notification = $this->notificationManager->createNotification();
215
		$notification->setApp('circles')
216
					 ->setDateTime($this->timezoneService->getDateTime())
217
					 ->setUser($userId)
218
					 ->setObject('member', $memberId)
219
					 ->setSubject($subject);
220
221
		return $notification;
222
	}
223
224
225
	/**
226
	 * @param string $route
227
	 * @param array $params
228
	 *
229
	 * @return string
230
	 */
231
	private function linkToOCS(string $route, array $params = []): string {
232
		$absolute = $this->urlGenerator->linkToOCSRouteAbsolute($route, $params);
233
234
		return parse_url($absolute, PHP_URL_PATH);
235
	}
236
237
}
238
239