Passed
Push — master ( 5cdc85...37718d )
by Morris
38:53 queued 21:57
created

SyncBirthdayCalendar::execute()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 38
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 24
nc 4
nop 2
dl 0
loc 38
rs 9.2248
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Joas Schilling <[email protected]>
6
 * @author Thomas Müller <[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
namespace OCA\DAV\Command;
24
25
use OCA\DAV\CalDAV\BirthdayService;
26
use OCP\IConfig;
27
use OCP\IUser;
28
use OCP\IUserManager;
29
use Symfony\Component\Console\Command\Command;
30
use Symfony\Component\Console\Helper\ProgressBar;
31
use Symfony\Component\Console\Input\InputArgument;
32
use Symfony\Component\Console\Input\InputInterface;
33
use Symfony\Component\Console\Output\OutputInterface;
34
35
class SyncBirthdayCalendar extends Command {
36
37
	/** @var BirthdayService */
38
	private $birthdayService;
39
40
	/** @var IConfig */
41
	private $config;
42
43
	/** @var IUserManager */
44
	private $userManager;
45
46
	/**
47
	 * @param IUserManager $userManager
48
	 * @param IConfig $config
49
	 * @param BirthdayService $birthdayService
50
	 */
51
	function __construct(IUserManager $userManager, IConfig $config,
52
						 BirthdayService $birthdayService) {
53
		parent::__construct();
54
		$this->birthdayService = $birthdayService;
55
		$this->config = $config;
56
		$this->userManager = $userManager;
57
	}
58
59
	protected function configure() {
60
		$this
61
			->setName('dav:sync-birthday-calendar')
62
			->setDescription('Synchronizes the birthday calendar')
63
			->addArgument('user',
64
				InputArgument::OPTIONAL,
65
				'User for whom the birthday calendar will be synchronized');
66
	}
67
68
	/**
69
	 * @param InputInterface $input
70
	 * @param OutputInterface $output
71
	 */
72
	protected function execute(InputInterface $input, OutputInterface $output) {
73
		$this->verifyEnabled();
74
75
		$user = $input->getArgument('user');
76
		if (!is_null($user)) {
77
			if (!$this->userManager->userExists($user)) {
0 ignored issues
show
Bug introduced by
It seems like $user can also be of type string[]; however, parameter $uid of OCP\IUserManager::userExists() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

77
			if (!$this->userManager->userExists(/** @scrutinizer ignore-type */ $user)) {
Loading history...
78
				throw new \InvalidArgumentException("User <$user> in unknown.");
79
			}
80
81
			// re-enable the birthday calendar in case it's called directly with a user name
82
			$isEnabled = $this->config->getUserValue($user, 'dav', 'generateBirthdayCalendar', 'yes');
0 ignored issues
show
Bug introduced by
It seems like $user can also be of type string[]; however, parameter $userId of OCP\IConfig::getUserValue() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

82
			$isEnabled = $this->config->getUserValue(/** @scrutinizer ignore-type */ $user, 'dav', 'generateBirthdayCalendar', 'yes');
Loading history...
83
			if ($isEnabled !== 'yes') {
84
				$this->config->setUserValue($user, 'dav', 'generateBirthdayCalendar', 'yes');
0 ignored issues
show
Bug introduced by
It seems like $user can also be of type string[]; however, parameter $userId of OCP\IConfig::setUserValue() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

84
				$this->config->setUserValue(/** @scrutinizer ignore-type */ $user, 'dav', 'generateBirthdayCalendar', 'yes');
Loading history...
85
				$output->writeln("Re-enabling birthday calendar for $user");
86
			}
87
88
			$output->writeln("Start birthday calendar sync for $user");
89
			$this->birthdayService->syncUser($user);
0 ignored issues
show
Bug introduced by
It seems like $user can also be of type string[]; however, parameter $user of OCA\DAV\CalDAV\BirthdayService::syncUser() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

89
			$this->birthdayService->syncUser(/** @scrutinizer ignore-type */ $user);
Loading history...
90
			return;
91
		}
92
		$output->writeln("Start birthday calendar sync for all users ...");
93
		$p = new ProgressBar($output);
94
		$p->start();
95
		$this->userManager->callForAllUsers(function($user) use ($p)  {
96
			$p->advance();
97
98
			$userId = $user->getUID();
99
			$isEnabled = $this->config->getUserValue($userId, 'dav', 'generateBirthdayCalendar', 'yes');
100
			if ($isEnabled !== 'yes') {
101
				return;
102
			}
103
104
			/** @var IUser $user */
105
			$this->birthdayService->syncUser($user->getUID());
106
		});
107
108
		$p->finish();
109
		$output->writeln('');
110
	}
111
112
	protected function verifyEnabled () {
113
		$isEnabled = $this->config->getAppValue('dav', 'generateBirthdayCalendar', 'yes');
114
115
		if ($isEnabled !== 'yes') {
116
			throw new \InvalidArgumentException('Birthday calendars are disabled');
117
		}
118
	}
119
}
120