Completed
Push — master ( e1740c...d98dea )
by Morris
14:21
created

SyncBirthdayCalendar   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 85
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 3

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A configure() 0 8 1
B execute() 0 39 5
A verifyEnabled() 0 7 2
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,
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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)) {
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');
83
			if ($isEnabled !== 'yes') {
84
				$this->config->setUserValue($user, 'dav', 'generateBirthdayCalendar', 'yes');
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);
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