Skip to content

Commit 3b11dcc

Browse files
committed
fixup! feat(snowflake): extend Entity class to support snowflakes
1 parent bd6cf6b commit 3b11dcc

File tree

4 files changed

+16
-35
lines changed

4 files changed

+16
-35
lines changed

lib/private/Snowflake/SnowflakeDecoder.php

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
*/
2424
final class SnowflakeDecoder implements ISnowflakeDecoder {
2525
#[Override]
26-
public function decode(string $snowflakeId): array {
26+
public function decode(string $snowflakeId): Snowflake {
2727
if (!ctype_digit($snowflakeId)) {
2828
throw new \Exception('Invalid Snowflake ID: ' . $snowflakeId);
2929
}
@@ -41,7 +41,14 @@ public function decode(string $snowflakeId): array {
4141
)
4242
);
4343

44-
return $data;
44+
return new Snowflake(
45+
$data['serverId'],
46+
$data['sequenceId'],
47+
$data['isCli'],
48+
$data['seconds'],
49+
$data['milliseconds'],
50+
$data['createdAt'],
51+
);
4552
}
4653

4754
private function decode64bits(int $snowflakeId): array {
@@ -120,16 +127,4 @@ private function convertBase16(string $decimal): string {
120127

121128
return str_pad($hex, 16, '0', STR_PAD_LEFT);
122129
}
123-
124-
public function decodeToSnowflake(string $snowflakeId): Snowflake {
125-
$data = $this->decode($snowflakeId);
126-
return new Snowflake(
127-
$data['serverId'],
128-
$data['sequenceId'],
129-
$data['isCli'],
130-
$data['seconds'],
131-
$data['milliseconds'],
132-
$data['createdAt'],
133-
);
134-
}
135130
}

lib/public/AppFramework/Db/SnowflakeAwareEntity.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
use OCP\Snowflake\Snowflake;
1616

1717
/**
18+
* Entity with snowflake support
19+
*
1820
* @since 33.0.0
1921
*/
2022
#[Consumable(since: '33.0.0')]
@@ -50,9 +52,6 @@ public function getId(): string {
5052
}
5153

5254
public function getCreatedAt(): ?\DateTimeImmutable {
53-
if (empty($this->id)) {
54-
return null;
55-
}
5655
return $this->getSnowflake()?->getCreatedAt();
5756
}
5857

@@ -61,8 +60,8 @@ public function getSnowflake(): ?Snowflake {
6160
return null;
6261
}
6362

64-
if (empty($this->snowflake)) {
65-
$this->snowflake = Server::get(ISnowflakeDecoder::class)->decodeToSnowflake($this->id);
63+
if ($this->snowflake === null) {
64+
$this->snowflake = Server::get(ISnowflakeDecoder::class)->decode($this->id);
6665
}
6766

6867
return $this->snowflake;

lib/public/Snowflake/ISnowflakeDecoder.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,8 @@ interface ISnowflakeDecoder {
2828
* - createdAt: timestamp at which ID was generated
2929
* - isCli: if ID was generated using CLI or not
3030
*
31-
* @return array{createdAt: \DateTimeImmutable, serverId: int<0,1023>, sequenceId: int<0,4095>, isCli: bool, seconds: positive-int, milliseconds: int<0,999>}
31+
* @return Snowflake
3232
* @since 33.0
3333
*/
34-
public function decode(string $snowflakeId): array;
35-
36-
public function decodeToSnowflake(string $snowflakeId): Snowflake;
34+
public function decode(string $snowflakeId): Snowflake;
3735
}

lib/public/Snowflake/Snowflake.php

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* @since 33.0.0
1818
*/
1919
#[Consumable(since: '33.0.0')]
20-
final class Snowflake {
20+
final readonly class Snowflake {
2121
/**
2222
* @psalm-param int<0,1023> $serverId
2323
* @psalm-param int<0,4095> $sequenceId
@@ -34,17 +34,6 @@ public function __construct(
3434
) {
3535
}
3636

37-
public function toArray(): array {
38-
return [
39-
'serverId' => $this->getServerId(),
40-
'sequenceId' => $this->getSequenceId(),
41-
'isCli' => $this->isCli(),
42-
'seconds' => $this->getSeconds(),
43-
'milliseconds' => $this->getMilliseconds(),
44-
'createdAt' => $this->getCreatedAt(),
45-
];
46-
}
47-
4837
/**
4938
* @psalm-return int<0,1023>
5039
*/

0 commit comments

Comments
 (0)