Completed
Push — master ( 11144a...f5a495 )
by Maxence
12:24 queued 10s
created

WidgetSetup::jsonSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
5
/**
6
 * Nextcloud - Dashboard App
7
 *
8
 * This file is licensed under the Affero General Public License version 3 or
9
 * later. See the COPYING file.
10
 *
11
 * @author Maxence Lange <[email protected]>
12
 * @copyright 2018, Maxence Lange <[email protected]>
13
 * @license GNU AGPL version 3 or any later version
14
 *
15
 * This program is free software: you can redistribute it and/or modify
16
 * it under the terms of the GNU Affero General Public License as
17
 * published by the Free Software Foundation, either version 3 of the
18
 * License, or (at your option) any later version.
19
 *
20
 * This program is distributed in the hope that it will be useful,
21
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 * GNU Affero General Public License for more details.
24
 *
25
 * You should have received a copy of the GNU Affero General Public License
26
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27
 *
28
 */
29
30
namespace OCP\Dashboard\Model;
31
32
33
use JsonSerializable;
34
35
36
/**
37
 * Interface WidgetSetup
38
 *
39
 * A widget must create an WidgetSetup object and returns it in the
40
 * IDashboardWidget::getWidgetSetup method.
41
 *
42
 * @see IDashboardWidget::getWidgetSetup
43
 *
44
 * @since 15.0.0
45
 *
46
 * @package OCP\Dashboard\Model
47
 */
48
final class WidgetSetup implements JsonSerializable {
49
50
51
	const SIZE_TYPE_MIN = 'min';
52
	const SIZE_TYPE_MAX = 'max';
53
	const SIZE_TYPE_DEFAULT = 'default';
54
55
56
	/** @var array */
57
	private $sizes = [];
58
59
	/** @var array */
60
	private $menus = [];
61
62
	/** @var array */
63
	private $jobs = [];
64
65
	/** @var string */
66
	private $push = '';
67
68
	/** @var array */
69
	private $settings = [];
70
71
72
	/**
73
	 * Get the defined size for a specific type (min, max, default)
74
	 * Returns an array:
75
	 * [
76
	 *   'width' => width,
77
	 *   'height' => height
78
	 * ]
79
	 *
80
	 *
81
	 * @since 15.0.0
82
	 *
83
	 * @param string $type
84
	 *
85
	 * @return array
86
	 */
87
	public function getSize(string $type): array {
88
		if (array_key_exists($type, $this->sizes)) {
89
			return $this->sizes[$type];
90
		}
91
92
		return [];
93
	}
94
95
	/**
96
	 * Returns all sizes defined for the widget.
97
	 *
98
	 * @since 15.0.0
99
	 *
100
	 * @return array
101
	 */
102
	public function getSizes(): array {
103
		return $this->sizes;
104
	}
105
106
	/**
107
	 * Add a new size to the setup.
108
	 *
109
	 * @since 15.0.0
110
	 *
111
	 * @param string $type
112
	 * @param int $width
113
	 * @param int $height
114
	 *
115
	 * @return WidgetSetup
116
	 */
117
	public function addSize(string $type, int $width, int $height): WidgetSetup {
118
		$this->sizes[$type] = [
119
			'width' => $width,
120
			'height' => $height
121
		];
122
123
		return $this;
124
	}
125
126
	/**
127
	 * Returns menu entries.
128
	 *
129
	 * @since 15.0.0
130
	 *
131
	 * @return array
132
	 */
133
	public function getMenuEntries(): array {
134
		return $this->menus;
135
	}
136
137
	/**
138
	 * Add a menu entry to the widget.
139
	 * $function is the Javascript function to be called when clicking the
140
	 *           menu entry.
141
	 * $icon is the css class of the icon.
142
	 * $text is the display name of the menu entry.
143
	 *
144
	 * @since 15.0.0
145
	 *
146
	 * @param string $function
147
	 * @param string $icon
148
	 * @param string $text
149
	 *
150
	 * @return WidgetSetup
151
	 */
152
	public function addMenuEntry(string $function, string $icon, string $text): WidgetSetup {
153
		$this->menus[] = [
154
			'function' => $function,
155
			'icon' => $icon,
156
			'text' => $text
157
		];
158
159
		return $this;
160
	}
161
162
163
	/**
164
	 * Add a delayed job to the widget.
165
	 *
166
	 * $function is the Javascript function to be called.
167
	 * $delay is the time in seconds between each call.
168
	 *
169
	 * @since 15.0.0
170
	 *
171
	 * @param string $function
172
	 * @param int $delay
173
	 *
174
	 * @return WidgetSetup
175
	 */
176
	public function addDelayedJob(string $function, int $delay): WidgetSetup {
177
		$this->jobs[] = [
178
			'function' => $function,
179
			'delay' => $delay
180
		];
181
182
		return $this;
183
	}
184
185
	/**
186
	 * Get delayed jobs.
187
	 *
188
	 * @since 15.0.0
189
	 *
190
	 * @return array
191
	 */
192
	public function getDelayedJobs(): array {
193
		return $this->jobs;
194
	}
195
196
197
	/**
198
	 * Get the push function, called when an event is send to the front-end
199
	 *
200
	 * @since 15.0.0
201
	 *
202
	 * @return string
203
	 */
204
	public function getPush(): string {
205
		return $this->push;
206
	}
207
208
	/**
209
	 * Set the Javascript function to be called when an event is pushed to the
210
	 * frontend.
211
	 *
212
	 * @since 15.0.0
213
	 *
214
	 * @param string $function
215
	 *
216
	 * @return WidgetSetup
217
	 */
218
	public function setPush(string $function): WidgetSetup {
219
		$this->push = $function;
220
221
		return $this;
222
	}
223
224
225
	/**
226
	 * Returns the default settings for a widget.
227
	 *
228
	 * @since 15.0.0
229
	 *
230
	 * @return array
231
	 */
232
	public function getDefaultSettings(): array {
233
		return $this->settings;
234
	}
235
236
	/**
237
	 * Set the default settings for a widget.
238
	 * This method is used by the Dashboard app, using the settings created
239
	 * using WidgetSetting
240
	 *
241
	 * @see WidgetSetting
242
	 *
243
	 * @since 15.0.0
244
	 *
245
	 * @param array $settings
246
	 *
247
	 * @return WidgetSetup
248
	 */
249
	public function setDefaultSettings(array $settings): WidgetSetup {
250
		$this->settings = $settings;
251
252
		return $this;
253
	}
254
255
256
	/**
257
	 * @since 15.0.0
258
	 *
259
	 * @return array
260
	 */
261
	public function jsonSerialize() {
262
		return [
263
			'size' => $this->getSizes(),
264
			'menu' => $this->getMenuEntries(),
265
			'jobs' => $this->getDelayedJobs(),
266
			'push' => $this->getPush(),
267
			'settings' => $this->getDefaultSettings()
268
		];
269
	}
270
}
271
272