-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Expand file tree
/
Copy pathSnowflakeAwareEntity.php
More file actions
69 lines (59 loc) · 1.54 KB
/
SnowflakeAwareEntity.php
File metadata and controls
69 lines (59 loc) · 1.54 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
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCP\AppFramework\Db;
use OCP\AppFramework\Attribute\Consumable;
use OCP\DB\Types;
use OCP\Server;
use OCP\Snowflake\ISnowflakeDecoder;
use OCP\Snowflake\ISnowflakeGenerator;
use OCP\Snowflake\Snowflake;
/**
* Entity with snowflake support
* @method string getId()
* @since 33.0.0
*/
#[Consumable(since: '33.0.0')]
abstract class SnowflakeAwareEntity extends Entity {
protected ?Snowflake $snowflake = null;
/** @psalm-param $_fieldTypes array<string, Types::*> */
protected array $_fieldTypes = ['id' => Types::STRING];
/**
* @since 33.0.0
*/
public function setId($id): void {
throw new \LogicException('Use generated id to set a new id to the Snowflake aware entity.');
}
/**
* Automatically creates a snowflake ID
* @since 33.0.0
*/
public function generateId(): void {
if ($this->id === null) {
/** @psalm-suppress InvalidPropertyAssignmentValue */
$this->id = Server::get(ISnowflakeGenerator::class)->nextId();
$this->markFieldUpdated('id');
}
}
/**
* @since 33.0.0
*/
public function getCreatedAt(): ?\DateTimeImmutable {
return $this->getSnowflake()?->getCreatedAt();
}
/**
* @since 33.0.0
*/
public function getSnowflake(): ?Snowflake {
if ($this->id === null) {
return null;
}
if ($this->snowflake === null) {
$this->snowflake = Server::get(ISnowflakeDecoder::class)->decode($this->getId());
}
return $this->snowflake;
}
}