Completed
Push — master ( a7d2b9...a7d2b9 )
by Maxence
06:32 queued 02:55
created

EventsService::onCircleRequestInvitation()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 12

Duplication

Lines 19
Ratio 100 %

Importance

Changes 0
Metric Value
dl 19
loc 19
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 12
nc 3
nop 2
1
<?php
2
/**
3
 * Circles - Bring cloud-users closer together.
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Maxence Lange <[email protected]>
9
 * @copyright 2017
10
 * @license GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
 *
25
 */
26
27
namespace OCA\Circles\Service;
28
29
use OCA\Circles\Db\CirclesRequest;
30
use OCA\Circles\Model\Circle;
31
use OCA\Circles\Model\Member;
32
use OCP\Activity\IManager;
33
use OCP\IUser;
34
use OCP\IUserManager;
35
36
class EventsService {
37
38
39
	/** @var string */
40
	private $userId;
41
42
	/** @var IManager */
43
	private $activityManager;
44
45
	/** @var IUserManager */
46
	private $userManager;
47
48
	/** @var CirclesRequest */
49
	private $circlesRequest;
50
51
	/** @var MiscService */
52
	private $miscService;
53
54
55
	/**
56
	 * Events constructor.
57
	 *
58
	 * @param string $userId
59
	 * @param IManager $activityManager
60
	 * @param IUserManager $userManager
61
	 * @param CirclesRequest $circlesRequest
62
	 * @param MiscService $miscService
63
	 */
64
	public function __construct(
65
		$userId, IManager $activityManager, IUserManager $userManager,
66
		CirclesRequest $circlesRequest, MiscService $miscService
67
	) {
68
		$this->userId = $userId;
69
		$this->activityManager = $activityManager;
70
		$this->userManager = $userManager;
71
		$this->circlesRequest = $circlesRequest;
72
		$this->miscService = $miscService;
73
	}
74
75
76
	/**
77
	 * onCircleCreation()
78
	 *
79
	 * Called when a circle is created.
80
	 * Broadcast an activity to the cloud
81
	 * We won't do anything if the circle is not PUBLIC or PRIVATE
82
	 *
83
	 * @param Circle $circle
84
	 */
85
	public function onCircleCreation(Circle $circle) {
86
87
		if ($circle->getType() !== Circle::CIRCLES_PUBLIC
88
			&& $circle->getType() !== Circle::CIRCLES_PRIVATE
89
		) {
90
			return;
91
		}
92
93
		$this->userManager->callForSeenUsers(
94
			function($user) use ($circle) {
95
				/** @var IUser $user */
96
97
				$event = $this->generateEvent('circles_creation');
98
				$event->setAffectedUser($user->getUID());
99
				$event->setSubject('create', ['circle' => json_encode($circle)]);
100
101
				$this->activityManager->publish($event);
102
			}
103
		);
104
	}
105
106
107
	/**
108
	 * onCircleDestruction()
109
	 *
110
	 * Called when a circle is destroyed.
111
	 * Broadcast an activity on its members.
112
	 * We won't do anything if the circle is PERSONAL
113
	 *
114
	 * @param Circle $circle
115
	 */
116 View Code Duplication
	public function onCircleDestruction(Circle $circle) {
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...
117
		if ($circle->getType() === Circle::CIRCLES_PERSONAL) {
118
			return;
119
		}
120
121
		$members = $this->circlesRequest->getMembers($circle->getId(), Member::LEVEL_MEMBER);
122
		foreach ($members AS $user) {
123
124
			$event = $this->generateEvent('circles_creation');
125
			$event->setAffectedUser($user->getUserId());
126
			$event->setSubject('delete', ['circle' => json_encode($circle)]);
127
128
			$this->activityManager->publish($event);
129
		}
130
	}
131
132
133
	/**
134
	 * onCircleInvitation()
135
	 *
136
	 * Called when a member is invited to a circle.
137
	 * Broadcast an activity to the invited member and to the moderators of the circle.
138
	 *
139
	 * @param Circle $circle
140
	 * @param Member $member
141
	 */
142 View Code Duplication
	public function onCircleInvitation(Circle $circle, Member $member) {
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...
143
		if ($circle->getType() !== Circle::CIRCLES_PRIVATE) {
144
			return;
145
		}
146
147
		$event = $this->generateEvent('circles_invitation');
148
		$event->setSubject(
149
			'invited', ['circle' => json_encode($circle), 'member' => json_encode($member)]
150
		);
151
152
		$event->setAffectedUser($member->getUserId());
153
		$this->activityManager->publish($event);
154
155
		$members = $this->circlesRequest->getMembers($circle->getId(), Member::LEVEL_MODERATOR);
156
		foreach ($members AS $user) {
157
158
			$event->setAffectedUser($user->getUserId());
159
			$this->activityManager->publish($event);
160
		}
161
	}
162
163
164
	/**
165
	 * onCircleRequestInvitation()
166
	 *
167
	 * Called when a member request an invitation to a private circle.
168
	 * Broadcast an activity to the requester and to the moderators of the circle.
169
	 *
170
	 * @param Circle $circle
171
	 * @param Member $member
172
	 */
173 View Code Duplication
	public function onCircleRequestInvitation(Circle $circle, Member $member) {
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...
174
		if ($circle->getType() !== Circle::CIRCLES_PRIVATE) {
175
			return;
176
		}
177
178
		$event = $this->generateEvent('circles_invitation');
179
		$event->setSubject(
180
			'request', ['circle' => json_encode($circle), 'member' => json_encode($member)]
181
		);
182
183
		$event->setAffectedUser($member->getUserId());
184
		$this->activityManager->publish($event);
185
186
		$members = $this->circlesRequest->getMembers($circle->getId(), Member::LEVEL_MODERATOR);
187
		foreach ($members AS $user) {
188
			$event->setAffectedUser($user->getUserId());
189
			$this->activityManager->publish($event);
190
		}
191
	}
192
193
194
	/**
195
	 * onCircleNewMember()
196
	 *
197
	 * Called when a member is added to a circle.
198
	 * Broadcast an activity to the new member and to the moderators of the circle.
199
	 * We won't do anything if the circle is PERSONAL
200
	 *
201
	 * @param Circle $circle
202
	 * @param Member $member
203
	 */
204 View Code Duplication
	public function onCircleNewMember(Circle $circle, Member $member) {
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...
205
		if ($circle->getType() === Circle::CIRCLES_PERSONAL) {
206
			return;
207
		}
208
209
		$event = $this->generateEvent('circles_population');
210
		$event->setSubject(
211
			'new_member', ['circle' => json_encode($circle), 'member' => json_encode($member)]
212
		);
213
214
		$members = $this->circlesRequest->getMembers($circle->getId(), Member::LEVEL_MODERATOR);
215
		foreach ($members AS $user) {
216
			$event->setAffectedUser($user->getUserId());
217
			$this->activityManager->publish($event);
218
		}
219
	}
220
221
222
	/**
223
	 * onCircleMemberLeaving()
224
	 *
225
	 * Called when a member is removed from a circle.
226
	 * Broadcast an activity to the new member and to the moderators of the circle.
227
	 * We won't do anything if the circle is PERSONAL
228
	 *
229
	 * @param Circle $circle
230
	 * @param Member $member
231
	 */
232 View Code Duplication
	public function onCircleMemberLeaving(Circle $circle, Member $member) {
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...
233
		if ($circle->getType() === Circle::CIRCLES_PERSONAL) {
234
			return;
235
		}
236
237
		$event = $this->generateEvent('circles_population');
238
		$event->setSubject(
239
			'remove_member', ['circle' => json_encode($circle), 'member' => json_encode($member)]
240
		);
241
242
		$members = $this->circlesRequest->getMembers($circle->getId(), Member::LEVEL_MODERATOR);
243
		foreach ($members AS $user) {
244
			$event->setAffectedUser($user->getUserId());
245
			$this->activityManager->publish($event);
246
		}
247
	}
248
249
250
	/**
251
	 * generateEvent()
252
	 * Create an Activity Event with the basic settings for the app.
253
	 *
254
	 * @param $type
255
	 *
256
	 * @return \OCP\Activity\IEvent
257
	 */
258
	private function generateEvent($type) {
259
		$event = $this->activityManager->generateEvent();
260
		$event->setApp('circles')
261
			  ->setType($type)
262
			  ->setAuthor($this->userId);
263
264
		return $event;
265
	}
266
267
268
}