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