11<?php
22
3+ declare (strict_types=1 );
4+
35namespace Ennexa \Snowflake ;
46
5- use Exception \InvalidArgumentException ;
6- use Exception \InvalidSystemClockException ;
7+ use Ennexa \ Snowflake \ Exception \InvalidArgumentException ;
8+ use Ennexa \ Snowflake \ Exception \InvalidSystemClockException ;
79
8- class Generator {
10+ class Generator
11+ {
912 const NODE_LEN = 8 ;
1013 const WORKER_LEN = 8 ;
1114 const SEQUENCE_LEN = 8 ;
1215
13- private $ instanceId = 0 ;
14- private $ startEpoch = 1546300800000 ;
15- private $ sequenceMax ;
16- private $ store ;
16+ private int $ instanceId = 0 ;
17+ private int $ startEpoch = 1546300800000 ;
18+ private int $ sequenceMask ;
19+ private int $ sequenceMax ;
20+ private StoreInterface $ store ;
21+ private int $ tickShift ;
1722
18- private static function getMaxValue (int $ len )
23+ private static function getMaxValue (int $ len ): int
1924 {
2025 return -1 ^ (-1 << $ len );
2126 }
2227
2328 public static function generateInstanceId ($ nodeId = 0 , $ workerId = 0 )
2429 {
25- $ nodeIdMax = $ this -> getMaxValue (self ::NODE_LEN );
30+ $ nodeIdMax = self :: getMaxValue (self ::NODE_LEN );
2631 if ($ nodeId < 0 || $ nodeId > $ nodeIdMax ) {
27- throw InvalidArgumentException ("Node ID should be between 0 and $ nodeIdMax " );
32+ throw new InvalidArgumentException ("Node ID should be between 0 and $ nodeIdMax " );
2833 }
2934
30- $ workerIdMax = $ this -> getMaxValue (self ::WORKER_LEN );
35+ $ workerIdMax = self :: getMaxValue (self ::WORKER_LEN );
3136 if ($ workerId < 0 || $ workerId > $ workerIdMax ) {
32- throw InvalidArgumentException ("Worker ID should be between 0 and $ workerIdMax " );
37+ throw new InvalidArgumentException ("Worker ID should be between 0 and $ workerIdMax " );
3338 }
3439
3540 return $ nodeId << self ::WORKER_LEN | $ workerId ;
@@ -51,33 +56,24 @@ public function __construct(StoreInterface $store, int $instanceId = 0, ?int $st
5156
5257 /**
5358 * Set the sequence store
54- *
55- * @param int Instance Id
56- * @return void
5759 */
58- public function setStore (StoreInterface $ store )
60+ public function setStore (StoreInterface $ store ): void
5961 {
6062 $ this ->store = $ store ;
6163 }
6264
6365 /**
6466 * Get the current generator instance id
65- *
66- * @param int Instance Id
67- * @return void
6867 */
69- public function getInstanceId ()
68+ public function getInstanceId (): int
7069 {
7170 return $ this ->instanceId >> self ::SEQUENCE_LEN ;
7271 }
7372
7473 /**
7574 * Set the instance id for the generator instance
76- *
77- * @param int Instance Id
78- * @return void
7975 */
80- public function setInstanceId (int $ instanceId )
76+ public function setInstanceId (int $ instanceId ): void
8177 {
8278 $ this ->instanceId = $ instanceId << self ::SEQUENCE_LEN ;
8379 }
@@ -95,10 +91,10 @@ public function nextSequence()
9591 /**
9692 * Generate a unique id based on the epoch and instance id
9793 *
98- * @return int unique 64-bit id
94+ * @return string unique 64-bit id
9995 * @throws InvalidSystemClockException
10096 */
101- public function nextId ()
97+ public function nextId (): string
10298 {
10399 list ($ timestamp , $ sequence ) = $ this ->nextSequence ();
104100
0 commit comments