Completed
Pull Request — master (#184)
by Joas
04:09
created

SendEmails::execute()   C

Complexity

Conditions 7
Paths 10

Size

Total Lines 31
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 31
ccs 0
cts 24
cp 0
rs 6.7272
cc 7
eloc 21
nc 10
nop 2
crap 56
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 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\Activity\Command;
25
26
use OCA\Activity\MailQueueHandler;
27
use OCA\Activity\UserSettings;
28
use OCP\IConfig;
29
use OCP\ILogger;
30
use Symfony\Component\Console\Command\Command;
31
use Symfony\Component\Console\Input\InputArgument;
32
use Symfony\Component\Console\Input\InputInterface;
33
use Symfony\Component\Console\Input\InputOption;
34
use Symfony\Component\Console\Output\OutputInterface;
35
36
class SendEmails extends Command {
37
38
	/** @var MailQueueHandler */
39
	protected $queueHandler;
40
41
	/** @var IConfig */
42
	protected $config;
43
44
	/** @var ILogger */
45
	protected $logger;
46
47
	/**
48
	 * @param MailQueueHandler $queueHandler
49
	 * @param IConfig $config
50
	 * @param ILogger $logger
51
	 */
52
	public function __construct(MailQueueHandler $queueHandler,
53
								IConfig $config,
54
								ILogger $logger) {
55
		parent::__construct();
56
57
		$this->queueHandler = $queueHandler;
58
		$this->config = $config;
59
		$this->logger = $logger;
60
	}
61
62
	protected function configure() {
63
		$this
64
			->setName('activity:send-mails')
65
			->setDescription('Sends the activity notification mails')
66
			->addArgument(
67
				'restrict-batching',
68
				InputArgument::OPTIONAL,
69
				'Only sends the emails for users which have configured the mails: "hourly", "daily" or "weekly"',
70
				'all'
71
			)
72
			->addOption(
73
				'limit',
74
				'l',
75
				InputOption::VALUE_REQUIRED,
76
				'Only sends this amount of emails to give the email server some time to relax',
77
				'unlimited'
78
			)
79
		;
80
	}
81
82
	/**
83
	 * @param InputInterface $input
84
	 * @param OutputInterface $output
85
	 * @return int
86
	 */
87
	protected function execute(InputInterface $input, OutputInterface $output) {
88
		// We don't use time() but "time() - 1" here, so we don't run into
89
		// runtime issues later and delete emails, which were created in the
90
		// same second, but were not collected for the emails.
91
		$sendTime = time() - 1;
92
93
		$restrictBatching = $input->getArgument('restrict-batching');
94
		if ($restrictBatching === 'hourly') {
95
			$restrictEmails = UserSettings::EMAIL_SEND_HOURLY;
96
		} else if ($restrictBatching === 'daily') {
97
			$restrictEmails = UserSettings::EMAIL_SEND_DAILY;
98
		} else if ($restrictBatching === 'weekly') {
99
			$restrictEmails = UserSettings::EMAIL_SEND_WEEKLY;
100
		} else if ($restrictBatching === 'asap') {
101
			$restrictEmails = UserSettings::EMAIL_SEND_ASAP;
102
		} else {
103
			$restrictEmails = null;
104
		}
105
106
		$limit = $input->getOption('limit');
107
108
		if ($limit === 'unlimited') {
109
			do {
110
				$emails_sent = $this->queueHandler->sendEmails(MailQueueHandler::CLI_EMAIL_BATCH_SIZE, $sendTime, true, $restrictEmails);
111
			} while ($emails_sent === MailQueueHandler::CLI_EMAIL_BATCH_SIZE);
112
		} else {
113
			$this->queueHandler->sendEmails($limit, $sendTime, true, $restrictEmails);
114
		}
115
116
		return 0;
117
	}
118
}
119