Completed
Pull Request — master (#505)
by Joas
03:22
created

Consumer::receive()   F

Complexity

Conditions 14
Paths 512

Size

Total Lines 30
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 14

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 30
ccs 19
cts 19
cp 1
rs 3.0216
cc 14
eloc 16
nc 512
nop 1
crap 14

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @author Joas Schilling <[email protected]>
4
 * @author Thomas Müller <[email protected]>
5
 *
6
 * @copyright Copyright (c) 2016, ownCloud, Inc.
7
 * @license AGPL-3.0
8
 *
9
 * This code is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License, version 3,
11
 * as published by the Free Software Foundation.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 * GNU Affero General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public License, version 3,
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
20
 *
21
 */
22
23
namespace OCA\Activity;
24
25
use OCP\Activity\IConsumer;
26
use OCP\Activity\IEvent;
27
use OCP\Activity\IExtension;
28
use OCP\Activity\IManager;
29
use OCP\AppFramework\IAppContainer;
30
use OCP\L10N\IFactory;
31
32
class Consumer implements IConsumer {
33
	/**
34
	 * Registers the consumer to the Activity Manager
35
	 *
36
	 * @param IManager $am
37
	 * @param IAppContainer $container
38
	 */
39
	public static function register(IManager $am, IAppContainer $container) {
40 2
		$am->registerConsumer(function() use ($container) {
41 1
			return $container->query('Consumer');
42 2
		});
43 2
	}
44
45
	/** @var Data */
46
	protected $data;
47
48
	/** @var UserSettings */
49
	protected $userSettings;
50
51
	/** @var IFactory */
52
	protected $l10nFactory;
53
54
	/**
55
	 * Constructor
56
	 *
57
	 * @param Data $data
58
	 * @param UserSettings $userSettings
59
	 * @param IFactory $l10nFactory
60
	 */
61 23
	public function __construct(Data $data, UserSettings $userSettings, IFactory $l10nFactory) {
62 23
		$this->data = $data;
63 23
		$this->userSettings = $userSettings;
64 23
		$this->l10nFactory = $l10nFactory;
65 23
	}
66
67
	/**
68
	 * Send an event to the notifications of a user
69
	 *
70
	 * @param IEvent $event
71
	 * @return null
72
	 */
73 20
	public function receive(IEvent $event) {
74 20
		$selfAction = $event->getAffectedUser() === $event->getAuthor();
75 20
		$streamSetting = $this->userSettings->getUserSetting($event->getAffectedUser(), 'stream', $event->getType());
76 20
		$emailSetting = $this->userSettings->getUserSetting($event->getAffectedUser(), 'email', $event->getType());
77 20
		$emailSetting = ($emailSetting) ? $this->userSettings->getUserSetting($event->getAffectedUser(), 'setting', 'batchtime') : false;
78
79 20
		$types = $this->data->getNotificationTypes($this->l10nFactory->get('activity'));
80 20
		$typeData = $types[$event->getType()];
81
82
		// User is not the author or wants to see their own actions
83 20
		$createStream = !$selfAction || $this->userSettings->getUserSetting($event->getAffectedUser(), 'setting', 'self');
84
		// User can not control the setting
85 20
		$createStream = $createStream || is_array($typeData) && isset($typeData['methods']) && !in_array(IExtension::METHOD_STREAM, $typeData['methods']);
86
87
		// Add activity to stream
88 20
		if ($streamSetting && $createStream) {
89 14
			$this->data->send($event);
90 14
		}
91
92
		// User is not the author or wants to see their own actions
93 20
		$createEmail = !$selfAction || $this->userSettings->getUserSetting($event->getAffectedUser(), 'setting', 'selfemail');
94
		// User can not control the setting
95 20
		$createEmail = $createEmail || is_array($typeData) && isset($typeData['methods']) && !in_array(IExtension::METHOD_MAIL, $typeData['methods']);
96
97
		// Add activity to mail queue
98 20
		if ($emailSetting && $createEmail) {
99 14
			$latestSend = $event->getTimestamp() + $emailSetting;
100 14
			$this->data->storeMail($event, $latestSend);
101 14
		}
102 20
	}
103
}
104