Completed
Push — master ( 8cad4a...3cc1c8 )
by Morris
02:32
created

SendEmails::completeArgumentValues()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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