Completed
Pull Request — master (#8)
by Joas
07:10
created

BackgroundJob::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 14
ccs 8
cts 8
cp 1
rs 9.4285
cc 1
eloc 13
nc 1
nop 6
crap 1
1
<?php
2
/**
3
 * @author Joas Schilling <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2016, Joas Schilling <[email protected]>
6
 * @license AGPL-3.0
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
22
namespace OCA\AnnouncementCenter;
23
24
use OC\BackgroundJob\QueuedJob;
25
use OCP\Activity\IEvent;
26
use OCP\Activity\IManager;
27
use OCP\IGroup;
28
use OCP\IGroupManager;
29
use OCP\IURLGenerator;
30
use OCP\IUser;
31
use OCP\IUserManager;
32
use OCP\Notification\IManager as INotificationManager;
33
use OCP\Notification\INotification;
34
35
class BackgroundJob extends QueuedJob {
36
	/** @var INotificationManager */
37
	protected $notificationManager;
38
39
	/** @var IUserManager */
40
	private $userManager;
41
42
	/** @var IGroupManager */
43
	private $groupManager;
44
45
	/** @var IURLGenerator */
46
	private $urlGenerator;
47
48
	/** @var Manager */
49
	private $manager;
50
51
	/** @var IManager */
52
	private $activityManager;
53
54
	/** @var array */
55
	protected $notifiedUsers = [];
56
57
	/**
58
	 * @param IUserManager $userManager
59
	 * @param IGroupManager $groupManager
60
	 * @param IManager $activityManager
61
	 * @param INotificationManager $notificationManager
62
	 * @param IURLGenerator $urlGenerator
63
	 * @param Manager $manager
64
	 */
65 7
	public function __construct(
66
		IUserManager $userManager,
67
		IGroupManager $groupManager,
68
		IManager $activityManager,
69
		INotificationManager $notificationManager,
70
		IURLGenerator $urlGenerator,
71
		Manager $manager) {
72 7
		$this->userManager = $userManager;
73 7
		$this->groupManager = $groupManager;
74 7
		$this->activityManager = $activityManager;
75 7
		$this->notificationManager = $notificationManager;
76 7
		$this->urlGenerator = $urlGenerator;
77 7
		$this->manager = $manager;
78 7
	}
79
80
	/**
81
	 * @param array $argument
82
	 */
83 3
	public function run($argument) {
84
		try {
85 3
			$announcement = $this->manager->getAnnouncement($argument['id'], false);
86 3
		} catch (\InvalidArgumentException $e) {
87
			// Announcement was deleted in the meantime, so no need to announce it anymore
88
			// So we die silently
89 1
			return;
90
		}
91
92 2
		$groups = $this->manager->getGroups($argument['id']);
93 2
		$this->createPublicity($announcement['id'], $announcement['author'], $announcement['time'], $groups);
94 2
	}
95
96
	/**
97
	 * @param int $id
98
	 * @param string $authorId
99
	 * @param int $timeStamp
100
	 * @param string[] $groups
101
	 */
102 2
	protected function createPublicity($id, $authorId, $timeStamp, array $groups) {
103 2
		$event = $this->activityManager->generateEvent();
104 2
		$event->setApp('announcementcenter')
105 2
			->setType('announcementcenter')
106 2
			->setAuthor($authorId)
107 2
			->setTimestamp($timeStamp)
108 2
			->setSubject('announcementsubject#' . $id, [$authorId])
109 2
			->setMessage('announcementmessage#' . $id, [$authorId])
110 2
			->setObject('announcement', $id);
111
112 2
		$dateTime = new \DateTime();
113 2
		$dateTime->setTimestamp($timeStamp);
114
115 2
		$notification = $this->notificationManager->createNotification();
116 2
		$notification->setApp('announcementcenter')
117 2
			->setDateTime($dateTime)
118 2
			->setObject('announcement', $id)
119 2
			->setSubject('announced', [$authorId])
120 2
			->setLink($this->urlGenerator->linkToRoute('announcementcenter.page.index'));
121
122 2
		if (in_array('everyone', $groups)) {
123 1
			$this->createPublicityEveryone($authorId, $event, $notification);
124 1
		} else {
125 1
			$this->createPublicityGroups($authorId, $event, $notification, $groups);
126
		}
127 2
	}
128
129
	/**
130
	 * @param string $authorId
131
	 * @param IEvent $event
132
	 * @param INotification $notification
133
	 */
134
	protected function createPublicityEveryone($authorId, IEvent $event, INotification $notification) {
135 1
		$this->userManager->callForAllUsers(function(IUser $user) use ($authorId, $event, $notification) {
136 1
			$event->setAffectedUser($user->getUID());
137 1
			$this->activityManager->publish($event);
138
139 1
			if ($authorId !== $user->getUID()) {
140 1
				$notification->setUser($user->getUID());
141 1
				$this->notificationManager->notify($notification);
142 1
			}
143 1
		});
144 1
	}
145
146
	/**
147
	 * @param string $authorId
148
	 * @param IEvent $event
149
	 * @param INotification $notification
150
	 * @param string[] $groups
151
	 */
152 1
	protected function createPublicityGroups($authorId, IEvent $event, INotification $notification, array $groups) {
153 1
		foreach ($groups as $gid) {
154 1
			$group = $this->groupManager->get($gid);
155 1
			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...
156 1
				continue;
157
			}
158
159 1
			$users = $group->getUsers();
160 1
			foreach ($users as $user) {
161 1
				$uid = $user->getUID();
162 1
				if (isset($this->notifiedUsers[$uid])) {
163 1
					continue;
164
				}
165
166 1
				$event->setAffectedUser($uid);
167 1
				$this->activityManager->publish($event);
168
169 1
				if ($authorId !== $uid) {
170 1
					$notification->setUser($uid);
171 1
					$this->notificationManager->notify($notification);
172 1
				}
173
174 1
				$this->notifiedUsers[$uid] = true;
175 1
			}
176 1
		}
177 1
	}
178
}
179