Passed
Push — master ( 9c2d70...6ef7ba )
by Roeland
10:28
created

BuildReminderIndexBackgroundJob::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 5
dl 0
loc 10
rs 10
1
<?php
2
declare(strict_types=1);
3
/**
4
 * @copyright 2019 Georg Ehrke <[email protected]>
5
 *
6
 * @author Georg Ehrke <[email protected]>
7
 *
8
 * @license GNU AGPL version 3 or any later version
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License as
12
 * published by the Free Software Foundation, either version 3 of the
13
 * License, or (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 *
23
 */
24
namespace OCA\DAV\BackgroundJob;
25
26
use OCP\BackgroundJob\QueuedJob;
27
use OCA\DAV\CalDAV\Reminder\ReminderService;
28
use OCP\AppFramework\Utility\ITimeFactory;
29
use OCP\BackgroundJob\IJobList;
30
use OCP\IDBConnection;
31
use OCP\ILogger;
32
33
/**
34
 * Class BuildReminderIndexBackgroundJob
35
 *
36
 * @package OCA\DAV\BackgroundJob
37
 */
38
class BuildReminderIndexBackgroundJob extends QueuedJob {
39
40
	/** @var IDBConnection */
41
	private $db;
42
43
	/** @var ReminderService */
44
	private $reminderService;
45
46
	/** @var ILogger */
47
	private $logger;
48
49
	/** @var IJobList */
50
	private $jobList;
51
52
	/** @var ITimeFactory */
53
	private $timeFactory;
54
55
	/**
56
	 * BuildReminderIndexBackgroundJob constructor.
57
	 *
58
	 * @param IDBConnection $db
59
	 * @param ReminderService $reminderService
60
	 * @param ILogger $logger
61
	 * @param IJobList $jobList
62
	 * @param ITimeFactory $timeFactory
63
	 */
64
	public function __construct(IDBConnection $db,
65
								ReminderService $reminderService,
66
								ILogger $logger,
67
								IJobList $jobList,
68
								ITimeFactory $timeFactory) {
69
		$this->db = $db;
70
		$this->reminderService = $reminderService;
71
		$this->logger = $logger;
72
		$this->jobList = $jobList;
73
		$this->timeFactory = $timeFactory;
74
	}
75
76
	/**
77
	 * @param $arguments
78
	 */
79
	public function run($arguments) {
80
		$offset = (int) $arguments['offset'];
81
		$stopAt = (int) $arguments['stopAt'];
82
83
		$this->logger->info('Building calendar reminder index (' . $offset .'/' . $stopAt . ')');
84
85
		$offset = $this->buildIndex($offset, $stopAt);
86
87
		if ($offset >= $stopAt) {
88
			$this->logger->info('Building calendar reminder index done');
89
		} else {
90
			$this->jobList->add(self::class, [
91
				'offset' => $offset,
92
				'stopAt' => $stopAt
93
			]);
94
			$this->logger->info('Scheduled a new BuildReminderIndexBackgroundJob with offset ' . $offset);
95
		}
96
	}
97
98
	/**
99
	 * @param int $offset
100
	 * @param int $stopAt
101
	 * @return int
102
	 */
103
	private function buildIndex(int $offset, int $stopAt):int {
104
		$startTime = $this->timeFactory->getTime();
105
106
		$query = $this->db->getQueryBuilder();
107
		$query->select('*')
108
			->from('calendarobjects')
109
			->where($query->expr()->lte('id', $query->createNamedParameter($stopAt)))
110
			->andWhere($query->expr()->gt('id', $query->createNamedParameter($offset)))
111
			->orderBy('id', 'ASC');
112
113
		$stmt = $query->execute();
114
		while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
115
			$offset = $row['id'];
116
			if (is_resource($row['calendardata'])) {
117
				$row['calendardata'] = stream_get_contents($row['calendardata']);
118
			}
119
			$row['component'] = $row['componenttype'];
120
121
			try {
122
				$this->reminderService->onTouchCalendarObject('\OCA\DAV\CalDAV\CalDavBackend::createCalendarObject', $row);
123
			} catch(\Exception $ex) {
124
				$this->logger->logException($ex);
125
			}
126
127
			if (($this->timeFactory->getTime() - $startTime) > 15) {
128
				return $offset;
129
			}
130
		}
131
132
		return $stopAt;
133
	}
134
}