diff --git a/psalm-baseline.xml b/psalm-baseline.xml index d62d21a..4a8f159 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -1,15 +1,6 @@ - - DateTimeImmutable::createFromFormat('U', (string) $this->received, new DateTimeZone('UTC')) - - - DateTimeImmutable - - - $serialized - $this->payload $this->received diff --git a/src/Event/WebhookEvent.php b/src/Event/WebhookEvent.php index 864b084..0e95cc3 100644 --- a/src/Event/WebhookEvent.php +++ b/src/Event/WebhookEvent.php @@ -6,6 +6,7 @@ use DateTimeImmutable; use DateTimeZone; +use Primo\Exception\RuntimeError; use Prismic\Json; use Serializable; @@ -26,12 +27,17 @@ private function __construct(object $payload) public static function new(object $payload): self { - return new static($payload); + return new self($payload); } public function received(): DateTimeImmutable { - return DateTimeImmutable::createFromFormat('U', (string) $this->received, new DateTimeZone('UTC')); + $date = DateTimeImmutable::createFromFormat('U', (string) $this->received, new DateTimeZone('UTC')); + if ($date === false) { + throw new RuntimeError('Webhook event time evaluated to false'); + } + + return $date; } public function payload(): object @@ -39,6 +45,9 @@ public function payload(): object return $this->payload; } + /** + * @deprecated + */ public function serialize(): string { return Json::encode([ @@ -47,11 +56,37 @@ public function serialize(): string ]); } - /** @param mixed $serialized */ + /** + * @deprecated + * + * @param string $serialized + * + * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint + */ public function unserialize($serialized): void { $object = Json::decodeObject($serialized); $this->payload = $object->payload; $this->received = $object->received; } + + /** + * @return array{received: int, payload: string} + */ + public function __serialize(): array + { + return [ + 'received' => $this->received, + 'payload' => Json::encode($this->payload), + ]; + } + + /** + * @param array{received: int, payload: string} $data + */ + public function __unserialize(array $data): void + { + $this->received = $data['received']; + $this->payload = Json::decodeObject($data['payload']); + } }