Skip to content

Commit

Permalink
Feature: Create notification types
Browse files Browse the repository at this point in the history
  • Loading branch information
davidgrayston-paddle committed Sep 13, 2024
1 parent 3ae4fa3 commit fc59e37
Show file tree
Hide file tree
Showing 40 changed files with 1,157 additions and 27 deletions.
13 changes: 6 additions & 7 deletions examples/webhook_verification_PSR7.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
*/

use GuzzleHttp\Psr7\ServerRequest;
use Paddle\SDK\Notifications\Events\TransactionUpdated;
use Paddle\SDK\Notifications\Notification;
use Paddle\SDK\Notifications\Notification\TransactionUpdatedNotification;
use Paddle\SDK\Notifications\Secret;
use Paddle\SDK\Notifications\Verifier;

Expand All @@ -26,13 +26,12 @@

$notification = Notification::fromRequest($request);
$id = $notification->id;
$event = $notification->event;
$eventId = $event->eventId;
$eventType = $event->eventType;
$occurredAt = $event->occurredAt;
$eventId = $notification->eventId;
$eventType = $notification->eventType;
$occurredAt = $notification->occurredAt;

if ($event instanceof TransactionUpdated) {
$transactionId = $event->transaction->id;
if ($notification instanceof TransactionUpdatedNotification) {
$transactionId = $notification->transaction->id;
}
} else {
echo "Webhook is not verified\n";
Expand Down
45 changes: 34 additions & 11 deletions src/Notifications/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@
namespace Paddle\SDK\Notifications;

use Paddle\SDK\Entities\Event;
use Paddle\SDK\Entities\Event\EventTypeName;
use Paddle\SDK\Notifications\Entities\Entity;
use Psr\Http\Message\ServerRequestInterface;

class Notification implements Entity
abstract class Notification implements Entity
{
private function __construct(
public readonly string $eventId;
public readonly EventTypeName $eventType;
public readonly \DateTimeInterface $occurredAt;

protected function __construct(
public readonly string $id,
public readonly Event $event,
Event $event,
) {
}

public static function from(array $data): self
{
return new self(
$data['notification_id'],
Event::from($data),
);
$this->eventId = $event->eventId;
$this->eventType = $event->eventType;
$this->occurredAt = $event->occurredAt;
}

public static function fromRequest(ServerRequestInterface $request): self
Expand All @@ -32,4 +32,27 @@ public static function fromRequest(ServerRequestInterface $request): self
JSON_THROW_ON_ERROR,
));
}

public static function from(array $data): self
{
$type = explode('.', (string) $data['event_type']);
$identifier = str_replace('_', '', ucwords(implode('_', $type), '_'));

/** @var class-string<Notification> $notification */
$notification = sprintf('\Paddle\SDK\Notifications\Notification\%sNotification', $identifier);

if (! class_exists($notification) || ! is_subclass_of($notification, self::class)) {
throw new \UnexpectedValueException("Notification type '{$identifier}' cannot be mapped to an object");
}

return $notification::fromEvent(
$data['notification_id'],
Event::from($data),
);
}

abstract protected static function fromEvent(
string $id,
Event $event,
): static;
}
30 changes: 30 additions & 0 deletions src/Notifications/Notification/AddressCreatedNotification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Paddle\SDK\Notifications\Notification;

use Paddle\SDK\Entities\Event;
use Paddle\SDK\Notifications\Entities\Address;
use Paddle\SDK\Notifications\Events\AddressCreated;
use Paddle\SDK\Notifications\Notification;

final class AddressCreatedNotification extends Notification
{
public readonly Address $address;

private function __construct(string $id, AddressCreated $event)
{
$this->address = $event->address;

parent::__construct($id, $event);
}

/**
* @param AddressCreated $event
*/
protected static function fromEvent(string $id, Event $event): static
{
return new self($id, $event);
}
}
30 changes: 30 additions & 0 deletions src/Notifications/Notification/AddressUpdatedNotification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Paddle\SDK\Notifications\Notification;

use Paddle\SDK\Entities\Event;
use Paddle\SDK\Notifications\Entities\Address;
use Paddle\SDK\Notifications\Events\AddressUpdated;
use Paddle\SDK\Notifications\Notification;

final class AddressUpdatedNotification extends Notification
{
public readonly Address $address;

private function __construct(string $id, AddressUpdated $event)
{
$this->address = $event->address;

parent::__construct($id, $event);
}

/**
* @param AddressUpdated $event
*/
protected static function fromEvent(string $id, Event $event): static
{
return new self($id, $event);
}
}
30 changes: 30 additions & 0 deletions src/Notifications/Notification/AdjustmentCreatedNotification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Paddle\SDK\Notifications\Notification;

use Paddle\SDK\Entities\Event;
use Paddle\SDK\Notifications\Entities\Adjustment;
use Paddle\SDK\Notifications\Events\AdjustmentCreated;
use Paddle\SDK\Notifications\Notification;

final class AdjustmentCreatedNotification extends Notification
{
public readonly Adjustment $adjustment;

private function __construct(string $id, AdjustmentCreated $event)
{
$this->adjustment = $event->adjustment;

parent::__construct($id, $event);
}

/**
* @param AdjustmentCreated $event
*/
protected static function fromEvent(string $id, Event $event): static
{
return new self($id, $event);
}
}
30 changes: 30 additions & 0 deletions src/Notifications/Notification/AdjustmentUpdatedNotification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Paddle\SDK\Notifications\Notification;

use Paddle\SDK\Entities\Event;
use Paddle\SDK\Notifications\Entities\Adjustment;
use Paddle\SDK\Notifications\Events\AdjustmentUpdated;
use Paddle\SDK\Notifications\Notification;

final class AdjustmentUpdatedNotification extends Notification
{
public readonly Adjustment $adjustment;

private function __construct(string $id, AdjustmentUpdated $event)
{
$this->adjustment = $event->adjustment;

parent::__construct($id, $event);
}

/**
* @param AdjustmentUpdated $event
*/
protected static function fromEvent(string $id, Event $event): static
{
return new self($id, $event);
}
}
30 changes: 30 additions & 0 deletions src/Notifications/Notification/BusinessCreatedNotification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Paddle\SDK\Notifications\Notification;

use Paddle\SDK\Entities\Event;
use Paddle\SDK\Notifications\Entities\Business;
use Paddle\SDK\Notifications\Events\BusinessCreated;
use Paddle\SDK\Notifications\Notification;

final class BusinessCreatedNotification extends Notification
{
public readonly Business $business;

private function __construct(string $id, BusinessCreated $event)
{
$this->business = $event->business;

parent::__construct($id, $event);
}

/**
* @param BusinessCreated $event
*/
protected static function fromEvent(string $id, Event $event): static
{
return new self($id, $event);
}
}
30 changes: 30 additions & 0 deletions src/Notifications/Notification/BusinessUpdatedNotification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Paddle\SDK\Notifications\Notification;

use Paddle\SDK\Entities\Event;
use Paddle\SDK\Notifications\Entities\Business;
use Paddle\SDK\Notifications\Events\BusinessUpdated;
use Paddle\SDK\Notifications\Notification;

final class BusinessUpdatedNotification extends Notification
{
public readonly Business $business;

private function __construct(string $id, BusinessUpdated $event)
{
$this->business = $event->business;

parent::__construct($id, $event);
}

/**
* @param BusinessUpdated $event
*/
protected static function fromEvent(string $id, Event $event): static
{
return new self($id, $event);
}
}
30 changes: 30 additions & 0 deletions src/Notifications/Notification/CustomerCreatedNotification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Paddle\SDK\Notifications\Notification;

use Paddle\SDK\Entities\Event;
use Paddle\SDK\Notifications\Entities\Customer;
use Paddle\SDK\Notifications\Events\CustomerCreated;
use Paddle\SDK\Notifications\Notification;

final class CustomerCreatedNotification extends Notification
{
public readonly Customer $customer;

private function __construct(string $id, CustomerCreated $event)
{
$this->customer = $event->customer;

parent::__construct($id, $event);
}

/**
* @param CustomerCreated $event
*/
protected static function fromEvent(string $id, Event $event): static
{
return new self($id, $event);
}
}
30 changes: 30 additions & 0 deletions src/Notifications/Notification/CustomerUpdatedNotification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Paddle\SDK\Notifications\Notification;

use Paddle\SDK\Entities\Event;
use Paddle\SDK\Notifications\Entities\Customer;
use Paddle\SDK\Notifications\Events\CustomerUpdated;
use Paddle\SDK\Notifications\Notification;

final class CustomerUpdatedNotification extends Notification
{
public readonly Customer $customer;

private function __construct(string $id, CustomerUpdated $event)
{
$this->customer = $event->customer;

parent::__construct($id, $event);
}

/**
* @param CustomerUpdated $event
*/
protected static function fromEvent(string $id, Event $event): static
{
return new self($id, $event);
}
}
30 changes: 30 additions & 0 deletions src/Notifications/Notification/DiscountCreatedNotification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Paddle\SDK\Notifications\Notification;

use Paddle\SDK\Entities\Event;
use Paddle\SDK\Notifications\Entities\Discount;
use Paddle\SDK\Notifications\Events\DiscountCreated;
use Paddle\SDK\Notifications\Notification;

final class DiscountCreatedNotification extends Notification
{
public readonly Discount $discount;

private function __construct(string $id, DiscountCreated $event)
{
$this->discount = $event->discount;

parent::__construct($id, $event);
}

/**
* @param DiscountCreated $event
*/
protected static function fromEvent(string $id, Event $event): static
{
return new self($id, $event);
}
}
Loading

0 comments on commit fc59e37

Please sign in to comment.