BackgroundJob   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 152
Duplicated Lines 14.47 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 97.06%

Importance

Changes 0
Metric Value
wmc 23
lcom 1
cbo 2
dl 22
loc 152
ccs 66
cts 68
cp 0.9706
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 14 14 1
A run() 0 14 3
A createPublicity() 0 26 2
A createPublicityEveryone() 4 17 6
B createPublicityGroups() 4 32 11

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
declare(strict_types=1);
3
/**
4
 * @copyright Copyright (c) 2016, Joas Schilling <[email protected]>
5
 *
6
 * @author Joas Schilling <[email protected]>
7
 *
8
 * @license GNU AGPL version 3 or any later version
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License as
12
 * published by the Free Software Foundation, either version 3 of the
13
 * License, or (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 *
23
 */
24
25
namespace OCA\AnnouncementCenter;
26
27
use OC\BackgroundJob\QueuedJob;
28
use OCA\AnnouncementCenter\Model\Announcement;
29
use OCA\AnnouncementCenter\Model\AnnouncementDoesNotExistException;
30
use OCA\Guests\UserBackend;
31
use OCP\Activity\IEvent;
32
use OCP\Activity\IManager as IActivityManager;
33
use OCP\IConfig;
34
use OCP\IGroup;
35
use OCP\IGroupManager;
36
use OCP\IUser;
37
use OCP\IUserManager;
38
use OCP\Notification\IManager as INotificationManager;
39
use OCP\Notification\INotification;
40
41
class BackgroundJob extends QueuedJob {
42
	/** @var IConfig */
43
	protected $config;
44
	/** @var INotificationManager */
45
	protected $notificationManager;
46
47
	/** @var IUserManager */
48
	private $userManager;
49
50
	/** @var IGroupManager */
51
	private $groupManager;
52
53
	/** @var Manager */
54
	private $manager;
55
56
	/** @var IActivityManager */
57
	private $activityManager;
58
59
	/** @var array */
60
	protected $notifiedUsers = [];
61
62
	/** @var bool */
63
	protected $enabledForGuestsUsers;
64
65 11 View Code Duplication
	public function __construct(
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...
66
		IConfig $config,
67
		IUserManager $userManager,
68
		IGroupManager $groupManager,
69
		IActivityManager $activityManager,
70
		INotificationManager $notificationManager,
71
		Manager $manager) {
72 11
		$this->config = $config;
73 11
		$this->userManager = $userManager;
74 11
		$this->groupManager = $groupManager;
75 11
		$this->activityManager = $activityManager;
76 11
		$this->notificationManager = $notificationManager;
77 11
		$this->manager = $manager;
78 11
	}
79
80
	/**
81
	 * @param array $arguments
82
	 */
83 4
	public function run($arguments) {
84
		try {
85 4
			$announcement = $this->manager->getAnnouncement($arguments['id'], true);
86 1
		} catch (AnnouncementDoesNotExistException $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 3
		$guestsWhiteList = $this->config->getAppValue('guests', 'whitelist', null);
93 3
		$this->enabledForGuestsUsers = $guestsWhiteList !== null && strpos($guestsWhiteList, 'announcementcenter') !== false;
94
95 3
		$this->createPublicity($announcement, $arguments);
96 3
	}
97
98
	/**
99
	 * @param Announcement $announcement
100
	 * @param array $publicity
101
	 */
102 2
	protected function createPublicity(Announcement $announcement, array $publicity): void {
103 2
		$event = $this->activityManager->generateEvent();
104 2
		$event->setApp('announcementcenter')
105 2
			->setType('announcementcenter')
106 2
			->setAuthor($announcement->getUser())
107 2
			->setTimestamp($announcement->getTime())
108 2
			->setSubject('announcementsubject', ['author' => $announcement->getUser(), 'announcement' => $announcement->getId()])
109 2
			->setMessage('announcementmessage')
110 2
			->setObject('announcement', $announcement->getId());
111
112 2
		$dateTime = new \DateTime();
113 2
		$dateTime->setTimestamp($announcement->getTime());
114
115 2
		$notification = $this->notificationManager->createNotification();
116 2
		$notification->setApp('announcementcenter')
117 2
			->setDateTime($dateTime)
118 2
			->setObject('announcement', (string)$announcement->getId())
119 2
			->setSubject('announced', [$announcement->getUser()]);
120
121 2
		$groups = $this->manager->getGroups($announcement);
122 2
		if (\in_array('everyone', $groups, true)) {
123 1
			$this->createPublicityEveryone($announcement->getUser(), $event, $notification, $publicity);
124
		} else {
125 1
			$this->createPublicityGroups($announcement->getUser(), $event, $notification, $groups, $publicity);
126
		}
127 2
	}
128
129
	/**
130
	 * @param string $authorId
131
	 * @param IEvent $event
132
	 * @param INotification $notification
133
	 * @param array $publicity
134
	 */
135 2
	protected function createPublicityEveryone(string $authorId, IEvent $event, INotification $notification, array $publicity) {
136
		$this->userManager->callForSeenUsers(function(IUser $user) use ($authorId, $event, $notification, $publicity) {
137 2
			if (!$this->enabledForGuestsUsers && $user->getBackend() instanceof UserBackend) {
0 ignored issues
show
Bug introduced by
The class OCA\Guests\UserBackend 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...
138
				return;
139
			}
140
141 2
			if (!empty($publicity['activities'])) {
142 1
				$event->setAffectedUser($user->getUID());
143 1
				$this->activityManager->publish($event);
144
			}
145
146 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...
147 1
				$notification->setUser($user->getUID());
148 1
				$this->notificationManager->notify($notification);
149
			}
150 2
		});
151 2
	}
152
153
	/**
154
	 * @param string $authorId
155
	 * @param IEvent $event
156
	 * @param INotification $notification
157
	 * @param string[] $groups
158
	 * @param array $publicity
159
	 */
160 2
	protected function createPublicityGroups($authorId, IEvent $event, INotification $notification, array $groups, array $publicity) {
161 2
		foreach ($groups as $gid) {
162 2
			$group = $this->groupManager->get($gid);
163 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...
164 2
				continue;
165
			}
166
167 2
			$users = $group->getUsers();
168 2
			foreach ($users as $user) {
169 2
				if (!$this->enabledForGuestsUsers && $user->getBackend() instanceof UserBackend) {
0 ignored issues
show
Bug introduced by
The class OCA\Guests\UserBackend 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...
170
					continue;
171
				}
172
173 2
				$uid = $user->getUID();
174 2
				if (isset($this->notifiedUsers[$uid]) || $user->getLastLogin() === 0) {
175 2
					continue;
176
				}
177
178 2
				if (!empty($publicity['activities'])) {
179 1
					$event->setAffectedUser($uid);
180 1
					$this->activityManager->publish($event);
181
				}
182
183 2 View Code Duplication
				if ($authorId !== $uid && !empty($publicity['notifications'])) {
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...
184 1
					$notification->setUser($uid);
185 1
					$this->notificationManager->notify($notification);
186
				}
187
188 2
				$this->notifiedUsers[$uid] = true;
189
			}
190
		}
191 2
	}
192
}
193