Skip to content

Commit

Permalink
Rename package and entities
Browse files Browse the repository at this point in the history
  • Loading branch information
arrilot committed Nov 19, 2021
1 parent 37a630b commit c24a447
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 93 deletions.
28 changes: 14 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ First of all you need to create initiator data and place it to holder:

```php

use Ensi\InitiatorPropagation\InitiatorHolder;
use Ensi\InitiatorPropagation\InitiatorDTO;
use Ensi\InitialEventPropagation\InitialEventHolder;
use Ensi\InitialEventPropagation\InitialEventDTO;

InitiatorHolder::getInstance()
InitialEventHolder::getInstance()
->setInitiator(
InitiatorDTO::fromScratch(
InitialEventDTO::fromScratch(
userId: "1",
userType: "admin",
app: "mobile-api-gateway",
Expand All @@ -37,27 +37,27 @@ If you are not in initial entrypoint context to need to get Initiator from `X-In

```php

use Ensi\InitiatorPropagation\Config;
use Ensi\InitiatorPropagation\InitiatorHolder;
use Ensi\InitiatorPropagation\InitiatorDTO;
use Ensi\InitialEventPropagation\Config;
use Ensi\InitialEventPropagation\InitialEventHolder;
use Ensi\InitialEventPropagation\InitialEventDTO;

InitiatorHolder::getInstance()
InitialEventHolder::getInstance()
->setInitiator(
InitiatorDTO::fromSerializedString($request->header(Config::REQUEST_HEADER))
InitialEventDTO::fromSerializedString($request->header(Config::REQUEST_HEADER))
);
```

Next, extract DTO from holder (`InitiatorHolder::getInstance()->getInitiator`) and pass it to any futher outcomming requests (Guzzle, RabbitMQ, Kafka etc)
Next, extract DTO from holder (`InitialEventHolder::getInstance()->getInitiator`) and pass it to any futher outcomming requests (Guzzle, RabbitMQ, Kafka etc)
For example:
```php

use Ensi\InitiatorPropagation\Config;
use Ensi\InitiatorPropagation\InitiatorHolder;
use Ensi\InitialEventPropagation\Config;
use Ensi\InitialEventPropagation\InitialEventHolder;

function some_middleware(callable $handler)
{
return function (RequestInterface $request, $options) use ($handler) {
$inititiator = InitiatorHolder::getInstance()->getInitiator();
$inititiator = InitialEventHolder::getInstance()->getInitiator();

return $handler(
$inititiator ? $request->withHeader(Config::REQUEST_HEADER, $inititiator->serialize()) : $request,
Expand All @@ -69,7 +69,7 @@ function some_middleware(callable $handler)

### Guzzle

You can use built-in `Ensi\InitiatorPropagation\PropagateInitiatorGuzzleMiddleware` to propagate `X-Initiator` header to every outcomming guzzle request made by the given Guzzle handler: `$stack->push(new PropagateInitiatorGuzzleMiddleware());`
You can use built-in `Ensi\InitialEventPropagation\PropagateInitiatorGuzzleMiddleware` to propagate `X-Initiator` header to every outcomming guzzle request made by the given Guzzle handler: `$stack->push(new PropagateInitiatorGuzzleMiddleware());`


## Contributing
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@
},
"autoload": {
"psr-4": {
"Ensi\\InitiatorPropagation\\": "src"
"Ensi\\InitialEventPropagation\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Ensi\\InitiatorPropagation\\Tests\\": "tests"
"Ensi\\InitialEventPropagation\\Tests\\": "tests"
}
},
"scripts": {
Expand Down
62 changes: 31 additions & 31 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/Config.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

namespace Ensi\InitiatorPropagation;
namespace Ensi\InitialEventPropagation;

class Config
{
public const REQUEST_HEADER = 'X-Initiator';
public const REQUEST_HEADER = 'X-Initial-Event';
}
4 changes: 2 additions & 2 deletions src/EmptySerializedFieldException.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Ensi\InitiatorPropagation;
namespace Ensi\InitialEventPropagation;

use InvalidArgumentException;
use Throwable;
Expand All @@ -9,7 +9,7 @@ class EmptySerializedFieldException extends InvalidArgumentException
{
public function __construct(string $field, int $code = 0, ?Throwable $previous = null)
{
$message = "Initiator propagation error: \"{$field}\" is not set in serialized string";
$message = "Initial event propagation error: \"{$field}\" is not set in serialized string";
parent::__construct($message, $code, $previous);
}
}
14 changes: 7 additions & 7 deletions src/InitiatorDTO.php → src/InitialEventDTO.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<?php

namespace Ensi\InitiatorPropagation;
namespace Ensi\InitialEventPropagation;

use DateTime;
use DateTimeZone;
use Ramsey\Uuid\Uuid;

class InitiatorDTO
class InitialEventDTO
{
public function __construct(
public string $correlationId,
public string $startedAt,
public string $timestamp,
public string $app,
public string $entrypoint,
public string $userId = '',
Expand All @@ -28,11 +28,11 @@ public static function fromScratch(
string $realUserId = '',
string $realUserType = '',
string $correlationId = '',
string $startedAt = ''
string $timestamp = ''
): static {
return new static(
correlationId: $correlationId ?: Uuid::uuid4()->toString(),
startedAt: $startedAt ?: (new DateTime())->setTimezone(new DateTimeZone("UTC"))->format('Y-m-d\TH:i:s.u\Z'),
timestamp: $timestamp ?: (new DateTime())->setTimezone(new DateTimeZone("UTC"))->format('Y-m-d\TH:i:s.u\Z'),
app: $app,
entrypoint: $entrypoint,
userId: $userId,
Expand All @@ -55,7 +55,7 @@ public static function fromSerializedString(string $serializedData): static

return new static(
correlationId: $params['correlationId'] ?? throw new EmptySerializedFieldException("correlationId"),
startedAt: $params['startedAt'] ?? throw new EmptySerializedFieldException("startedAt"),
timestamp: $params['timestamp'] ?? throw new EmptySerializedFieldException("timestamp"),
app: $params['app'] ?? throw new EmptySerializedFieldException("app"),
entrypoint: $params['entrypoint'] ?? throw new EmptySerializedFieldException("entrypoint"),
userId: $params['userId'] ?? '',
Expand All @@ -81,7 +81,7 @@ public function toArray(): array
{
return [
'correlationId' => $this->correlationId,
'startedAt' => $this->startedAt,
'timestamp' => $this->timestamp,
'app' => $this->app,
'entrypoint' => $this->entrypoint,
'userId' => $this->userId,
Expand Down
10 changes: 5 additions & 5 deletions src/InitiatorHolder.php → src/InitialEventHolder.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php

namespace Ensi\InitiatorPropagation;
namespace Ensi\InitialEventPropagation;

class InitiatorHolder
class InitialEventHolder
{
private static array $instances = [];

protected ?InitiatorDTO $initiator = null;
protected ?InitialEventDTO $initiator = null;

public static function getInstance(): static
{
Expand All @@ -18,14 +18,14 @@ public static function getInstance(): static
return self::$instances[$className];
}

public function setInitiator(InitiatorDTO $initiator): static
public function setInitiator(InitialEventDTO $initiator): static
{
$this->initiator = $initiator;

return $this;
}

public function getInitiator(): ?InitiatorDTO
public function getInitiator(): ?InitialEventDTO
{
return $this->initiator;
}
Expand Down
20 changes: 0 additions & 20 deletions src/PropagateInitiatorGuzzleMiddleware.php

This file was deleted.

Loading

0 comments on commit c24a447

Please sign in to comment.