Completed
Push — master ( 586935...49b771 )
by Joas
14:13 queued 03:42
created

BackgroundJob   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 150
Duplicated Lines 5.33 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 6
Bugs 0 Features 1
Metric Value
wmc 17
c 6
b 0
f 1
lcom 1
cbo 1
dl 8
loc 150
ccs 69
cts 69
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 1
A run() 0 11 2
B createPublicity() 0 26 2
A createPublicityEveryone() 4 13 4
C createPublicityGroups() 4 28 8

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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->linkToRoute('announcementcenter.page.index'));
123
124 2
		if (in_array('everyone', $groups)) {
125 1
			$this->createPublicityEveryone($authorId, $event, $notification, $publicity);
126 1
		} else {
127 1
			$this->createPublicityGroups($authorId, $event, $notification, $groups, $publicity);
128
		}
129 2
	}
130
131
	/**
132
	 * @param string $authorId
133
	 * @param IEvent $event
134
	 * @param INotification $notification
135
	 * @param array $publicity
136
	 */
137
	protected function createPublicityEveryone($authorId, IEvent $event, INotification $notification, array $publicity) {
138 2
		$this->userManager->callForAllUsers(function(IUser $user) use ($authorId, $event, $notification, $publicity) {
139 2
			if (!empty($publicity['activities'])) {
140 1
				$event->setAffectedUser($user->getUID());
141 1
				$this->activityManager->publish($event);
142 1
			}
143
144 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...
145 1
				$notification->setUser($user->getUID());
146 1
				$this->notificationManager->notify($notification);
147 1
			}
148 2
		});
149 2
	}
150
151
	/**
152
	 * @param string $authorId
153
	 * @param IEvent $event
154
	 * @param INotification $notification
155
	 * @param string[] $groups
156
	 * @param array $publicity
157
	 */
158 2
	protected function createPublicityGroups($authorId, IEvent $event, INotification $notification, array $groups, array $publicity) {
159 2
		foreach ($groups as $gid) {
160 2
			$group = $this->groupManager->get($gid);
161 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...
162 2
				continue;
163
			}
164
165 2
			$users = $group->getUsers();
166 2
			foreach ($users as $user) {
167 2
				$uid = $user->getUID();
168 2
				if (isset($this->notifiedUsers[$uid])) {
169 2
					continue;
170
				}
171
172 2
				if (!empty($publicity['activities'])) {
173 1
					$event->setAffectedUser($uid);
174 1
					$this->activityManager->publish($event);
175 1
				}
176
177 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...
178 1
					$notification->setUser($uid);
179 1
					$this->notificationManager->notify($notification);
180 1
				}
181
182 2
				$this->notifiedUsers[$uid] = true;
183 2
			}
184 2
		}
185 2
	}
186
}
187