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

BirthdayCalendarController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 7
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright 2017, Georg Ehrke <[email protected]>
4
 *
5
 * @author Georg Ehrke <[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\DAV\Controller;
25
26
use OCA\DAV\BackgroundJob\GenerateBirthdayCalendarBackgroundJob;
27
use OCA\DAV\CalDAV\CalDavBackend;
28
use OCP\AppFramework\Controller;
29
use OCP\AppFramework\Http\JSONResponse;
30
use OCP\AppFramework\Http\Response;
31
use OCP\BackgroundJob\IJobList;
32
use OCP\IConfig;
33
use OCP\IDBConnection;
34
use OCP\IRequest;
35
use OCP\IUser;
36
use OCP\IUserManager;
37
38
class BirthdayCalendarController extends Controller {
39
40
	/**
41
	 * @var IDBConnection
42
	 */
43
	protected $db;
44
45
	/**
46
	 * @var IConfig
47
	 */
48
	protected $config;
49
50
	/**
51
	 * @var IUserManager
52
	 */
53
	protected $userManager;
54
55
	/**
56
	 * @var CalDavBackend
57
	 */
58
	protected $caldavBackend;
59
60
	/**
61
	 * @var IJobList
62
	 */
63
	protected $jobList;
64
65
	/**
66
	 * BirthdayCalendar constructor.
67
	 *
68
	 * @param string $appName
69
	 * @param IRequest $request
70
	 * @param IDBConnection $db
71
	 * @param IConfig $config
72
	 * @param IJobList $jobList
73
	 * @param IUserManager $userManager
74
	 * @param CalDavBackend $calDavBackend
75
	 */
76
	public function __construct($appName, IRequest $request,
77
								IDBConnection $db, IConfig $config,
78
								IJobList $jobList,
79
								IUserManager $userManager,
80
								CalDavBackend $calDavBackend){
81
		parent::__construct($appName, $request);
82
		$this->db = $db;
83
		$this->config = $config;
84
		$this->userManager = $userManager;
85
		$this->jobList = $jobList;
86
		$this->caldavBackend = $calDavBackend;
87
	}
88
89
	/**
90
	 * @return Response
91
	 */
92
	public function enable() {
93
		$this->config->setAppValue($this->appName, 'generateBirthdayCalendar', 'yes');
94
95
		// add background job for each user
96
		$this->userManager->callForAllUsers(function(IUser $user) {
97
			$this->jobList->add(GenerateBirthdayCalendarBackgroundJob::class, [
98
				'userId' => $user->getUID(),
99
			]);
100
		});
101
102
		return new JSONResponse([]);
103
	}
104
105
	/**
106
	 * @return Response
107
	 */
108
	public function disable() {
109
		$this->config->setAppValue($this->appName, 'generateBirthdayCalendar', 'no');
110
111
		$this->jobList->remove(GenerateBirthdayCalendarBackgroundJob::class);
112
		$this->caldavBackend->deleteAllBirthdayCalendars();
113
114
		return new JSONResponse([]);
115
	}
116
}
117