RefreshWebcalJob   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 43
dl 0
loc 109
rs 10
c 0
b 0
f 0
wmc 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getIntervalFromDateInterval() 0 7 1
A execute() 0 29 4
A fixSubscriptionRowTyping() 0 12 3
A __construct() 0 6 1
A run() 0 2 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @copyright 2018 Georg Ehrke <[email protected]>
7
 *
8
 * @author Christoph Wurst <[email protected]>
9
 * @author Georg Ehrke <[email protected]>
10
 * @author Roeland Jago Douma <[email protected]>
11
 * @author Thomas Citharel <[email protected]>
12
 *
13
 * @license GNU AGPL version 3 or any later version
14
 *
15
 * This program is free software: you can redistribute it and/or modify
16
 * it under the terms of the GNU Affero General Public License as
17
 * published by the Free Software Foundation, either version 3 of the
18
 * License, or (at your option) any later version.
19
 *
20
 * This program is distributed in the hope that it will be useful,
21
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23
 * GNU Affero General Public License for more details.
24
 *
25
 * You should have received a copy of the GNU Affero General Public License
26
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27
 *
28
 */
29
namespace OCA\DAV\BackgroundJob;
30
31
use DateInterval;
32
use OCA\DAV\CalDAV\WebcalCaching\RefreshWebcalService;
33
use OCP\AppFramework\Utility\ITimeFactory;
34
use OCP\BackgroundJob\IJobList;
35
use OCP\BackgroundJob\Job;
36
use OCP\IConfig;
37
use OCP\ILogger;
38
use Sabre\VObject\DateTimeParser;
39
use Sabre\VObject\InvalidDataException;
40
41
class RefreshWebcalJob extends Job {
42
43
	/**
44
	 * @var RefreshWebcalService
45
	 */
46
	private $refreshWebcalService;
47
48
	/**
49
	 * @var IConfig
50
	 */
51
	private $config;
52
53
	/** @var ILogger */
54
	private $logger;
55
56
	/** @var ITimeFactory */
57
	private $timeFactory;
58
59
	/**
60
	 * RefreshWebcalJob constructor.
61
	 *
62
	 * @param RefreshWebcalService $refreshWebcalService
63
	 * @param IConfig $config
64
	 * @param ILogger $logger
65
	 * @param ITimeFactory $timeFactory
66
	 */
67
	public function __construct(RefreshWebcalService $refreshWebcalService, IConfig $config, ILogger $logger, ITimeFactory $timeFactory) {
68
		parent::__construct($timeFactory);
69
		$this->refreshWebcalService = $refreshWebcalService;
70
		$this->config = $config;
71
		$this->logger = $logger;
72
		$this->timeFactory = $timeFactory;
73
	}
74
75
	/**
76
	 * this function is called at most every hour
77
	 *
78
	 * @inheritdoc
79
	 */
80
	public function execute(IJobList $jobList, ILogger $logger = null) {
81
		$subscription = $this->refreshWebcalService->getSubscription($this->argument['principaluri'], $this->argument['uri']);
82
		if (!$subscription) {
83
			return;
84
		}
85
86
		$this->fixSubscriptionRowTyping($subscription);
87
88
		// if no refresh rate was configured, just refresh once a week
89
		$defaultRefreshRate = $this->config->getAppValue('dav', 'calendarSubscriptionRefreshRate', 'P1W');
90
		$refreshRate = $subscription[RefreshWebcalService::REFRESH_RATE] ?? $defaultRefreshRate;
91
92
		$subscriptionId = $subscription['id'];
93
94
		try {
95
			/** @var DateInterval $dateInterval */
96
			$dateInterval = DateTimeParser::parseDuration($refreshRate);
97
		} catch (InvalidDataException $ex) {
98
			$this->logger->logException($ex);
99
			$this->logger->warning("Subscription $subscriptionId could not be refreshed, refreshrate in database is invalid");
100
			return;
101
		}
102
103
		$interval = $this->getIntervalFromDateInterval($dateInterval);
104
		if (($this->timeFactory->getTime() - $this->lastRun) <= $interval) {
105
			return;
106
		}
107
108
		parent::execute($jobList, $logger);
109
	}
110
111
	/**
112
	 * @param array $argument
113
	 */
114
	protected function run($argument) {
115
		$this->refreshWebcalService->refreshSubscription($argument['principaluri'], $argument['uri']);
116
	}
117
118
	/**
119
	 * get total number of seconds from DateInterval object
120
	 *
121
	 * @param DateInterval $interval
122
	 * @return int
123
	 */
124
	private function getIntervalFromDateInterval(DateInterval $interval):int {
125
		return $interval->s
126
			+ ($interval->i * 60)
127
			+ ($interval->h * 60 * 60)
128
			+ ($interval->d * 60 * 60 * 24)
129
			+ ($interval->m * 60 * 60 * 24 * 30)
130
			+ ($interval->y * 60 * 60 * 24 * 365);
131
	}
132
133
	/**
134
	 * Fixes types of rows
135
	 *
136
	 * @param array $row
137
	 */
138
	private function fixSubscriptionRowTyping(array &$row):void {
139
		$forceInt = [
140
			'id',
141
			'lastmodified',
142
			RefreshWebcalService::STRIP_ALARMS,
143
			RefreshWebcalService::STRIP_ATTACHMENTS,
144
			RefreshWebcalService::STRIP_TODOS,
145
		];
146
147
		foreach ($forceInt as $column) {
148
			if (isset($row[$column])) {
149
				$row[$column] = (int) $row[$column];
150
			}
151
		}
152
	}
153
}
154