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

Backend::insertReminder()   A

Complexity

Conditions 5
Paths 1

Size

Total Lines 31
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 17
c 1
b 0
f 0
nc 1
nop 12
dl 0
loc 31
rs 9.3888

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
declare(strict_types=1);
3
/**
4
 * @copyright Copyright (c) 2019, Thomas Citharel
5
 * @copyright Copyright (c) 2019, Georg Ehrke
6
 *
7
 * @author Thomas Citharel <[email protected]>
8
 * @author Georg Ehrke <[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\Reminder;
27
28
use OCP\IDBConnection;
29
use OCP\AppFramework\Utility\ITimeFactory;
30
31
/**
32
 * Class Backend
33
 *
34
 * @package OCA\DAV\CalDAV\Reminder
35
 */
36
class Backend {
37
38
	/** @var IDBConnection */
39
	protected $db;
40
41
	/** @var ITimeFactory */
42
	private $timeFactory;
43
44
	/**
45
	 * Backend constructor.
46
	 *
47
	 * @param IDBConnection $db
48
	 * @param ITimeFactory $timeFactory
49
	 */
50
	public function __construct(IDBConnection $db,
51
								ITimeFactory $timeFactory) {
52
		$this->db = $db;
53
		$this->timeFactory = $timeFactory;
54
	}
55
56
	/**
57
	 * Get all reminders with a notification date before now
58
	 *
59
	 * @return array
60
	 * @throws \Exception
61
	 */
62
	public function getRemindersToProcess():array {
63
		$query = $this->db->getQueryBuilder();
64
		$query->select(['cr.*', 'co.calendardata', 'c.displayname', 'c.principaluri'])
65
			->from('calendar_reminders', 'cr')
66
			->where($query->expr()->lte('cr.notification_date', $query->createNamedParameter($this->timeFactory->getTime())))
67
			->leftJoin('cr', 'calendarobjects', 'co', $query->expr()->eq('cr.object_id', 'co.id'))
68
			->leftJoin('cr', 'calendars', 'c', $query->expr()->eq('cr.calendar_id', 'c.id'));
69
		$stmt = $query->execute();
70
71
		return array_map(
72
			[$this, 'fixRowTyping'],
73
			$stmt->fetchAll()
74
		);
75
	}
76
77
	/**
78
	 * Get all scheduled reminders for an event
79
	 *
80
	 * @param int $objectId
81
	 * @return array
82
	 */
83
	public function getAllScheduledRemindersForEvent(int $objectId):array {
84
		$query = $this->db->getQueryBuilder();
85
		$query->select('*')
86
			->from('calendar_reminders')
87
			->where($query->expr()->eq('object_id', $query->createNamedParameter($objectId)));
88
		$stmt = $query->execute();
89
90
		return array_map(
91
			[$this, 'fixRowTyping'],
92
			$stmt->fetchAll()
93
		);
94
	}
95
96
	/**
97
	 * Insert a new reminder into the database
98
	 *
99
	 * @param int $calendarId
100
	 * @param int $objectId
101
	 * @param string $uid
102
	 * @param bool $isRecurring
103
	 * @param int $recurrenceId
104
	 * @param bool $isRecurrenceException
105
	 * @param string $eventHash
106
	 * @param string $alarmHash
107
	 * @param string $type
108
	 * @param bool $isRelative
109
	 * @param int $notificationDate
110
	 * @param bool $isRepeatBased
111
	 * @return int The insert id
112
	 */
113
	public function insertReminder(int $calendarId,
114
								   int $objectId,
115
								   string $uid,
116
								   bool $isRecurring,
117
								   int $recurrenceId,
118
								   bool $isRecurrenceException,
119
								   string $eventHash,
120
								   string $alarmHash,
121
								   string $type,
122
								   bool $isRelative,
123
								   int $notificationDate,
124
								   bool $isRepeatBased):int {
125
		$query = $this->db->getQueryBuilder();
126
		$query->insert('calendar_reminders')
127
			->values([
128
				'calendar_id' => $query->createNamedParameter($calendarId),
129
				'object_id' => $query->createNamedParameter($objectId),
130
				'uid' => $query->createNamedParameter($uid),
131
				'is_recurring' => $query->createNamedParameter($isRecurring ? 1 : 0),
132
				'recurrence_id' => $query->createNamedParameter($recurrenceId),
133
				'is_recurrence_exception' => $query->createNamedParameter($isRecurrenceException ? 1 : 0),
134
				'event_hash' => $query->createNamedParameter($eventHash),
135
				'alarm_hash' => $query->createNamedParameter($alarmHash),
136
				'type' => $query->createNamedParameter($type),
137
				'is_relative' => $query->createNamedParameter($isRelative ? 1 : 0),
138
				'notification_date' => $query->createNamedParameter($notificationDate),
139
				'is_repeat_based' => $query->createNamedParameter($isRepeatBased ? 1 : 0),
140
			])
141
			->execute();
142
143
		return $query->getLastInsertId();
144
	}
145
146
	/**
147
	 * Sets a new notificationDate on an existing reminder
148
	 *
149
	 * @param int $reminderId
150
	 * @param int $newNotificationDate
151
	 */
152
	public function updateReminder(int $reminderId,
153
								   int $newNotificationDate):void {
154
		$query = $this->db->getQueryBuilder();
155
		$query->update('calendar_reminders')
156
			->set('notification_date', $query->createNamedParameter($newNotificationDate))
157
			->where($query->expr()->eq('id', $query->createNamedParameter($reminderId)))
158
			->execute();
159
	}
160
161
	/**
162
	 * Remove a reminder by it's id
163
	 *
164
	 * @param integer $reminderId
165
	 * @return void
166
	 */
167
	public function removeReminder(int $reminderId):void {
168
		$query = $this->db->getQueryBuilder();
169
170
		$query->delete('calendar_reminders')
171
			->where($query->expr()->eq('id', $query->createNamedParameter($reminderId)))
172
			->execute();
173
	}
174
175
	/**
176
	 * Cleans reminders in database
177
	 *
178
	 * @param int $objectId
179
	 */
180
	public function cleanRemindersForEvent(int $objectId):void {
181
		$query = $this->db->getQueryBuilder();
182
183
		$query->delete('calendar_reminders')
184
			->where($query->expr()->eq('object_id', $query->createNamedParameter($objectId)))
185
			->execute();
186
	}
187
188
	/**
189
	 * Remove all reminders for a calendar
190
	 *
191
	 * @param int $calendarId
192
	 * @return void
193
	 */
194
	public function cleanRemindersForCalendar(int $calendarId):void {
195
		$query = $this->db->getQueryBuilder();
196
197
		$query->delete('calendar_reminders')
198
			->where($query->expr()->eq('calendar_id', $query->createNamedParameter($calendarId)))
199
			->execute();
200
	}
201
202
	/**
203
	 * @param array $row
204
	 * @return array
205
	 */
206
	private function fixRowTyping(array $row): array {
207
		$row['id'] = (int) $row['id'];
208
		$row['calendar_id'] = (int) $row['calendar_id'];
209
		$row['object_id'] = (int) $row['object_id'];
210
		$row['is_recurring'] = (bool) $row['is_recurring'];
211
		$row['recurrence_id'] = (int) $row['recurrence_id'];
212
		$row['is_recurrence_exception'] = (bool) $row['is_recurrence_exception'];
213
		$row['is_relative'] = (bool) $row['is_relative'];
214
		$row['notification_date'] = (int) $row['notification_date'];
215
		$row['is_repeat_based'] = (bool) $row['is_repeat_based'];
216
217
		return $row;
218
	}
219
}
220