Skip to content

Commit

Permalink
Add typehinting to PicqerWebhook
Browse files Browse the repository at this point in the history
  • Loading branch information
casperbakker committed Aug 14, 2024
1 parent cee5974 commit bfb019f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
2 changes: 1 addition & 1 deletion examples/retrieveWebhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
$webhook = Picqer\Api\PicqerWebhook::retrieve();

echo 'Hook received: ' . $webhook->getName() . ' that was triggered at ' . $webhook->getEventTriggeredAt() . PHP_EOL;
echo $webhook->getData();
echo var_dump($webhook->getData());
34 changes: 18 additions & 16 deletions src/PicqerWebhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
*/
class PicqerWebhook
{
protected $idhook;
protected $name;
protected $event;
protected $data;
protected $event_triggered_at;
protected $rawPayload;
protected int $idhook;
protected string $name;
protected string $event;
protected array $data;
protected string $event_triggered_at;
protected array $rawPayload;

public function __construct($webhookPayload)
public function __construct(array $webhookPayload)
{
$this->rawPayload = $webhookPayload;

Expand All @@ -30,7 +30,7 @@ public function __construct($webhookPayload)
}
}

public static function retrieve()
public static function retrieve(): PicqerWebhook
{
$webhookPayloadRaw = file_get_contents('php://input');

Expand All @@ -43,7 +43,7 @@ public static function retrieve()
return new self($webhookPayloadDecoded);
}

public static function retrieveWithSecret($secret)
public static function retrieveWithSecret($secret): PicqerWebhook
{
if (! isset($_SERVER) || ! array_key_exists('HTTP_X_PICQER_SIGNATURE', $_SERVER)) {
throw new WebhookSignatureMismatchException('Could not find signature header in webhook');
Expand All @@ -58,31 +58,33 @@ public static function retrieveWithSecret($secret)
if (! hash_equals($calculatedSignature, $signatureHeader)) {
throw new WebhookSignatureMismatchException('Signatures do not match');
}

return self::retrieve();

$webhookPayloadDecoded = json_decode($webhookPayloadRaw, true);

return new self($webhookPayloadDecoded);
}

public function getIdhook()
public function getIdhook(): int
{
return $this->idhook;
}

public function getName()
public function getName(): string
{
return $this->name;
}

public function getEvent()
public function getEvent(): string
{
return $this->event;
}

public function getData()
public function getData(): array
{
return $this->data;
}

public function getEventTriggeredAt()
public function getEventTriggeredAt(): string
{
return $this->event_triggered_at;
}
Expand Down

0 comments on commit bfb019f

Please sign in to comment.