Skip to content

Commit

Permalink
Adding a sameAs() check to CorrelationID
Browse files Browse the repository at this point in the history
  • Loading branch information
burzum committed Dec 9, 2019
1 parent 76e429e commit e854a7e
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 13 deletions.
15 changes: 7 additions & 8 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,28 @@

### 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

Licensed under the [MIT license](LICENSE.txt).

Copyright (c) [Phauthentic](https://github.com/Phauthentic) / Florian Krämer

10 changes: 5 additions & 5 deletions src/Http/Middleware/CorrelationIDMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class CorrelationIDMiddleware implements MiddlewareInterface
/**
* @var string
*/
protected $correlationId;
protected $correlationID;

/**
* @var string
Expand All @@ -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;
}
Expand All @@ -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);
Expand Down
11 changes: 11 additions & 0 deletions src/Utils/CorrelationID.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
4 changes: 4 additions & 0 deletions tests/TestCase/CorrelationIDTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
}

0 comments on commit e854a7e

Please sign in to comment.