@@ -10,7 +10,8 @@ final class RabbitState implements StateInterface
1010{
1111 private Client $ connection ;
1212 private Channel $ channel ;
13- private array $ metrics ;
13+ private \DateTimeInterface $ date ;
14+ private string $ stepUuid ;
1415
1516 public function __construct (
1617 private string $ host ,
@@ -21,6 +22,9 @@ public function __construct(
2122 private ?string $ vhost = null ,
2223 private ?string $ exchange = null ,
2324 ) {
25+ $ this ->date = new \DateTime ();
26+ $ this ->stepUuid = $ this ->generateRandomUuid ();
27+
2428 $ this ->connection = new Client ([
2529 'host ' => $ this ->host ,
2630 'vhost ' => $ this ->vhost ,
@@ -36,19 +40,25 @@ public function __construct(
3640
3741 public function initialize (int $ start = 0 ): void
3842 {
39- $ this ->metrics = [
40- 'accept ' => 0 ,
41- 'reject ' => 0 ,
42- ];
4343 }
4444
4545 public function accept (int $ step = 1 ): void
4646 {
47- $ this ->metrics ['accept ' ] += $ step ;
48-
4947 $ this ->channel ->publish (
5048 \json_encode ([
51- 'Line accepted : ' => $ this ->metrics ['accept ' ]
49+ 'id ' => $ this ->generateRandomUuid (),
50+ 'date ' => ['date ' => $ this ->date ->format ('c ' ), 'tz ' => $ this ->date ->getTimezone ()->getName ()],
51+ 'stepsUpdates ' => [
52+ [
53+ 'code ' => $ this ->stepUuid ,
54+ 'measurements ' => [
55+ 'increment ' => [
56+ 'code ' => $ this ->generateRandomUuid (),
57+ 'increment ' => $ step
58+ ]
59+ ]
60+ ]
61+ ]
5262 ]),
5363 [
5464 'content-type ' => 'application/json ' ,
@@ -60,11 +70,21 @@ public function accept(int $step = 1): void
6070
6171 public function reject (int $ step = 1 ): void
6272 {
63- $ this ->metrics ['reject ' ] += $ step ;
64-
6573 $ this ->channel ->publish (
6674 \json_encode ([
67- 'Line rejected : ' => $ this ->metrics ['reject ' ]
75+ 'id ' => $ this ->generateRandomUuid (),
76+ 'date ' => ['date ' => $ this ->date ->format ('c ' ), 'tz ' => $ this ->date ->getTimezone ()->getName ()],
77+ 'stepsUpdates ' => [
78+ [
79+ 'code ' => $ this ->stepUuid ,
80+ 'measurements ' => [
81+ 'decrement ' => [
82+ 'code ' => $ this ->generateRandomUuid (),
83+ 'decrement ' => $ step
84+ ]
85+ ]
86+ ]
87+ ]
6888 ]),
6989 [
7090 'content-type ' => 'application/json ' ,
@@ -74,6 +94,16 @@ public function reject(int $step = 1): void
7494 );
7595 }
7696
97+ private function generateRandomUuid (): string
98+ {
99+ $ data = $ data ?? random_bytes (16 );
100+ assert (strlen ($ data ) == 16 );
101+ $ data [6 ] = chr (ord ($ data [6 ]) & 0x0f | 0x40 );
102+ $ data [8 ] = chr (ord ($ data [8 ]) & 0x3f | 0x80 );
103+
104+ return vsprintf ('%s%s-%s-%s-%s-%s%s%s ' , str_split (bin2hex ($ data ), 4 ));
105+ }
106+
77107 public function __destruct ()
78108 {
79109 $ this ->channel ->close ();
0 commit comments