-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathServer.php
More file actions
466 lines (404 loc) · 11.3 KB
/
Copy pathServer.php
File metadata and controls
466 lines (404 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
<?php
namespace BNETDocs\Libraries\Server;
use \BNETDocs\Libraries\Core\DateTimeImmutable;
use \BNETDocs\Libraries\Db\MariaDb;
use \BNETDocs\Libraries\Server\Type as ServerType;
use \BNETDocs\Libraries\Tag\Tag;
use \BNETDocs\Libraries\Tag\Types as TagTypes;
use \BNETDocs\Libraries\User\User;
use \DateTimeInterface;
use \DateTimeZone;
use \OutOfBoundsException;
use \StdClass;
class Server implements \BNETDocs\Interfaces\DatabaseObject, \JsonSerializable
{
public const MAX_ADDRESS = 0xFF; // varchar(255)
public const MAX_ID = 0xFFFFFFFFFFFFFFFF; // bigint(20) unsigned
public const MAX_LABEL = 0xFF; // varchar(255)
public const MAX_PORT = 0xFFFF; // smallint(5) unsigned
public const MAX_STATUS_BITMASK = 0xFF; // tinyint(3) unsigned
public const MAX_TYPE_ID = ServerType::MAX_ID;
public const MAX_USER_ID = User::MAX_ID;
public const STATUS_ONLINE = 0x01;
public const STATUS_DISABLED = 0x02;
protected string $address;
protected DateTimeInterface $created_datetime;
protected ?int $id;
protected ?string $label;
protected int $port;
protected int $status_bitmask;
protected int $type_id;
protected ?DateTimeInterface $updated_datetime;
protected ?int $user_id;
public function __construct(StdClass|int|null $value)
{
if ($value instanceof StdClass)
{
$this->allocateObject($value);
}
else
{
$this->setId($value);
if (!$this->allocate()) throw new \BNETDocs\Exceptions\ServerNotFoundException($this);
}
}
public function allocate(): bool
{
$this->setAddress('', true);
$this->setCreatedDateTime('now');
$this->setLabel(null);
$this->setPort(0);
$this->setStatusBitmask(self::STATUS_DISABLED);
$this->setTypeId(0);
$this->setUpdatedDateTime(null);
$this->setUser(null);
$id = $this->getId();
if (is_null($id)) return true;
$q = MariaDb::instance()->prepare('
SELECT
`address`,
`created_datetime`,
`id`,
`label`,
`port`,
`status_bitmask`,
`type_id`,
`updated_datetime`,
`user_id`
FROM `servers` WHERE `id` = ? LIMIT 1;
');
if (!$q || !$q->execute([$id]) || $q->rowCount() != 1) return false;
$this->allocateObject($q->fetchObject());
$q->closeCursor();
return true;
}
/**
* Internal function to process and translate StdClass objects into properties.
*/
private function allocateObject(StdClass $value): void
{
$this->setAddress($value->address);
$this->setCreatedDateTime($value->created_datetime);
$this->setId($value->id);
$this->setLabel($value->label);
$this->setPort($value->port);
$this->setStatusBitmask($value->status_bitmask);
$this->setTypeId($value->type_id);
$this->setUpdatedDateTime($value->updated_datetime);
$this->setUserId($value->user_id);
}
public function commit(): bool
{
$q = MariaDb::instance()->prepare('
INSERT INTO `servers` (
`address`,
`created_datetime`,
`id`,
`label`,
`port`,
`status_bitmask`,
`type_id`,
`updated_datetime`,
`user_id`
) VALUES (
:a, :cdt, :id, :l, :p, :s, :tid, :udt, :uid
) ON DUPLICATE KEY UPDATE
`address` = :a,
`created_datetime` = :cdt,
`label` = :l,
`port` = :p,
`status_bitmask` = :s,
`type_id` = :tid,
`updated_datetime` = :udt,
`user_id` = :uid;
');
$p = [
':a' => $this->getAddress(),
':cdt' => $this->getCreatedDateTime(),
':l' => $this->getLabel(),
':id' => $this->getId(),
':p' => $this->getPort(),
':s' => $this->getStatusBitmask(),
':tid' => $this->getTypeId(),
':udt' => $this->getUpdatedDateTime(),
':uid' => $this->getUserId(),
];
foreach ($p as $k => &$v)
{
if ($v instanceof DateTimeInterface)
{
$p[$k] = $v->format(self::DATE_SQL);
}
}
if (!$q || !$q->execute($p)) return false;
if (is_null($p[':id'])) $this->setId(MariaDb::instance()->lastInsertId());
$q->closeCursor();
return true;
}
/**
* Deallocates the properties of this object from the database.
*
* @return boolean Whether the operation was successful.
*/
public function deallocate(): bool
{
$id = $this->getId();
if (is_null($id)) return false;
$q = MariaDb::instance()->prepare('DELETE FROM `servers` WHERE `id` = ? LIMIT 1;');
try { return $q && $q->execute([$id]); }
finally { $q->closeCursor(); }
}
public function getAddress(): string
{
return $this->address;
}
public static function getAllServers(): ?array
{
$q = MariaDb::instance()->prepare('
SELECT
`address`,
`created_datetime`,
`id`,
`label`,
`port`,
`status_bitmask`,
`type_id`,
`updated_datetime`,
`user_id`
FROM `servers`
ORDER BY
`type_id` ASC,
ISNULL(`label`) ASC,
`label` ASC,
`address` ASC,
`id` ASC;
');
if (!$q || !$q->execute()) return null;
$r = [];
while ($row = $q->fetchObject()) $r[] = new self($row);
$q->closeCursor();
return $r;
}
public function getCreatedDateTime(): DateTimeInterface
{
return $this->created_datetime;
}
public function getName(): string
{
return empty($this->label) ? $this->address . ':' . $this->port : $this->label;
}
public function getId(): ?int
{
return $this->id;
}
public function getLabel(): ?string
{
return $this->label;
}
public function getPort(): int
{
return $this->port;
}
public static function getServersByUserId(int $user_id): ?array
{
$q = MariaDb::instance()->prepare('
SELECT
`address`,
`created_datetime`,
`id`,
`label`,
`port`,
`status_bitmask`,
`type_id`,
`updated_datetime`,
`user_id`
FROM `servers`
WHERE `user_id` = :user_id
ORDER BY `id` ASC;
');
if (!$q || !$q->execute([':user_id' => $user_id])) return null;
$r = [];
while ($row = $q->fetchObject()) $r[] = new self($row);
$q->closeCursor();
return $r;
}
public function getStatusBitmask(): int
{
return $this->status_bitmask;
}
public function getTags(): array
{
$id = $this->getId();
return is_null($id) ? [] : (Tag::allocateAll(TagTypes::Server, $this->getId()) ?? []);
}
public function getType(): ServerType
{
return new ServerType($this->type_id);
}
public function getTypeId(): int
{
return $this->type_id;
}
public function getURI(): ?string
{
$id = $this->getId();
if (is_null($id)) return null;
$label = $this->getLabel();
$value = !empty($label) ? $label : sprintf('%s:%d', $this->getAddress(), $this->getPort());
return \BNETDocs\Libraries\Core\UrlFormatter::format(sprintf(
'/server/%d/%s', $this->getId(), \BNETDocs\Libraries\Core\StringProcessor::sanitizeForUrl($value, true)
));
}
public function getUpdatedDateTime(): ?DateTimeInterface
{
return $this->updated_datetime;
}
public function getUser(): ?User
{
if (is_null($this->user_id)) return null;
return new User($this->user_id);
}
public function getUserId(): ?int
{
return $this->user_id;
}
public function isDisabled(): bool
{
return ($this->status_bitmask & self::STATUS_DISABLED);
}
public function isEnabled(): bool
{
return !$this->isDisabled();
}
public function isOffline(): bool
{
return !$this->isOnline();
}
public function isOnline(): bool
{
return ($this->status_bitmask & self::STATUS_ONLINE);
}
public function jsonSerialize(): mixed
{
return [
'address' => $this->getAddress(),
'created_datetime' => $this->getCreatedDateTime(),
'id' => $this->getId(),
'label' => $this->getLabel(),
'port' => $this->getPort(),
'status_bitmask' => $this->getStatusBitmask(),
'type_id' => $this->getTypeId(),
'updated_datetime' => $this->getUpdatedDateTime(),
'uri' => $this->getURI(),
'user_id' => $this->getUserId(),
];
}
public function setAddress(string $value, bool $allow_empty = false): void
{
if ((!$allow_empty && empty($value)) || strlen($value) > self::MAX_ADDRESS)
{
throw new OutOfBoundsException(sprintf(
'value must be a string between 1-%d', self::MAX_ADDRESS
));
}
$this->address = $value;
}
public function setCreatedDateTime(DateTimeInterface|string $value): void
{
$this->created_datetime = (is_string($value) ?
new DateTimeImmutable($value, new DateTimeZone(self::DATE_TZ)) : $value
);
}
public function setDisabled(bool $value = true): void
{
if ($value) $this->status_bitmask |= self::STATUS_DISABLED;
else $this->status_bitmask &= ~self::STATUS_DISABLED;
}
public function setEnabled(bool $value = true): void
{
$this->setDisabled(!$value);
}
public function setId(?int $value): void
{
if ($value < 0 || $value > self::MAX_ID)
{
throw new OutOfBoundsException(sprintf(
'value must be null or an integer between 0-%d', self::MAX_ID
));
}
$this->id = $value;
}
public function setLabel(?string $value, bool $auto_null = true): void
{
if (!is_null($value) && strlen($value) > self::MAX_LABEL)
{
throw new OutOfBoundsException(sprintf(
'value must be a string between 0-%d characters', self::MAX_LABEL
));
}
$this->label = empty($value) && $auto_null ? null : $value;
}
public function setOffline(bool $value = true): void
{
$this->setOnline(!$value);
}
public function setOnline(bool $value = true): void
{
if ($value) $this->status_bitmask |= self::STATUS_ONLINE;
else $this->status_bitmask &= ~self::STATUS_ONLINE;
}
public function setPort(int $value) : void
{
if ($value < 0 || $value > self::MAX_PORT)
{
throw new OutOfBoundsException(sprintf(
'value must be null or an integer between 0-%d', self::MAX_PORT
));
}
$this->port = $value;
}
public function setStatusBitmask(int $value): void
{
if ($value < 0 || $value > self::MAX_STATUS_BITMASK)
{
throw new OutOfBoundsException(sprintf(
'value must be null or an integer between 0-%d', self::MAX_STATUS_BITMASK
));
}
$this->status_bitmask = $value;
}
public function setType(ServerType $value): void
{
$this->setTypeId($value instanceof ServerType ? $value->getId() : $value);
}
public function setTypeId(int $value): void
{
if ($value < 0 || $value > self::MAX_TYPE_ID)
{
throw new OutOfBoundsException(sprintf(
'value must be null or an integer between 0-%d', self::MAX_TYPE_ID
));
}
$this->type_id = $value;
}
public function setUpdatedDateTime(DateTimeInterface|string|null $value): void
{
$this->updated_datetime = (is_string($value) ?
new DateTimeImmutable($value, new DateTimeZone(self::DATE_TZ)) : $value
);
}
public function setUser(?User $value): void
{
$this->setUserId($value instanceof User ? $value->getId() : $value);
}
public function setUserId(?int $value): void
{
if ($value < 0 || $value > self::MAX_USER_ID)
{
throw new OutOfBoundsException(sprintf(
'value must be null or an integer between 0-%d', self::MAX_USER_ID
));
}
$this->user_id = $value;
}
}