Completed
Push — stable9 ( be630e...27be8a )
by Morris
12s
created

EmailNotification   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 65.85%

Importance

Changes 0
Metric Value
wmc 15
c 0
b 0
f 0
lcom 1
cbo 2
dl 0
loc 107
ccs 27
cts 41
cp 0.6585
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 16 5
A fixDIForJobs() 0 8 1
A run() 0 17 3
B runStep() 0 33 6
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Joas Schilling <[email protected]>
6
 * @author Morris Jobke <[email protected]>
7
 *
8
 * @license AGPL-3.0
9
 *
10
 * This code is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License, version 3,
12
 * as published by the Free Software Foundation.
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, version 3,
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
21
 *
22
 */
23
24
namespace OCA\Activity\BackgroundJob;
25
26
use OC\BackgroundJob\TimedJob;
27
use OCA\Activity\AppInfo\Application;
28
use OCA\Activity\MailQueueHandler;
29
use OCP\IConfig;
30
use OCP\ILogger;
31
32
/**
33
 * Class EmailNotification
34
 *
35
 * @package OCA\Activity\BackgroundJob
36
 */
37
class EmailNotification extends TimedJob {
38
	const CLI_EMAIL_BATCH_SIZE = 500;
39
	const WEB_EMAIL_BATCH_SIZE = 25;
40
41
	/** @var MailQueueHandler */
42
	protected $mqHandler;
43
44
	/** @var IConfig */
45
	protected $config;
46
47
	/** @var ILogger */
48
	protected $logger;
49
50
	/** @var bool */
51
	protected $isCLI;
52
53
	/**
54
	 * @param MailQueueHandler $mailQueueHandler
55
	 * @param IConfig $config
56
	 * @param ILogger $logger
57
	 * @param bool|null $isCLI
58
	 */
59 4
	public function __construct(MailQueueHandler $mailQueueHandler = null,
60
								IConfig $config = null,
61
								ILogger $logger = null,
62
								$isCLI = null) {
63
		// Run all 15 Minutes
64 4
		$this->setInterval(15 * 60);
65
66 4
		if ($mailQueueHandler === null || $config === null || $logger === null || $isCLI === null) {
67 1
			$this->fixDIForJobs();
68
		} else {
69 3
			$this->mqHandler = $mailQueueHandler;
70 3
			$this->config = $config;
71 3
			$this->logger = $logger;
72 3
			$this->isCLI = $isCLI;
73
		}
74 4
	}
75
76 1
	protected function fixDIForJobs() {
77 1
		$application = new Application();
78
79 1
		$this->mqHandler = $application->getContainer()->query('MailQueueHandler');
80 1
		$this->config = \OC::$server->getConfig();
81 1
		$this->logger = \OC::$server->getLogger();
82 1
		$this->isCLI = \OC::$CLI;
83 1
	}
84
85 3
	protected function run($argument) {
86
		// We don't use time() but "time() - 1" here, so we don't run into
87
		// runtime issues later and delete emails, which were created in the
88
		// same second, but were not collected for the emails.
89 3
		$sendTime = time() - 1;
90
91 3
		if ($this->isCLI) {
92
			do {
93
				// If we are in CLI mode, we keep sending emails
94
				// until we are done.
95 2
				$emails_sent = $this->runStep(self::CLI_EMAIL_BATCH_SIZE, $sendTime);
96 2
			} while ($emails_sent === self::CLI_EMAIL_BATCH_SIZE);
97
		} else {
98
			// Only send 25 Emails in one go for web cron
99 1
			$this->runStep(self::WEB_EMAIL_BATCH_SIZE, $sendTime);
100
		}
101 3
	}
102
103
	/**
104
	 * Send an email to {$limit} users
105
	 *
106
	 * @param int $limit Number of users we want to send an email to
107
	 * @param int $sendTime The latest send time
108
	 * @return int Number of users we sent an email to
109
	 */
110 3
	protected function runStep($limit, $sendTime) {
111
		// Get all users which should receive an email
112 3
		$affectedUsers = $this->mqHandler->getAffectedUsers($limit, $sendTime);
113 3
		if (empty($affectedUsers)) {
114
			// No users found to notify, mission abort
115 3
			return 0;
116
		}
117
118
		$userLanguages = $this->config->getUserValueForUsers('core', 'lang', $affectedUsers);
119
		$userTimezones = $this->config->getUserValueForUsers('core', 'timezone', $affectedUsers);
120
		$userEmails = $this->config->getUserValueForUsers('settings', 'email', $affectedUsers);
121
122
		// Send Email
123
		$default_lang = $this->config->getSystemValue('default_language', 'en');
124
		$defaultTimeZone = date_default_timezone_get();
125
		foreach ($affectedUsers as $user) {
126
			if (empty($userEmails[$user])) {
127
				// The user did not setup an email address
128
				// So we will not send an email :(
129
				$this->logger->debug("Couldn't send notification email to user '" . $user . "' (email address isn't set for that user)", ['app' => 'activity']);
130
				continue;
131
			}
132
133
			$language = (!empty($userLanguages[$user])) ? $userLanguages[$user] : $default_lang;
134
			$timezone = (!empty($userTimezones[$user])) ? $userTimezones[$user] : $defaultTimeZone;
135
			$this->mqHandler->sendEmailToUser($user, $userEmails[$user], $language, $timezone, $sendTime);
136
		}
137
138
		// Delete all entries we dealt with
139
		$this->mqHandler->deleteSentItems($affectedUsers, $sendTime);
140
141
		return sizeof($affectedUsers);
142
	}
143
}
144