Files
server/shared/lib/Event/Event.php
2025-12-21 10:09:54 -05:00

146 lines
2.8 KiB
PHP

<?php
declare(strict_types=1);
namespace KTXF\Event;
/**
* Base event class for the event bus system
*/
class Event
{
private bool $propagationStopped = false;
private array $data = [];
private float $timestamp;
private ?string $tenantId = null;
private ?string $identityId = null;
public function __construct(
private readonly string $name,
array $data = []
) {
$this->data = $data;
$this->timestamp = microtime(true);
}
/**
* Get the event name
*/
public function getName(): string
{
return $this->name;
}
/**
* Get a data value by key
*/
public function get(string $key, mixed $default = null): mixed
{
return $this->data[$key] ?? $default;
}
/**
* Set a data value
*/
public function set(string $key, mixed $value): self
{
$this->data[$key] = $value;
return $this;
}
/**
* Check if a data key exists
*/
public function has(string $key): bool
{
return array_key_exists($key, $this->data);
}
/**
* Get all data
*/
public function getData(): array
{
return $this->data;
}
/**
* Alias for getData() for backward compatibility
*/
public function all(): array
{
return $this->data;
}
/**
* Get the event timestamp
*/
public function getTimestamp(): float
{
return $this->timestamp;
}
/**
* Stop event propagation to subsequent listeners
*/
public function stopPropagation(): void
{
$this->propagationStopped = true;
}
/**
* Check if propagation is stopped
*/
public function isPropagationStopped(): bool
{
return $this->propagationStopped;
}
/**
* Get tenant ID for multi-tenant context
*/
public function getTenantId(): ?string
{
return $this->tenantId;
}
/**
* Set tenant ID for multi-tenant context
*/
public function setTenantId(?string $tenantId): self
{
$this->tenantId = $tenantId;
return $this;
}
/**
* Get identity ID (user who triggered the event)
*/
public function getIdentityId(): ?string
{
return $this->identityId;
}
/**
* Set identity ID
*/
public function setIdentityId(?string $identityId): self
{
$this->identityId = $identityId;
return $this;
}
/**
* Convert event to array for serialization/logging
*/
public function toArray(): array
{
return [
'name' => $this->name,
'data' => $this->data,
'timestamp' => $this->timestamp,
'tenantId' => $this->tenantId,
'identityId' => $this->identityId,
];
}
}