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

AbstractProvider::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 4
dl 0
loc 8
rs 10
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\NotificationProvider;
27
28
use OCA\DAV\CalDAV\Reminder\INotificationProvider;
29
use OCP\IConfig;
30
use OCP\IL10N;
31
use OCP\ILogger;
32
use OCP\IURLGenerator;
33
use OCP\L10N\IFactory as L10NFactory;
34
use OCP\IUser;
35
use Sabre\VObject\Component\VEvent;
36
use Sabre\VObject\DateTimeParser;
37
use Sabre\VObject\Property;
38
39
/**
40
 * Class AbstractProvider
41
 *
42
 * @package OCA\DAV\CalDAV\Reminder\NotificationProvider
43
 */
44
abstract class AbstractProvider implements INotificationProvider  {
45
46
	/** @var string */
47
	public const NOTIFICATION_TYPE = '';
48
49
	/** @var ILogger */
50
	protected $logger;
51
52
	/** @var L10NFactory */
53
	private $l10nFactory;
54
55
	/** @var IL10N[] */
56
	private $l10ns;
57
58
	/** @var string */
59
	private $fallbackLanguage;
60
61
	/** @var IURLGenerator */
62
	protected $urlGenerator;
63
64
	/** @var IConfig */
65
	protected $config;
66
67
	/**
68
	 * @param ILogger $logger
69
	 * @param L10NFactory $l10nFactory
70
	 * @param IConfig $config
71
	 * @param IUrlGenerator $urlGenerator
72
	 */
73
	public function __construct(ILogger $logger,
74
								L10NFactory $l10nFactory,
75
								IURLGenerator $urlGenerator,
76
								IConfig $config) {
77
		$this->logger = $logger;
78
		$this->l10nFactory = $l10nFactory;
79
		$this->urlGenerator = $urlGenerator;
80
		$this->config = $config;
81
	}
82
83
	/**
84
	 * Send notification
85
	 *
86
	 * @param VEvent $vevent
87
	 * @param string $calendarDisplayName
88
	 * @param IUser[] $users
89
	 * @return void
90
	 */
91
	abstract public function send(VEvent $vevent,
92
						   string $calendarDisplayName,
93
						   array $users=[]): void;
94
95
	/**
96
	 * @return string
97
	 */
98
	protected function getFallbackLanguage():string {
99
		if ($this->fallbackLanguage) {
100
			return $this->fallbackLanguage;
101
		}
102
103
		$fallbackLanguage = $this->l10nFactory->findLanguage();
104
		$this->fallbackLanguage = $fallbackLanguage;
105
106
		return $fallbackLanguage;
107
	}
108
109
	/**
110
	 * @param string $lang
111
	 * @return bool
112
	 */
113
	protected function hasL10NForLang(string $lang):bool {
114
		return $this->l10nFactory->languageExists('dav', $lang);
115
	}
116
117
	/**
118
	 * @param string $lang
119
	 * @return IL10N
120
	 */
121
	protected function getL10NForLang(string $lang):IL10N {
122
		if (isset($this->l10ns[$lang])) {
123
			return $this->l10ns[$lang];
124
		}
125
126
		$l10n = $this->l10nFactory->get('dav', $lang);
127
		$this->l10ns[$lang] = $l10n;
128
129
		return $l10n;
130
	}
131
132
	/**
133
	 * @param VEvent $vevent
134
	 * @return string
135
	 */
136
	private function getStatusOfEvent(VEvent $vevent):string {
137
		if ($vevent->STATUS) {
138
			return (string) $vevent->STATUS;
139
		}
140
141
		// Doesn't say so in the standard,
142
		// but we consider events without a status
143
		// to be confirmed
144
		return 'CONFIRMED';
145
	}
146
147
	/**
148
	 * @param VEvent $vevent
149
	 * @return bool
150
	 */
151
	protected function isEventTentative(VEvent $vevent):bool {
152
		return $this->getStatusOfEvent($vevent) === 'TENTATIVE';
153
	}
154
155
	/**
156
	 * @param VEvent $vevent
157
	 * @return Property\ICalendar\DateTime
158
	 */
159
	protected function getDTEndFromEvent(VEvent $vevent):Property\ICalendar\DateTime {
160
		if (isset($vevent->DTEND)) {
161
			return $vevent->DTEND;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $vevent->DTEND returns the type Sabre\VObject\Property which includes types incompatible with the type-hinted return Sabre\VObject\Property\ICalendar\DateTime.
Loading history...
162
		}
163
164
		if (isset($vevent->DURATION)) {
165
			$isFloating = $vevent->DTSTART->isFloating();
0 ignored issues
show
Bug introduced by
The method isFloating() does not exist on Sabre\VObject\Property. It seems like you code against a sub-type of Sabre\VObject\Property such as Sabre\VObject\Property\ICalendar\DateTime. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

165
			/** @scrutinizer ignore-call */ 
166
   $isFloating = $vevent->DTSTART->isFloating();
Loading history...
166
			/** @var Property\ICalendar\DateTime $end */
167
			$end = clone $vevent->DTSTART;
168
			$endDateTime = $end->getDateTime();
169
			$endDateTime = $endDateTime->add(DateTimeParser::parse($vevent->DURATION->getValue()));
170
			$end->setDateTime($endDateTime, $isFloating);
171
172
			return $end;
173
		}
174
175
		if (!$vevent->DTSTART->hasTime()) {
0 ignored issues
show
Bug introduced by
The method hasTime() does not exist on Sabre\VObject\Property. It seems like you code against a sub-type of Sabre\VObject\Property such as Sabre\VObject\Property\ICalendar\DateTime. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

175
		if (!$vevent->DTSTART->/** @scrutinizer ignore-call */ hasTime()) {
Loading history...
176
			$isFloating = $vevent->DTSTART->isFloating();
177
			/** @var Property\ICalendar\DateTime $end */
178
			$end = clone $vevent->DTSTART;
179
			$endDateTime = $end->getDateTime();
180
			$endDateTime = $endDateTime->modify('+1 day');
181
			$end->setDateTime($endDateTime, $isFloating);
182
183
			return $end;
184
		}
185
186
		return clone $vevent->DTSTART;
0 ignored issues
show
Bug Best Practice introduced by
The expression return clone $vevent->DTSTART returns the type Sabre\VObject\Property which includes types incompatible with the type-hinted return Sabre\VObject\Property\ICalendar\DateTime.
Loading history...
187
	}
188
}
189