Completed
Pull Request — master (#8)
by Joas
02:16
created

BackgroundJob   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 70.49%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 11
c 4
b 0
f 0
lcom 1
cbo 1
dl 0
loc 125
ccs 43
cts 61
cp 0.7049
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 1
A run() 0 12 2
B createPublicity() 0 57 8
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\IManager;
26
use OCP\IGroup;
27
use OCP\IGroupManager;
28
use OCP\IURLGenerator;
29
use OCP\IUser;
30
use OCP\IUserManager;
31
use OCP\Notification\IManager as INotificationManager;
32
33
class BackgroundJob extends QueuedJob {
34
	/** @var INotificationManager */
35
	protected $notificationManager;
36
37
	/** @var IUserManager */
38
	private $userManager;
39
40
	/** @var IGroupManager */
41
	private $groupManager;
42
43
	/** @var IURLGenerator */
44
	private $urlGenerator;
45
46
	/** @var Manager */
47
	private $manager;
48
49
	/** @var IManager */
50
	private $activityManager;
51
52
	/** @var array */
53
	protected $notifiedUsers = [];
54
55
	/**
56
	 * @param IUserManager $userManager
57
	 * @param IGroupManager $groupManager
58
	 * @param IManager $activityManager
59
	 * @param INotificationManager $notificationManager
60
	 * @param IURLGenerator $urlGenerator
61
	 * @param Manager $manager
62
	 */
63 4
	public function __construct(
64
		IUserManager $userManager,
65
		IGroupManager $groupManager,
66
		IManager $activityManager,
67
		INotificationManager $notificationManager,
68
		IURLGenerator $urlGenerator,
69
		Manager $manager) {
70 4
		$this->userManager = $userManager;
71 4
		$this->groupManager = $groupManager;
72 4
		$this->activityManager = $activityManager;
73 4
		$this->notificationManager = $notificationManager;
74 4
		$this->urlGenerator = $urlGenerator;
75 4
		$this->manager = $manager;
76 4
	}
77
78
	/**
79
	 * @param array $argument
80
	 */
81 3
	public function run($argument) {
82
		try {
83 3
			$announcement = $this->manager->getAnnouncement($argument['id'], false);
84 3
		} catch (\InvalidArgumentException $e) {
85
			// Announcement was deleted in the meantime, so no need to announce it anymore
86
			// So we die silently
87 1
			return;
88
		}
89
90 2
		$groups = $this->manager->getGroups($argument['id']);
91 2
		$this->createPublicity($announcement['id'], $announcement['author'], $announcement['time'], $groups);
92 2
	}
93
94
	/**
95
	 * @param int $id
96
	 * @param string $authorId
97
	 * @param int $timeStamp
98
	 * @param string[] $groups
99
	 */
100 1
	protected function createPublicity($id, $authorId, $timeStamp, array $groups) {
101 1
		$event = $this->activityManager->generateEvent();
102 1
		$event->setApp('announcementcenter')
103 1
			->setType('announcementcenter')
104 1
			->setAuthor($authorId)
105 1
			->setTimestamp($timeStamp)
106 1
			->setSubject('announcementsubject#' . $id, [$authorId])
107 1
			->setMessage('announcementmessage#' . $id, [$authorId])
108 1
			->setObject('announcement', $id);
109
110 1
		$dateTime = new \DateTime();
111 1
		$dateTime->setTimestamp($timeStamp);
112
113 1
		$notification = $this->notificationManager->createNotification();
114 1
		$notification->setApp('announcementcenter')
115 1
			->setDateTime($dateTime)
116 1
			->setObject('announcement', $id)
117 1
			->setSubject('announced', [$authorId])
118 1
			->setLink($this->urlGenerator->linkToRoute('announcementcenter.page.index'));
119
120 1
		if (in_array('everyone', $groups)) {
121 1
			$this->userManager->callForAllUsers(function(IUser $user) use ($authorId, $event, $notification) {
122 1
				$event->setAffectedUser($user->getUID());
123 1
				$this->activityManager->publish($event);
124
125 1
				if ($authorId !== $user->getUID()) {
126 1
					$notification->setUser($user->getUID());
127 1
					$this->notificationManager->notify($notification);
128 1
				}
129 1
			});
130 1
		} else {
131
			foreach ($groups as $gid) {
132
				$group = $this->groupManager->get($gid);
133
				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...
134
					continue;
135
				}
136
137
				$users = $group->getUsers();
138
				foreach ($users as $user) {
139
					$uid = $user->getUID();
140
					if (isset($this->notifiedUsers[$uid])) {
141
						continue;
142
					}
143
144
					$event->setAffectedUser($uid);
145
					$this->activityManager->publish($event);
146
147
					if ($authorId !== $uid) {
148
						$notification->setUser($uid);
149
						$this->notificationManager->notify($notification);
150
					}
151
152
					$this->notifiedUsers[$uid] = true;
153
				}
154
			}
155
		}
156 1
	}
157
}
158