Skip to content

Commit 259a8ba

Browse files
committed
Refactor Snowflace typecasters
1 parent 907158d commit 259a8ba

File tree

8 files changed

+107
-111
lines changed

8 files changed

+107
-111
lines changed

src/Listener/SnowflakeDiscord.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,11 @@ protected function createValue(): DiscordSnowflake
6666

6767
/**
6868
* Get the current process ID.
69+
*
70+
* @return int<0, 281474976710655>
6971
*/
7072
private function getProcessId(): int
7173
{
72-
return self::$defaultProcessId === null ? \intval(\getmypid()) : self::$defaultProcessId;
74+
return self::$defaultProcessId ?? \intval(\getmypid());
7375
}
7476
}

src/Listener/SnowflakeGeneric.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ public static function setDefaults(?int $node, Epoch|int|null $epochOffset): voi
5656
}
5757
}
5858

59+
/**
60+
* Get default epoch offset.
61+
*/
62+
public static function getEpochOffset(): Epoch|int
63+
{
64+
return self::$epochOffset;
65+
}
66+
5967
#[\Override]
6068
protected function createValue(): Snowflake
6169
{

src/Snowflake.php

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace Cycle\ORM\Entity\Behavior\Identifier;
66

7-
use Cycle\Database\DatabaseInterface;
87
use Cycle\ORM\Entity\Behavior\Schema\BaseModifier;
98
use Cycle\ORM\Entity\Behavior\Schema\RegistryModifier;
109
use Cycle\ORM\Schema\GeneratedField;
@@ -24,7 +23,6 @@ abstract class Snowflake extends BaseModifier
2423
public function compute(Registry $registry): void
2524
{
2625
$modifier = new RegistryModifier($registry, $this->role);
27-
/** @var non-empty-string column */
2826
$this->column = $modifier->findColumnName($this->field, $this->column);
2927
if (\is_string($this->column) && $this->column !== '') {
3028
$modifier->addSnowflakeColumn(
@@ -35,7 +33,7 @@ public function compute(Registry $registry): void
3533

3634
$modifier->setTypecast(
3735
$registry->getEntity($this->role)->getFields()->get($this->field),
38-
[static::class, 'fromInteger', $this->getTypecastArgs()],
36+
$this->getTypecast(),
3937
);
4038
}
4139
}
@@ -44,7 +42,6 @@ public function compute(Registry $registry): void
4442
public function render(Registry $registry): void
4543
{
4644
$modifier = new RegistryModifier($registry, $this->role);
47-
/** @var non-empty-string column */
4845
$this->column = $modifier->findColumnName($this->field, $this->column) ?? $this->field;
4946

5047
$modifier->addSnowflakeColumn(
@@ -55,18 +52,12 @@ public function render(Registry $registry): void
5552

5653
$modifier->setTypecast(
5754
$registry->getEntity($this->role)->getFields()->get($this->field),
58-
[static::class, 'fromInteger', $this->getTypecastArgs()],
55+
$this->getTypecast(),
5956
);
6057
}
6158

6259
/**
63-
* @param int<0, max>|numeric-string $identifier
60+
* @return array{0: class-string, 1: non-empty-string, 2?: non-empty-array}
6461
*/
65-
abstract public static function fromInteger(
66-
int|string $identifier,
67-
DatabaseInterface $database,
68-
array $arguments,
69-
): \Ramsey\Identifier\Snowflake;
70-
71-
abstract protected function getTypecastArgs(): array;
62+
abstract protected function getTypecast(): array;
7263
}

src/SnowflakeDiscord.php

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
namespace Cycle\ORM\Entity\Behavior\Identifier;
66

7-
use Cycle\Database\DatabaseInterface;
87
use Cycle\ORM\Entity\Behavior\Identifier\Snowflake as BaseSnowflake;
98
use Cycle\ORM\Entity\Behavior\Identifier\Listener\SnowflakeDiscord as Listener;
109
use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor;
10+
use Ramsey\Identifier\Snowflake\DiscordSnowflake;
1111
use Ramsey\Identifier\Snowflake\DiscordSnowflakeFactory;
1212

1313
/**
@@ -26,8 +26,6 @@ final class SnowflakeDiscord extends BaseSnowflake
2626
* @param int<0, 281474976710655>|null $workerId A worker identifier to use when creating Snowflakes
2727
* @param int<0, 281474976710655>|null $processId A process identifier to use when creating Snowflakes
2828
* @param bool $nullable Indicates whether to generate a new Snowflake or not
29-
*
30-
* @see \Ramsey\Identifier\Snowflake\DiscordSnowflakeFactory::create()
3129
*/
3230
public function __construct(
3331
string $field = 'snowflake',
@@ -41,16 +39,23 @@ public function __construct(
4139
$this->nullable = $nullable;
4240
}
4341

44-
#[\Override]
45-
public static function fromInteger(
42+
/**
43+
* Identifier factory method from an existing identifier value.
44+
*
45+
* @param int<0, max>|numeric-string $identifier The identifier to create the Snowflake from
46+
*
47+
* @see DiscordSnowflakeFactory::create()
48+
*/
49+
public static function create(
4650
int|string $identifier,
47-
DatabaseInterface $database,
48-
array $arguments,
49-
): \Ramsey\Identifier\Snowflake {
50-
return (new DiscordSnowflakeFactory(
51-
$arguments['workerId'],
52-
$arguments['processId'],
53-
))->createFromInteger($identifier);
51+
): DiscordSnowflake {
52+
return new DiscordSnowflake($identifier);
53+
}
54+
55+
#[\Override]
56+
protected function getTypecast(): array
57+
{
58+
return [self::class, 'create'];
5459
}
5560

5661
#[\Override]
@@ -77,13 +82,4 @@ protected function getListenerArgs(): array
7782
'nullable' => $this->nullable,
7883
];
7984
}
80-
81-
#[\Override]
82-
protected function getTypecastArgs(): array
83-
{
84-
return [
85-
'workerId' => $this->workerId,
86-
'processId' => $this->processId,
87-
];
88-
}
8985
}

src/SnowflakeGeneric.php

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,19 @@
44

55
namespace Cycle\ORM\Entity\Behavior\Identifier;
66

7-
use Cycle\Database\DatabaseInterface;
87
use Cycle\ORM\Entity\Behavior\Identifier\Snowflake as BaseSnowflake;
98
use Cycle\ORM\Entity\Behavior\Identifier\Listener\SnowflakeGeneric as Listener;
109
use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor;
1110
use Ramsey\Identifier\Snowflake\Epoch;
11+
use Ramsey\Identifier\Snowflake\GenericSnowflake;
1212
use Ramsey\Identifier\Snowflake\GenericSnowflakeFactory;
1313

1414
/**
1515
* A distributed ID generation system developed by Twitter that produces
1616
* 64-bit unique, sortable identifiers
1717
*
18+
* Use {@see Listener::setDefaults()} to set default node and epoch offset.
19+
*
1820
* @Annotation
1921
* @NamedArgumentConstructor()
2022
* @Target({"CLASS"})
@@ -28,8 +30,6 @@ final class SnowflakeGeneric extends BaseSnowflake
2830
* @param int<0, 1023>|null $node A node identifier to use when creating Snowflakes
2931
* @param Epoch|int|null $epochOffset The offset from the Unix Epoch in milliseconds
3032
* @param bool $nullable Indicates whether to generate a new Snowflake or not
31-
*
32-
* @see \Ramsey\Identifier\Snowflake\GenericSnowflakeFactory::create()
3333
*/
3434
public function __construct(
3535
string $field = 'snowflake',
@@ -43,16 +43,28 @@ public function __construct(
4343
$this->nullable = $nullable;
4444
}
4545

46-
#[\Override]
47-
public static function fromInteger(
46+
/**
47+
* Identifier factory method from an existing identifier value.
48+
*
49+
* @param int<0, max>|numeric-string $identifier The identifier to create the Snowflake from
50+
* @param int $epochOffset The offset from the Unix Epoch in milliseconds
51+
*
52+
* @see GenericSnowflakeFactory::create()
53+
*/
54+
public static function create(
4855
int|string $identifier,
49-
DatabaseInterface $database,
50-
array $arguments,
51-
): \Ramsey\Identifier\Snowflake {
52-
return (new GenericSnowflakeFactory(
53-
$arguments['node'],
54-
$arguments['epochOffset'],
55-
))->createFromInteger($identifier);
56+
int $epochOffset,
57+
): GenericSnowflake {
58+
return new GenericSnowflake($identifier, $epochOffset);
59+
}
60+
61+
#[\Override]
62+
protected function getTypecast(): array
63+
{
64+
$epochOffset = $this->epochOffset ?? Listener::getEpochOffset();
65+
$epochOffset instanceof Epoch and $epochOffset = $epochOffset->value;
66+
67+
return [self::class, 'create', [$epochOffset]];
5668
}
5769

5870
#[\Override]
@@ -79,13 +91,4 @@ protected function getListenerArgs(): array
7991
'nullable' => $this->nullable,
8092
];
8193
}
82-
83-
#[\Override]
84-
protected function getTypecastArgs(): array
85-
{
86-
return [
87-
'node' => $this->node,
88-
'epochOffset' => $this->epochOffset,
89-
];
90-
}
9194
}

src/SnowflakeInstagram.php

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
namespace Cycle\ORM\Entity\Behavior\Identifier;
66

7-
use Cycle\Database\DatabaseInterface;
87
use Cycle\ORM\Entity\Behavior\Identifier\Snowflake as BaseSnowflake;
98
use Cycle\ORM\Entity\Behavior\Identifier\Listener\SnowflakeInstagram as Listener;
109
use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor;
10+
use Ramsey\Identifier\Snowflake\InstagramSnowflake;
1111
use Ramsey\Identifier\Snowflake\InstagramSnowflakeFactory;
1212

1313
/**
@@ -25,8 +25,6 @@ final class SnowflakeInstagram extends BaseSnowflake
2525
* @param non-empty-string|null $column Snowflake column name
2626
* @param int<0, 1023>|null $shardId A shard identifier to use when creating Snowflakes
2727
* @param bool $nullable Indicates whether to generate a new Snowflake or not
28-
*
29-
* @see \Ramsey\Identifier\Snowflake\InstagramSnowflakeFactory::create()
3028
*/
3129
public function __construct(
3230
string $field = 'snowflake',
@@ -39,15 +37,23 @@ public function __construct(
3937
$this->nullable = $nullable;
4038
}
4139

42-
#[\Override]
43-
public static function fromInteger(
40+
/**
41+
* Identifier factory method from an existing identifier value.
42+
*
43+
* @param int<0, max>|numeric-string $identifier The identifier to create the Snowflake from
44+
*
45+
* @see InstagramSnowflakeFactory::create()
46+
*/
47+
public static function create(
4448
int|string $identifier,
45-
DatabaseInterface $database,
46-
array $arguments,
47-
): \Ramsey\Identifier\Snowflake {
48-
return (new InstagramSnowflakeFactory(
49-
$arguments['shardId'],
50-
))->createFromInteger($identifier);
49+
): InstagramSnowflake {
50+
return new InstagramSnowflake($identifier);
51+
}
52+
53+
#[\Override]
54+
protected function getTypecast(): array
55+
{
56+
return [self::class, 'create'];
5157
}
5258

5359
#[\Override]
@@ -72,12 +78,4 @@ protected function getListenerArgs(): array
7278
'nullable' => $this->nullable,
7379
];
7480
}
75-
76-
#[\Override]
77-
protected function getTypecastArgs(): array
78-
{
79-
return [
80-
'shardId' => $this->shardId,
81-
];
82-
}
8381
}

src/SnowflakeMastodon.php

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
namespace Cycle\ORM\Entity\Behavior\Identifier;
66

7-
use Cycle\Database\DatabaseInterface;
87
use Cycle\ORM\Entity\Behavior\Identifier\Snowflake as BaseSnowflake;
98
use Cycle\ORM\Entity\Behavior\Identifier\Listener\SnowflakeMastodon as Listener;
109
use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor;
10+
use Ramsey\Identifier\Snowflake\MastodonSnowflake;
1111
use Ramsey\Identifier\Snowflake\MastodonSnowflakeFactory;
1212

1313
/**
@@ -25,8 +25,6 @@ final class SnowflakeMastodon extends BaseSnowflake
2525
* @param non-empty-string|null $column Snowflake column name
2626
* @param non-empty-string|null $tableName Database table name ensuring different tables derive separate sequence bases
2727
* @param bool $nullable Indicates whether to generate a new Snowflake or not
28-
*
29-
* @see \Ramsey\Identifier\Snowflake\MastodonSnowflakeFactory::create()
3028
*/
3129
public function __construct(
3230
string $field = 'snowflake',
@@ -39,15 +37,23 @@ public function __construct(
3937
$this->nullable = $nullable;
4038
}
4139

42-
#[\Override]
43-
public static function fromInteger(
40+
/**
41+
* Identifier factory method from an existing identifier value.
42+
*
43+
* @param int<0, max>|numeric-string $identifier The identifier to create the Snowflake from
44+
*
45+
* @see MastodonSnowflakeFactory::create()
46+
*/
47+
public static function create(
4448
int|string $identifier,
45-
DatabaseInterface $database,
46-
array $arguments,
47-
): \Ramsey\Identifier\Snowflake {
48-
return (new MastodonSnowflakeFactory(
49-
$arguments['tableName'],
50-
))->createFromInteger($identifier);
49+
): MastodonSnowflake {
50+
return new MastodonSnowflake($identifier);
51+
}
52+
53+
#[\Override]
54+
protected function getTypecast(): array
55+
{
56+
return [self::class, 'create'];
5157
}
5258

5359
#[\Override]
@@ -72,12 +78,4 @@ protected function getListenerArgs(): array
7278
'nullable' => $this->nullable,
7379
];
7480
}
75-
76-
#[\Override]
77-
protected function getTypecastArgs(): array
78-
{
79-
return [
80-
'tableName' => $this->tableName,
81-
];
82-
}
8381
}

0 commit comments

Comments
 (0)