Completed
Push — master ( f75e77...71ccca )
by Joas
11s
created

EmailNotification::runStep()   C

Complexity

Conditions 7
Paths 11

Size

Total Lines 39
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 7.0052

Importance

Changes 0
Metric Value
dl 0
loc 39
ccs 20
cts 21
cp 0.9524
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 22
nc 11
nop 2
crap 7.0052
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\MailQueueHandler;
28
29
/**
30
 * Class EmailNotification
31
 *
32
 * @package OCA\Activity\BackgroundJob
33
 */
34
class EmailNotification extends TimedJob {
35
36
	/** @var MailQueueHandler */
37
	protected $queueHandler;
38
39
	/** @var bool */
40
	protected $isCLI;
41
42
	/**
43
	 * @param MailQueueHandler $mailQueueHandler
44
	 * @param bool $isCLI
45
	 */
46 4
	public function __construct(MailQueueHandler $mailQueueHandler,
47
								$isCLI) {
48
		// Run all 15 Minutes
49 4
		$this->setInterval(15 * 60);
50
51 4
		$this->queueHandler = $mailQueueHandler;
52 4
		$this->isCLI = $isCLI;
53 4
	}
54
55 2
	protected function run($argument) {
56
		// We don't use time() but "time() - 1" here, so we don't run into
57
		// runtime issues later and delete emails, which were created in the
58
		// same second, but were not collected for the emails.
59 2
		$sendTime = time() - 1;
60
61 2
		if ($this->isCLI) {
62
			do {
63
				// If we are in CLI mode, we keep sending emails
64
				// until we are done.
65 1
				$emails_sent = $this->queueHandler->sendEmails(MailQueueHandler::CLI_EMAIL_BATCH_SIZE, $sendTime);
66 1
			} while ($emails_sent === MailQueueHandler::CLI_EMAIL_BATCH_SIZE);
67
		} else {
68
			// Only send 25 Emails in one go for web cron
69 1
			$this->queueHandler->sendEmails(MailQueueHandler::WEB_EMAIL_BATCH_SIZE, $sendTime);
70
		}
71 2
	}
72
}
73