Passed
Push — master ( f9ab75...626538 )
by Roeland
42:47 queued 27:37
created
apps/dav/lib/CalDAV/Reminder/Notifier.php 1 patch
Indentation   +278 added lines, -278 removed lines patch added patch discarded remove patch
@@ -47,282 +47,282 @@
 block discarded – undo
47 47
  */
48 48
 class Notifier implements INotifier {
49 49
 
50
-	/** @var IFactory */
51
-	private $l10nFactory;
52
-
53
-	/** @var IURLGenerator */
54
-	private $urlGenerator;
55
-
56
-	/** @var IL10N */
57
-	private $l10n;
58
-
59
-	/** @var ITimeFactory */
60
-	private $timeFactory;
61
-
62
-	/**
63
-	 * Notifier constructor.
64
-	 *
65
-	 * @param IFactory $factory
66
-	 * @param IURLGenerator $urlGenerator
67
-	 * @param ITimeFactory $timeFactory
68
-	 */
69
-	public function __construct(IFactory $factory,
70
-								IURLGenerator $urlGenerator,
71
-								ITimeFactory $timeFactory) {
72
-		$this->l10nFactory = $factory;
73
-		$this->urlGenerator = $urlGenerator;
74
-		$this->timeFactory = $timeFactory;
75
-	}
76
-
77
-	/**
78
-	 * Identifier of the notifier, only use [a-z0-9_]
79
-	 *
80
-	 * @return string
81
-	 * @since 17.0.0
82
-	 */
83
-	public function getID():string {
84
-		return Application::APP_ID;
85
-	}
86
-
87
-	/**
88
-	 * Human readable name describing the notifier
89
-	 *
90
-	 * @return string
91
-	 * @since 17.0.0
92
-	 */
93
-	public function getName():string {
94
-		return $this->l10nFactory->get('dav')->t('Calendar');
95
-	}
96
-
97
-	/**
98
-	 * Prepare sending the notification
99
-	 *
100
-	 * @param INotification $notification
101
-	 * @param string $languageCode The code of the language that should be used to prepare the notification
102
-	 * @return INotification
103
-	 * @throws \Exception
104
-	 */
105
-	public function prepare(INotification $notification,
106
-							string $languageCode):INotification {
107
-		if ($notification->getApp() !== Application::APP_ID) {
108
-			throw new \InvalidArgumentException('Notification not from this app');
109
-		}
110
-
111
-		// Read the language from the notification
112
-		$this->l10n = $this->l10nFactory->get('dav', $languageCode);
113
-
114
-		// Handle notifier subjects
115
-		switch ($notification->getSubject()) {
116
-			case 'calendar_reminder':
117
-				return $this->prepareReminderNotification($notification);
118
-
119
-			default:
120
-				throw new \InvalidArgumentException('Unknown subject');
121
-
122
-		}
123
-	}
124
-
125
-	/**
126
-	 * @param INotification $notification
127
-	 * @return INotification
128
-	 */
129
-	private function prepareReminderNotification(INotification $notification):INotification {
130
-		$imagePath = $this->urlGenerator->imagePath('core', 'places/calendar.svg');
131
-		$iconUrl = $this->urlGenerator->getAbsoluteURL($imagePath);
132
-		$notification->setIcon($iconUrl);
133
-
134
-		$this->prepareNotificationSubject($notification);
135
-		$this->prepareNotificationMessage($notification);
136
-
137
-		return $notification;
138
-	}
139
-
140
-	/**
141
-	 * Sets the notification subject based on the parameters set in PushProvider
142
-	 *
143
-	 * @param INotification $notification
144
-	 */
145
-	private function prepareNotificationSubject(INotification $notification): void {
146
-		$parameters = $notification->getSubjectParameters();
147
-
148
-		$startTime = \DateTime::createFromFormat(\DateTime::ATOM, $parameters['start_atom']);
149
-		$now = $this->timeFactory->getDateTime();
150
-		$title = $this->getTitleFromParameters($parameters);
151
-
152
-		$diff = $startTime->diff($now);
153
-		if ($diff === false) {
154
-			return;
155
-		}
156
-
157
-		$components = [];
158
-		if ($diff->y) {
159
-			$components[] = $this->l10n->n('%n year', '%n years', $diff->y);
160
-		}
161
-		if ($diff->m) {
162
-			$components[] = $this->l10n->n('%n month', '%n months', $diff->m);
163
-		}
164
-		if ($diff->d) {
165
-			$components[] = $this->l10n->n('%n day', '%n days', $diff->d);
166
-		}
167
-		if ($diff->h) {
168
-			$components[] = $this->l10n->n('%n hour', '%n hours', $diff->h);
169
-		}
170
-		if ($diff->i) {
171
-			$components[] = $this->l10n->n('%n minute', '%n minutes', $diff->i);
172
-		}
173
-
174
-		// Limiting to the first three components to prevent
175
-		// the string from getting too long
176
-		$firstThreeComponents = array_slice($components, 0, 2);
177
-		$diffLabel = implode(', ', $firstThreeComponents);
178
-
179
-		if ($diff->invert) {
180
-			$title = $this->l10n->t('%s (in %s)', [$title, $diffLabel]);
181
-		} else {
182
-			$title = $this->l10n->t('%s (%s ago)', [$title, $diffLabel]);
183
-		}
184
-
185
-		$notification->setParsedSubject($title);
186
-	}
187
-
188
-	/**
189
-	 * Sets the notification message based on the parameters set in PushProvider
190
-	 *
191
-	 * @param INotification $notification
192
-	 */
193
-	private function prepareNotificationMessage(INotification $notification): void {
194
-		$parameters = $notification->getMessageParameters();
195
-
196
-		$description = [
197
-			$this->l10n->t('Calendar: %s', $parameters['calendar_displayname']),
198
-			$this->l10n->t('Date: %s', $this->generateDateString($parameters)),
199
-		];
200
-		if ($parameters['description']) {
201
-			$description[] = $this->l10n->t('Description: %s', $parameters['description']);
202
-		}
203
-		if ($parameters['location']) {
204
-			$description[] = $this->l10n->t('Where: %s', $parameters['location']);
205
-		}
206
-
207
-		$message = implode("\r\n", $description);
208
-		$notification->setParsedMessage($message);
209
-	}
210
-
211
-	/**
212
-	 * @param array $parameters
213
-	 * @return string
214
-	 */
215
-	private function getTitleFromParameters(array $parameters):string {
216
-		return $parameters['title'] ?? $this->l10n->t('Untitled event');
217
-	}
218
-
219
-	/**
220
-	 * @param array $parameters
221
-	 * @return string
222
-	 * @throws \Exception
223
-	 */
224
-	private function generateDateString(array $parameters):string {
225
-		$startDateTime = DateTime::createFromFormat(\DateTime::ATOM, $parameters['start_atom']);
226
-		$endDateTime = DateTime::createFromFormat(\DateTime::ATOM, $parameters['end_atom']);
227
-
228
-		// If the event has already ended, dismiss the notification
229
-		if ($endDateTime < $this->timeFactory->getDateTime()) {
230
-			throw new AlreadyProcessedException();
231
-		}
232
-
233
-		$isAllDay = $parameters['all_day'];
234
-		$diff = $startDateTime->diff($endDateTime);
235
-
236
-		if ($isAllDay) {
237
-			// One day event
238
-			if ($diff->days === 1) {
239
-				return $this->getDateString($startDateTime);
240
-			}
241
-
242
-			return implode(' - ', [
243
-				$this->getDateString($startDateTime),
244
-				$this->getDateString($endDateTime),
245
-			]);
246
-		}
247
-
248
-		$startTimezone = $endTimezone = null;
249
-		if (!$parameters['start_is_floating']) {
250
-			$startTimezone = $parameters['start_timezone'];
251
-			$endTimezone = $parameters['end_timezone'];
252
-		}
253
-
254
-		$localeStart = implode(', ', [
255
-			$this->getWeekDayName($startDateTime),
256
-			$this->getDateTimeString($startDateTime)
257
-		]);
258
-
259
-		// always show full date with timezone if timezones are different
260
-		if ($startTimezone !== $endTimezone) {
261
-			$localeEnd = implode(', ', [
262
-				$this->getWeekDayName($endDateTime),
263
-				$this->getDateTimeString($endDateTime)
264
-			]);
265
-
266
-			return $localeStart
267
-				. ' (' . $startTimezone . ') '
268
-				. ' - '
269
-				. $localeEnd
270
-				. ' (' . $endTimezone . ')';
271
-		}
272
-
273
-		// Show only the time if the day is the same
274
-		$localeEnd = $this->isDayEqual($startDateTime, $endDateTime)
275
-			? $this->getTimeString($endDateTime)
276
-			: implode(', ', [
277
-				$this->getWeekDayName($endDateTime),
278
-				$this->getDateTimeString($endDateTime)
279
-			]);
280
-
281
-		return $localeStart
282
-			. ' - '
283
-			. $localeEnd
284
-			. ' (' . $startTimezone . ')';
285
-	}
286
-
287
-	/**
288
-	 * @param DateTime $dtStart
289
-	 * @param DateTime $dtEnd
290
-	 * @return bool
291
-	 */
292
-	private function isDayEqual(DateTime $dtStart,
293
-								DateTime $dtEnd):bool {
294
-		return $dtStart->format('Y-m-d') === $dtEnd->format('Y-m-d');
295
-	}
296
-
297
-	/**
298
-	 * @param DateTime $dt
299
-	 * @return string
300
-	 */
301
-	private function getWeekDayName(DateTime $dt):string {
302
-		return $this->l10n->l('weekdayName', $dt, ['width' => 'abbreviated']);
303
-	}
304
-
305
-	/**
306
-	 * @param DateTime $dt
307
-	 * @return string
308
-	 */
309
-	private function getDateString(DateTime $dt):string {
310
-		return $this->l10n->l('date', $dt, ['width' => 'medium']);
311
-	}
312
-
313
-	/**
314
-	 * @param DateTime $dt
315
-	 * @return string
316
-	 */
317
-	private function getDateTimeString(DateTime $dt):string {
318
-		return $this->l10n->l('datetime', $dt, ['width' => 'medium|short']);
319
-	}
320
-
321
-	/**
322
-	 * @param DateTime $dt
323
-	 * @return string
324
-	 */
325
-	private function getTimeString(DateTime $dt):string {
326
-		return $this->l10n->l('time', $dt, ['width' => 'short']);
327
-	}
50
+    /** @var IFactory */
51
+    private $l10nFactory;
52
+
53
+    /** @var IURLGenerator */
54
+    private $urlGenerator;
55
+
56
+    /** @var IL10N */
57
+    private $l10n;
58
+
59
+    /** @var ITimeFactory */
60
+    private $timeFactory;
61
+
62
+    /**
63
+     * Notifier constructor.
64
+     *
65
+     * @param IFactory $factory
66
+     * @param IURLGenerator $urlGenerator
67
+     * @param ITimeFactory $timeFactory
68
+     */
69
+    public function __construct(IFactory $factory,
70
+                                IURLGenerator $urlGenerator,
71
+                                ITimeFactory $timeFactory) {
72
+        $this->l10nFactory = $factory;
73
+        $this->urlGenerator = $urlGenerator;
74
+        $this->timeFactory = $timeFactory;
75
+    }
76
+
77
+    /**
78
+     * Identifier of the notifier, only use [a-z0-9_]
79
+     *
80
+     * @return string
81
+     * @since 17.0.0
82
+     */
83
+    public function getID():string {
84
+        return Application::APP_ID;
85
+    }
86
+
87
+    /**
88
+     * Human readable name describing the notifier
89
+     *
90
+     * @return string
91
+     * @since 17.0.0
92
+     */
93
+    public function getName():string {
94
+        return $this->l10nFactory->get('dav')->t('Calendar');
95
+    }
96
+
97
+    /**
98
+     * Prepare sending the notification
99
+     *
100
+     * @param INotification $notification
101
+     * @param string $languageCode The code of the language that should be used to prepare the notification
102
+     * @return INotification
103
+     * @throws \Exception
104
+     */
105
+    public function prepare(INotification $notification,
106
+                            string $languageCode):INotification {
107
+        if ($notification->getApp() !== Application::APP_ID) {
108
+            throw new \InvalidArgumentException('Notification not from this app');
109
+        }
110
+
111
+        // Read the language from the notification
112
+        $this->l10n = $this->l10nFactory->get('dav', $languageCode);
113
+
114
+        // Handle notifier subjects
115
+        switch ($notification->getSubject()) {
116
+            case 'calendar_reminder':
117
+                return $this->prepareReminderNotification($notification);
118
+
119
+            default:
120
+                throw new \InvalidArgumentException('Unknown subject');
121
+
122
+        }
123
+    }
124
+
125
+    /**
126
+     * @param INotification $notification
127
+     * @return INotification
128
+     */
129
+    private function prepareReminderNotification(INotification $notification):INotification {
130
+        $imagePath = $this->urlGenerator->imagePath('core', 'places/calendar.svg');
131
+        $iconUrl = $this->urlGenerator->getAbsoluteURL($imagePath);
132
+        $notification->setIcon($iconUrl);
133
+
134
+        $this->prepareNotificationSubject($notification);
135
+        $this->prepareNotificationMessage($notification);
136
+
137
+        return $notification;
138
+    }
139
+
140
+    /**
141
+     * Sets the notification subject based on the parameters set in PushProvider
142
+     *
143
+     * @param INotification $notification
144
+     */
145
+    private function prepareNotificationSubject(INotification $notification): void {
146
+        $parameters = $notification->getSubjectParameters();
147
+
148
+        $startTime = \DateTime::createFromFormat(\DateTime::ATOM, $parameters['start_atom']);
149
+        $now = $this->timeFactory->getDateTime();
150
+        $title = $this->getTitleFromParameters($parameters);
151
+
152
+        $diff = $startTime->diff($now);
153
+        if ($diff === false) {
154
+            return;
155
+        }
156
+
157
+        $components = [];
158
+        if ($diff->y) {
159
+            $components[] = $this->l10n->n('%n year', '%n years', $diff->y);
160
+        }
161
+        if ($diff->m) {
162
+            $components[] = $this->l10n->n('%n month', '%n months', $diff->m);
163
+        }
164
+        if ($diff->d) {
165
+            $components[] = $this->l10n->n('%n day', '%n days', $diff->d);
166
+        }
167
+        if ($diff->h) {
168
+            $components[] = $this->l10n->n('%n hour', '%n hours', $diff->h);
169
+        }
170
+        if ($diff->i) {
171
+            $components[] = $this->l10n->n('%n minute', '%n minutes', $diff->i);
172
+        }
173
+
174
+        // Limiting to the first three components to prevent
175
+        // the string from getting too long
176
+        $firstThreeComponents = array_slice($components, 0, 2);
177
+        $diffLabel = implode(', ', $firstThreeComponents);
178
+
179
+        if ($diff->invert) {
180
+            $title = $this->l10n->t('%s (in %s)', [$title, $diffLabel]);
181
+        } else {
182
+            $title = $this->l10n->t('%s (%s ago)', [$title, $diffLabel]);
183
+        }
184
+
185
+        $notification->setParsedSubject($title);
186
+    }
187
+
188
+    /**
189
+     * Sets the notification message based on the parameters set in PushProvider
190
+     *
191
+     * @param INotification $notification
192
+     */
193
+    private function prepareNotificationMessage(INotification $notification): void {
194
+        $parameters = $notification->getMessageParameters();
195
+
196
+        $description = [
197
+            $this->l10n->t('Calendar: %s', $parameters['calendar_displayname']),
198
+            $this->l10n->t('Date: %s', $this->generateDateString($parameters)),
199
+        ];
200
+        if ($parameters['description']) {
201
+            $description[] = $this->l10n->t('Description: %s', $parameters['description']);
202
+        }
203
+        if ($parameters['location']) {
204
+            $description[] = $this->l10n->t('Where: %s', $parameters['location']);
205
+        }
206
+
207
+        $message = implode("\r\n", $description);
208
+        $notification->setParsedMessage($message);
209
+    }
210
+
211
+    /**
212
+     * @param array $parameters
213
+     * @return string
214
+     */
215
+    private function getTitleFromParameters(array $parameters):string {
216
+        return $parameters['title'] ?? $this->l10n->t('Untitled event');
217
+    }
218
+
219
+    /**
220
+     * @param array $parameters
221
+     * @return string
222
+     * @throws \Exception
223
+     */
224
+    private function generateDateString(array $parameters):string {
225
+        $startDateTime = DateTime::createFromFormat(\DateTime::ATOM, $parameters['start_atom']);
226
+        $endDateTime = DateTime::createFromFormat(\DateTime::ATOM, $parameters['end_atom']);
227
+
228
+        // If the event has already ended, dismiss the notification
229
+        if ($endDateTime < $this->timeFactory->getDateTime()) {
230
+            throw new AlreadyProcessedException();
231
+        }
232
+
233
+        $isAllDay = $parameters['all_day'];
234
+        $diff = $startDateTime->diff($endDateTime);
235
+
236
+        if ($isAllDay) {
237
+            // One day event
238
+            if ($diff->days === 1) {
239
+                return $this->getDateString($startDateTime);
240
+            }
241
+
242
+            return implode(' - ', [
243
+                $this->getDateString($startDateTime),
244
+                $this->getDateString($endDateTime),
245
+            ]);
246
+        }
247
+
248
+        $startTimezone = $endTimezone = null;
249
+        if (!$parameters['start_is_floating']) {
250
+            $startTimezone = $parameters['start_timezone'];
251
+            $endTimezone = $parameters['end_timezone'];
252
+        }
253
+
254
+        $localeStart = implode(', ', [
255
+            $this->getWeekDayName($startDateTime),
256
+            $this->getDateTimeString($startDateTime)
257
+        ]);
258
+
259
+        // always show full date with timezone if timezones are different
260
+        if ($startTimezone !== $endTimezone) {
261
+            $localeEnd = implode(', ', [
262
+                $this->getWeekDayName($endDateTime),
263
+                $this->getDateTimeString($endDateTime)
264
+            ]);
265
+
266
+            return $localeStart
267
+                . ' (' . $startTimezone . ') '
268
+                . ' - '
269
+                . $localeEnd
270
+                . ' (' . $endTimezone . ')';
271
+        }
272
+
273
+        // Show only the time if the day is the same
274
+        $localeEnd = $this->isDayEqual($startDateTime, $endDateTime)
275
+            ? $this->getTimeString($endDateTime)
276
+            : implode(', ', [
277
+                $this->getWeekDayName($endDateTime),
278
+                $this->getDateTimeString($endDateTime)
279
+            ]);
280
+
281
+        return $localeStart
282
+            . ' - '
283
+            . $localeEnd
284
+            . ' (' . $startTimezone . ')';
285
+    }
286
+
287
+    /**
288
+     * @param DateTime $dtStart
289
+     * @param DateTime $dtEnd
290
+     * @return bool
291
+     */
292
+    private function isDayEqual(DateTime $dtStart,
293
+                                DateTime $dtEnd):bool {
294
+        return $dtStart->format('Y-m-d') === $dtEnd->format('Y-m-d');
295
+    }
296
+
297
+    /**
298
+     * @param DateTime $dt
299
+     * @return string
300
+     */
301
+    private function getWeekDayName(DateTime $dt):string {
302
+        return $this->l10n->l('weekdayName', $dt, ['width' => 'abbreviated']);
303
+    }
304
+
305
+    /**
306
+     * @param DateTime $dt
307
+     * @return string
308
+     */
309
+    private function getDateString(DateTime $dt):string {
310
+        return $this->l10n->l('date', $dt, ['width' => 'medium']);
311
+    }
312
+
313
+    /**
314
+     * @param DateTime $dt
315
+     * @return string
316
+     */
317
+    private function getDateTimeString(DateTime $dt):string {
318
+        return $this->l10n->l('datetime', $dt, ['width' => 'medium|short']);
319
+    }
320
+
321
+    /**
322
+     * @param DateTime $dt
323
+     * @return string
324
+     */
325
+    private function getTimeString(DateTime $dt):string {
326
+        return $this->l10n->l('time', $dt, ['width' => 'short']);
327
+    }
328 328
 }
Please login to merge, or discard this patch.