From e854a7e8f9cdeaa2fb54c3297dbc0c61777c609b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Kr=C3=A4mer?= Date: Mon, 9 Dec 2019 14:25:37 +0100 Subject: [PATCH] Adding a sameAs() check to CorrelationID --- readme.md | 15 +++++++-------- src/Http/Middleware/CorrelationIDMiddleware.php | 10 +++++----- src/Utils/CorrelationID.php | 11 +++++++++++ tests/TestCase/CorrelationIDTest.php | 4 ++++ 4 files changed, 27 insertions(+), 13 deletions(-) diff --git a/readme.md b/readme.md index 7638ed6..4aa1fdc 100644 --- a/readme.md +++ b/readme.md @@ -13,24 +13,24 @@ ### Correlation ID -The correlation id is a singleon class that will always return the same id for the current life-cycle of the request. +The correlation ID is a singleton class that will always return the same ID for the current life-cycle of the request. ### Middleware -The middleware will automatically put the correlation id into your request object as attribute and header value. By default both use the `CorrelationId` name. +The middleware will automatically put the correlation ID into your request object as attribute and header value. By default both use the `CorrelationID` name. ``` -$middleware = new CorrelationIdMiddleware( - CorrelationId::toString() -); +$middleware = new CorrelationIDMiddleware( + CorrelationID::toString() +); ``` ### Response -Since there is no standard for where this needs to be done, just add the correlation id to your response where ever it suits your architecture or framework. +Since there is no standard for where this needs to be done, just add the correlation ID to your response where ever it suits your architecture or framework. ```php -$response->withHeader('CorrelationId', CorrelationId::toString()); +$response->withHeader('CorrelationID', CorrelationId::toString()); ``` ## Copyright & License @@ -38,4 +38,3 @@ $response->withHeader('CorrelationId', CorrelationId::toString()); Licensed under the [MIT license](LICENSE.txt). Copyright (c) [Phauthentic](https://github.com/Phauthentic) / Florian Krämer - diff --git a/src/Http/Middleware/CorrelationIDMiddleware.php b/src/Http/Middleware/CorrelationIDMiddleware.php index 7ff2935..52e9f97 100644 --- a/src/Http/Middleware/CorrelationIDMiddleware.php +++ b/src/Http/Middleware/CorrelationIDMiddleware.php @@ -30,7 +30,7 @@ class CorrelationIDMiddleware implements MiddlewareInterface /** * @var string */ - protected $correlationId; + protected $correlationID; /** * @var string @@ -50,11 +50,11 @@ class CorrelationIDMiddleware implements MiddlewareInterface * @param string $headerName Header Name */ public function __construct( - string $correlationId, + string $correlationID, string $attributeName = 'CorrelationID', string $headerName = 'CorrelationID' ) { - $this->correlationId = $correlationId; + $this->correlationID = $correlationID; $this->attributeName = $attributeName; $this->headerName = $headerName; } @@ -71,12 +71,12 @@ public function process( ): ResponseInterface { $request = $request->withAttribute( $this->attributeName, - $this->correlationId + $this->correlationID ); $request = $request->withHeader( $this->headerName, - $this->correlationId + $this->correlationID ); return $handler->handle($request); diff --git a/src/Utils/CorrelationID.php b/src/Utils/CorrelationID.php index ac7c386..549fed9 100644 --- a/src/Utils/CorrelationID.php +++ b/src/Utils/CorrelationID.php @@ -41,6 +41,17 @@ public static function toString(): string return static::$uuid; } + /** + * Compares the current id against another + * + * @param string $otherID Other ID + * @return boolean + */ + public static function sameAs(string $otherID): bool + { + return static::$uuid === $otherID; + } + /** * Generates a new correlation ID. * diff --git a/tests/TestCase/CorrelationIDTest.php b/tests/TestCase/CorrelationIDTest.php index d6c8276..2228f24 100644 --- a/tests/TestCase/CorrelationIDTest.php +++ b/tests/TestCase/CorrelationIDTest.php @@ -34,5 +34,9 @@ public function testCorrelationId(): void $result2 = CorrelationID::toString(); $this->assertEquals($result, $result2); + + $string = CorrelationID::toString(); + $this->assertTrue(CorrelationID::sameAs(CorrelationID::toString())); + $this->assertFalse(CorrelationID::sameAs('1234')); } }