Passed
Push — master ( 26f537...188bc0 )
by Christoph
14:27 queued 10s
created

RetentionService::getDuration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 8
rs 10
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 getDuration(): int {
55
		return 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
	}
64
65
	public function cleanUp(): void {
66
		$retentionTime = $this->getDuration();
67
		$now = $this->time->getTime();
68
69
		$calendars = $this->calDavBackend->getDeletedCalendars($now - $retentionTime);
70
		foreach ($calendars as $calendar) {
71
			$this->calDavBackend->deleteCalendar($calendar['id'], true);
72
		}
73
74
		$objects = $this->calDavBackend->getDeletedCalendarObjects($now - $retentionTime);
75
		foreach ($objects as $object) {
76
			$this->calDavBackend->deleteCalendarObject(
77
				$object['calendarid'],
78
				$object['uri'],
79
				$object['calendartype'],
80
				true
81
			);
82
		}
83
	}
84
}
85