Passed
Push — master ( 9e596d...9f70c6 )
by Christoph
15:44 queued 10s
created

RetentionService::cleanUp()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 16
nc 4
nop 0
dl 0
loc 23
rs 9.7333
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * @copyright 2021 Christoph Wurst <[email protected]>
7
 *
8
 * @author 2021 Christoph Wurst <[email protected]>
9
 *
10
 * @license GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
 */
25
26
namespace OCA\DAV\CalDAV;
27
28
use OCA\DAV\AppInfo\Application;
29
use OCP\AppFramework\Utility\ITimeFactory;
30
use OCP\IConfig;
31
use function max;
32
33
class RetentionService {
34
	public const RETENTION_CONFIG_KEY = 'calendarRetentionObligation';
35
	private const DEFAULT_RETENTION_SECONDS = 30 * 24 * 60 * 60;
36
37
	/** @var IConfig */
38
	private $config;
39
40
	/** @var ITimeFactory */
41
	private $time;
42
43
	/** @var CalDavBackend */
44
	private $calDavBackend;
45
46
	public function __construct(IConfig $config,
47
								ITimeFactory $time,
48
								CalDavBackend $calDavBackend) {
49
		$this->config = $config;
50
		$this->time = $time;
51
		$this->calDavBackend = $calDavBackend;
52
	}
53
54
	public function cleanUp(): void {
55
		$retentionTime = max(
56
			(int) $this->config->getAppValue(
57
				Application::APP_ID,
58
				self::RETENTION_CONFIG_KEY,
59
				(string) self::DEFAULT_RETENTION_SECONDS
60
			),
61
			0 // Just making sure we don't delete things in the future when a negative number is passed
62
		);
63
		$now = $this->time->getTime();
64
65
		$calendars = $this->calDavBackend->getDeletedCalendars($now - $retentionTime);
66
		foreach ($calendars as $calendar) {
67
			$this->calDavBackend->deleteCalendar($calendar['id'], true);
68
		}
69
70
		$objects = $this->calDavBackend->getDeletedCalendarObjects($now - $retentionTime);
71
		foreach ($objects as $object) {
72
			$this->calDavBackend->deleteCalendarObject(
73
				$object['calendarid'],
74
				$object['uri'],
75
				$object['calendartype'],
76
				true
77
			);
78
		}
79
	}
80
}
81