refactor: base event

Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
2026-08-08 22:15:24 -04:00
parent c8e6efe203
commit 62b416f13e
20 changed files with 109 additions and 99 deletions
+33 -33
View File
@@ -10,74 +10,74 @@ namespace KTXF\Event;
class Event
{
private bool $propagationStopped = false;
private readonly array $data;
private readonly array $context;
private readonly float $timestamp;
private readonly string $eventId;
private readonly string $identifier;
public function __construct(
private readonly string $name,
array $data = [],
private readonly ?string $tenantId = null,
private readonly ?string $identityId = null,
private readonly string $label,
array $context = [],
private readonly ?string $tenantIdentifier = null,
private readonly ?string $actorIdentity = null,
) {
self::validateData($data);
self::validateContext($context);
$this->data = $data;
$this->context = $context;
$this->timestamp = microtime(true);
$this->eventId = bin2hex(random_bytes(16));
$this->identifier = bin2hex(random_bytes(16));
}
/**
* Get the event name
* Get the event label
*/
public function getName(): string
public function label(): string
{
return $this->name;
return $this->label;
}
/**
* Get a data value by key
* Get a context value by key
*/
public function get(string $key, mixed $default = null): mixed
{
return $this->data[$key] ?? $default;
return $this->context[$key] ?? $default;
}
/**
* Check if a data key exists
* Check if a context key exists
*/
public function has(string $key): bool
{
return array_key_exists($key, $this->data);
return array_key_exists($key, $this->context);
}
/**
* Get all data
* Get the event context
*/
public function getData(): array
public function context(): array
{
return $this->data;
return $this->context;
}
/**
* Alias for getData() for backward compatibility
* Get all event context
*/
public function all(): array
{
return $this->data;
return $this->context;
}
/**
* Get the event timestamp
*/
public function getTimestamp(): float
public function timestamp(): float
{
return $this->timestamp;
}
public function getEventId(): string
public function identifier(): string
{
return $this->eventId;
return $this->identifier;
}
/**
@@ -99,29 +99,29 @@ class Event
/**
* Get tenant ID for multi-tenant context
*/
public function getTenantId(): ?string
public function tenantIdentifier(): ?string
{
return $this->tenantId;
return $this->tenantIdentifier;
}
/**
* Get identity ID (user who triggered the event)
* Get the identity of the actor who triggered the event
*/
public function getIdentityId(): ?string
public function actorIdentity(): ?string
{
return $this->identityId;
return $this->actorIdentity;
}
private static function validateData(array $data): void
private static function validateContext(array $context): void
{
foreach ($data as $value) {
foreach ($context as $value) {
if (is_array($value)) {
self::validateData($value);
self::validateContext($value);
continue;
}
if ($value !== null && !is_scalar($value)) {
throw new \InvalidArgumentException(
'Event data must contain only scalar, null, or array values.',
'Event context must contain only scalar, null, or array values.',
);
}
}