Skip to content

Commit

Permalink
Merge pull request #71 from netglue/webhook-event-serialisable
Browse files Browse the repository at this point in the history
Event Serialisation Fix for 8.1
  • Loading branch information
gsteel committed Jan 31, 2022
2 parents aadff44 + db2be55 commit 152a883
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 12 deletions.
9 changes: 0 additions & 9 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="4.19.0@a2ad69ae4f5ab1f7d225a8dc4e2ec2d9415ed599">
<file src="src/Event/WebhookEvent.php">
<FalsableReturnStatement occurrences="1">
<code>DateTimeImmutable::createFromFormat('U', (string) $this-&gt;received, new DateTimeZone('UTC'))</code>
</FalsableReturnStatement>
<InvalidFalsableReturnType occurrences="1">
<code>DateTimeImmutable</code>
</InvalidFalsableReturnType>
<MixedArgument occurrences="1">
<code>$serialized</code>
</MixedArgument>
<MixedAssignment occurrences="2">
<code>$this-&gt;payload</code>
<code>$this-&gt;received</code>
Expand Down
41 changes: 38 additions & 3 deletions src/Event/WebhookEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use DateTimeImmutable;
use DateTimeZone;
use Primo\Exception\RuntimeError;
use Prismic\Json;
use Serializable;

Expand All @@ -26,19 +27,27 @@ 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
{
return $this->payload;
}

/**
* @deprecated
*/
public function serialize(): string
{
return Json::encode([
Expand All @@ -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']);
}
}

0 comments on commit 152a883

Please sign in to comment.