. */ namespace SP\Core\Events; use SP\Domain\Core\Exceptions\InvalidClassException; /** * Class Event */ readonly class Event { public function __construct( private object $source, private ?EventMessage $eventMessage = null ) { } /** * @template T of object * * @param class-string|null $type * @return T&object * @throws InvalidClassException */ public function getSource(?string $type = null): object { if ($type !== null && !is_a($this->source, $type)) { throw InvalidClassException::error( 'Source type mismatch', sprintf('Source: %s - Expected: %s', get_class($this->source), $type) ); } return $this->source; } public function getEventMessage(): ?EventMessage { return $this->eventMessage; } }