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

SendEmails::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 0
cts 12
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 0
crap 2
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
		;
73
	}
74
75
	/**
76
	 * @param InputInterface $input
77
	 * @param OutputInterface $output
78
	 * @return int
79
	 */
80
	protected function execute(InputInterface $input, OutputInterface $output) {
81
		// We don't use time() but "time() - 1" here, so we don't run into
82
		// runtime issues later and delete emails, which were created in the
83
		// same second, but were not collected for the emails.
84
		$sendTime = time() - 1;
85
86
		$restrictBatching = $input->getArgument('restrict-batching');
87
		if ($restrictBatching === 'hourly') {
88
			$restrictEmails = UserSettings::EMAIL_SEND_HOURLY;
89
		} else if ($restrictBatching === 'daily') {
90
			$restrictEmails = UserSettings::EMAIL_SEND_DAILY;
91
		} else if ($restrictBatching === 'weekly') {
92
			$restrictEmails = UserSettings::EMAIL_SEND_WEEKLY;
93
		} else {
94
			$restrictEmails = null;
95
		}
96
97
		do {
98
			// If we are in CLI mode, we keep sending emails
99
			// until we are done.
100
			$emails_sent = $this->queueHandler->sendEmails(MailQueueHandler::CLI_EMAIL_BATCH_SIZE, $sendTime, true, $restrictEmails);
101
		} while ($emails_sent === MailQueueHandler::CLI_EMAIL_BATCH_SIZE);
102
103
		return 0;
104
	}
105
}
106