Completed
Pull Request — master (#52)
by
unknown
03:15
created

BackgroundJob::createPublicity()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 31
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 31
ccs 25
cts 25
cp 1
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 23
nc 4
nop 5
crap 3
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;
25
26
use OC\BackgroundJob\QueuedJob;
27
use OCP\Activity\IEvent;
28
use OCP\Activity\IManager;
29
use OCP\IGroup;
30
use OCP\IGroupManager;
31
use OCP\IURLGenerator;
32
use OCP\IUser;
33
use OCP\IUserManager;
34
use OCP\Notification\IManager as INotificationManager;
35
use OCP\Notification\INotification;
36
37
class BackgroundJob extends QueuedJob {
38
	/** @var INotificationManager */
39
	protected $notificationManager;
40
41
	/** @var IUserManager */
42
	private $userManager;
43
44
	/** @var IGroupManager */
45
	private $groupManager;
46
47
	/** @var IURLGenerator */
48
	private $urlGenerator;
49
50
	/** @var Manager */
51
	private $manager;
52
53
	/** @var IManager */
54
	private $activityManager;
55
56
	/** @var array */
57
	protected $notifiedUsers = [];
58
59
	/**
60
	 * @param IUserManager $userManager
61
	 * @param IGroupManager $groupManager
62
	 * @param IManager $activityManager
63
	 * @param INotificationManager $notificationManager
64
	 * @param IURLGenerator $urlGenerator
65
	 * @param Manager $manager
66
	 */
67 9
	public function __construct(
68
		IUserManager $userManager,
69
		IGroupManager $groupManager,
70
		IManager $activityManager,
71
		INotificationManager $notificationManager,
72
		IURLGenerator $urlGenerator,
73
		Manager $manager) {
74 9
		$this->userManager = $userManager;
75 9
		$this->groupManager = $groupManager;
76 9
		$this->activityManager = $activityManager;
77 9
		$this->notificationManager = $notificationManager;
78 9
		$this->urlGenerator = $urlGenerator;
79 9
		$this->manager = $manager;
80 9
	}
81
82
	/**
83
	 * @param array $arguments
84
	 */
85 3
	public function run($arguments) {
86
		try {
87 3
			$announcement = $this->manager->getAnnouncement($arguments['id'], false, true);
88 3
		} catch (\InvalidArgumentException $e) {
89
			// Announcement was deleted in the meantime, so no need to announce it anymore
90
			// So we die silently
91 1
			return;
92
		}
93
94 2
		$this->createPublicity($announcement['id'], $announcement['author'], $announcement['time'], $announcement['groups'], $arguments);
95 2
	}
96
97
	/**
98
	 * @param int $id
99
	 * @param string $authorId
100
	 * @param int $timeStamp
101
	 * @param string[] $groups
102
	 * @param array $publicity
103
	 */
104 2
	protected function createPublicity($id, $authorId, $timeStamp, array $groups, array $publicity) {
105 2
		$event = $this->activityManager->generateEvent();
106 2
		$event->setApp('announcementcenter')
107 2
			->setType('announcementcenter')
108 2
			->setAuthor($authorId)
109 2
			->setTimestamp($timeStamp)
110 2
			->setSubject('announcementsubject#' . $id, [$authorId])
111 2
			->setMessage('announcementmessage#' . $id, [$authorId])
112 2
			->setObject('announcement', $id);
113
114 2
		$dateTime = new \DateTime();
115 2
		$dateTime->setTimestamp($timeStamp);
116
117 2
		$notification = $this->notificationManager->createNotification();
118 2
		$notification->setApp('announcementcenter')
119 2
			->setDateTime($dateTime)
120 2
			->setObject('announcement', $id)
121 2
			->setSubject('announced', [$authorId])
122 2
			->setLink($this->urlGenerator->linkToRouteAbsolute('announcementcenter.page.index'));
123
124
		// Nextcloud 11+
125 2
		if (method_exists($notification, 'setIcon')) {
126 2
			$notification->setIcon($this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('announcementcenter', 'announcementcenter-dark.svg')));
127 2
		}
128
129 2
		if (in_array('everyone', $groups)) {
130 1
			$this->createPublicityEveryone($authorId, $event, $notification, $publicity);
131 1
		} else {
132 1
			$this->createPublicityGroups($authorId, $event, $notification, $groups, $publicity);
133
		}
134 2
	}
135
136
	/**
137
	 * @param string $authorId
138
	 * @param IEvent $event
139
	 * @param INotification $notification
140
	 * @param array $publicity
141
	 */
142
	protected function createPublicityEveryone($authorId, IEvent $event, INotification $notification, array $publicity) {
143 2
		$this->userManager->callForSeenUsers(function(IUser $user) use ($authorId, $event, $notification, $publicity) {
144 2
			if (!empty($publicity['activities'])) {
145 1
				$event->setAffectedUser($user->getUID());
146 1
				$this->activityManager->publish($event);
147 1
			}
148
149 2 View Code Duplication
			if (!empty($publicity['notifications']) && $authorId !== $user->getUID()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
150 1
				$notification->setUser($user->getUID());
151 1
				$this->notificationManager->notify($notification);
152 1
			}
153 2
		});
154 2
	}
155
156
	/**
157
	 * @param string $authorId
158
	 * @param IEvent $event
159
	 * @param INotification $notification
160
	 * @param string[] $groups
161
	 * @param array $publicity
162
	 */
163 2
	protected function createPublicityGroups($authorId, IEvent $event, INotification $notification, array $groups, array $publicity) {
164 2
		foreach ($groups as $gid) {
165 2
			$group = $this->groupManager->get($gid);
166 2
			if (!($group instanceof IGroup)) {
0 ignored issues
show
Bug introduced by
The class OCP\IGroup 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...
167 2
				continue;
168
			}
169
170 2
			$users = $group->getUsers();
171 2
			foreach ($users as $user) {
172 2
				$uid = $user->getUID();
173 2
				if (isset($this->notifiedUsers[$uid]) || $user->getLastLogin() === 0) {
174 2
					continue;
175
				}
176
177 2
				if (!empty($publicity['activities'])) {
178 1
					$event->setAffectedUser($uid);
179 1
					$this->activityManager->publish($event);
180 1
				}
181
182 2 View Code Duplication
				if (!empty($publicity['notifications']) && $authorId !== $uid) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
183 1
					$notification->setUser($uid);
184 1
					$this->notificationManager->notify($notification);
185 1
				}
186
187 2
				$this->notifiedUsers[$uid] = true;
188 2
			}
189 2
		}
190 2
	}
191
}
192