Passed
Push — master ( fce6df...8e01ff )
by Georg
14:04 queued 11s
created

PredefinedStatusService::getIconForId()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 13
c 1
b 0
f 0
nc 6
nop 1
dl 0
loc 19
rs 9.2222
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @copyright Copyright (c) 2020, Georg Ehrke
7
 *
8
 * @author Georg Ehrke <[email protected]>
9
 *
10
 * @license AGPL-3.0
11
 *
12
 * This code is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License, version 3,
14
 * as published by the Free Software Foundation.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License, version 3,
22
 * along with this program. If not, see <http://www.gnu.org/licenses/>
23
 *
24
 */
25
26
namespace OCA\UserStatus\Service;
27
28
use OCP\IL10N;
29
30
/**
31
 * Class DefaultStatusService
32
 *
33
 * We are offering a set of default statuses, so we can
34
 * translate them into different languages.
35
 *
36
 * @package OCA\UserStatus\Service
37
 */
38
class PredefinedStatusService {
39
	private const MEETING = 'meeting';
40
	private const COMMUTING = 'commuting';
41
	private const SICK_LEAVE = 'sick-leave';
42
	private const VACATIONING = 'vacationing';
43
	private const REMOTE_WORK = 'remote-work';
44
45
	/** @var IL10N */
46
	private $l10n;
47
48
	/**
49
	 * DefaultStatusService constructor.
50
	 *
51
	 * @param IL10N $l10n
52
	 */
53
	public function __construct(IL10N $l10n) {
54
		$this->l10n = $l10n;
55
	}
56
57
	/**
58
	 * @return array
59
	 */
60
	public function getDefaultStatuses(): array {
61
		return [
62
			[
63
				'id' => self::MEETING,
64
				'icon' => '📅',
65
				'message' => $this->getTranslatedStatusForId(self::MEETING),
66
				'clearAt' => [
67
					'type' => 'period',
68
					'time' => 3600,
69
				],
70
			],
71
			[
72
				'id' => self::COMMUTING,
73
				'icon' => '🚌',
74
				'message' => $this->getTranslatedStatusForId(self::COMMUTING),
75
				'clearAt' => [
76
					'type' => 'period',
77
					'time' => 1800,
78
				],
79
			],
80
			[
81
				'id' => self::REMOTE_WORK,
82
				'icon' => '🏡',
83
				'message' => $this->getTranslatedStatusForId(self::REMOTE_WORK),
84
				'clearAt' => [
85
					'type' => 'end-of',
86
					'time' => 'day',
87
				],
88
			],
89
			[
90
				'id' => self::SICK_LEAVE,
91
				'icon' => '🤒',
92
				'message' => $this->getTranslatedStatusForId(self::SICK_LEAVE),
93
				'clearAt' => [
94
					'type' => 'end-of',
95
					'time' => 'day',
96
				],
97
			],
98
			[
99
				'id' => self::VACATIONING,
100
				'icon' => '🌴',
101
				'message' => $this->getTranslatedStatusForId(self::VACATIONING),
102
				'clearAt' => null,
103
			],
104
		];
105
	}
106
107
	/**
108
	 * @param string $id
109
	 * @return array|null
110
	 */
111
	public function getDefaultStatusById(string $id): ?array {
112
		foreach ($this->getDefaultStatuses() as $status) {
113
			if ($status['id'] === $id) {
114
				return $status;
115
			}
116
		}
117
118
		return null;
119
	}
120
121
	/**
122
	 * @param string $id
123
	 * @return string|null
124
	 */
125
	public function getIconForId(string $id): ?string {
126
		switch ($id) {
127
			case self::MEETING:
128
				return '📅';
129
130
			case self::COMMUTING:
131
				return '🚌';
132
133
			case self::SICK_LEAVE:
134
				return '🤒';
135
136
			case self::VACATIONING:
137
				return '🌴';
138
139
			case self::REMOTE_WORK:
140
				return '🏡';
141
142
			default:
143
				return null;
144
		}
145
	}
146
147
	/**
148
	 * @param string $lang
149
	 * @param string $id
150
	 * @return string|null
151
	 */
152
	public function getTranslatedStatusForId(string $id): ?string {
153
		switch ($id) {
154
			case self::MEETING:
155
				return $this->l10n->t('In a meeting');
156
157
			case self::COMMUTING:
158
				return $this->l10n->t('Commuting');
159
160
			case self::SICK_LEAVE:
161
				return $this->l10n->t('Out sick');
162
163
			case self::VACATIONING:
164
				return $this->l10n->t('Vacationing');
165
166
			case self::REMOTE_WORK:
167
				return $this->l10n->t('Working remotely');
168
169
			default:
170
				return null;
171
		}
172
	}
173
174
	/**
175
	 * @param string $id
176
	 * @return bool
177
	 */
178
	public function isValidId(string $id): bool {
179
		return \in_array($id, [
180
			self::MEETING,
181
			self::COMMUTING,
182
			self::SICK_LEAVE,
183
			self::VACATIONING,
184
			self::REMOTE_WORK,
185
		], true);
186
	}
187
}
188