Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Event notification sub types #75

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
'concat_space' => [
'spacing' => 'one',
],
'self_accessor' => true,
// Disable self_accessor to allow self intersection return types.
// 'self_accessor' => true,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prevents

- public static function notificationFrom(array $data): Event&NotificationInterface
+ public static function notificationFrom(array $data): self&NotificationInterface

'nullable_type_declaration' => ['syntax' => 'union'],
'ordered_types' => [
'null_adjustment' => 'always_last',
Expand Down
12 changes: 12 additions & 0 deletions examples/webhook_verification_PSR7.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
*/

use GuzzleHttp\Psr7\ServerRequest;
use Paddle\SDK\Entities\Event;
use Paddle\SDK\Notifications\Notification\TransactionUpdatedNotification;
use Paddle\SDK\Notifications\Secret;
use Paddle\SDK\Notifications\Verifier;

Expand All @@ -21,6 +23,16 @@

if ($isVerified) {
echo "Webhook is verified\n";

$notification = Event::notificationFromRequest($request);
$notification_id = $notification->getNotificationId();
$eventId = $notification->eventId;
$eventType = $notification->eventType;
$occurredAt = $notification->occurredAt;

if ($notification instanceof TransactionUpdatedNotification) {
$transactionId = $notification->transaction->id;
}
} else {
echo "Webhook is not verified\n";
}
76 changes: 61 additions & 15 deletions src/Entities/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,52 +6,98 @@

use Paddle\SDK\Entities\Event\EventTypeName;
use Paddle\SDK\Notifications\Entities\Entity as NotificationEntity;
use Paddle\SDK\Notifications\Notification\NotificationInterface;
use Psr\Http\Message\ServerRequestInterface;

abstract class Event implements Entity
{
/**
* @internal
*/
protected function __construct(
public string $eventId,
public EventTypeName $eventType,
public \DateTimeInterface $occurredAt,
public NotificationEntity $data,
public readonly string $eventId,
public readonly EventTypeName $eventType,
public readonly \DateTimeInterface $occurredAt,
public readonly NotificationEntity $data,
) {
}

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

/** @var class-string<Event> $entity */
/** @var class-string<NotificationEntity> $entity */
$entity = sprintf('\Paddle\SDK\Notifications\Entities\%s', ucfirst($entity));

if (! class_exists($entity) || ! in_array(NotificationEntity::class, class_implements($entity), true)) {
throw new \UnexpectedValueException("Event type '{$identifier}' cannot be mapped to an object");
}

return $entity::from($data['data']);
}

public static function identifierFrom(array $data): string
{
$type = explode('.', (string) $data['event_type']);

return str_replace('_', '', ucwords(implode('_', $type), '_'));
}

public static function from(array $data): self
{
$identifier = self::identifierFrom($data);

/** @var class-string<Event> $event */
$event = sprintf('\Paddle\SDK\Notifications\Events\%s', $identifier);

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

/** @var class-string<NotificationEntity> $entity */
$entity = sprintf('\Paddle\SDK\Notifications\Entities\%s', ucfirst($entity));
return $event::fromEvent(
$data['event_id'],
EventTypeName::from($data['event_type']),
DateTime::from($data['occurred_at']),
self::entityFrom($data),
);
}

if (! class_exists($entity) || ! in_array(NotificationEntity::class, class_implements($entity), true)) {
public static function notificationFrom(array $data): Event&NotificationInterface
{
$identifier = self::identifierFrom($data);

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

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

return $event::fromEvent(
return $notification::fromNotification(
$data['event_id'],
EventTypeName::from($data['event_type']),
DateTime::from($data['occurred_at']),
$entity::from($data['data']),
self::entityFrom($data),
$data['notification_id'],
);
}

public static function notificationFromRequest(ServerRequestInterface $request): Event&NotificationInterface
{
return self::notificationFrom(json_decode(
(string) $request->getBody(),
true,
JSON_THROW_ON_ERROR,
));
}

abstract public static function fromEvent(
string $eventId,
EventTypeName $eventType,
\DateTimeInterface $occurredAt,
NotificationEntity $data,
): static;
): self;
}
5 changes: 3 additions & 2 deletions src/Entities/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
use Paddle\SDK\Entities\Event\EventTypeName;
use Paddle\SDK\Entities\Notification\NotificationOrigin;
use Paddle\SDK\Entities\Notification\NotificationStatus;
use Paddle\SDK\Notifications\Notification\NotificationInterface;

class Notification implements Entity
{
private function __construct(
public string $id,
public EventTypeName $type,
public NotificationStatus $status,
public Event $payload,
public Event&NotificationInterface $payload,
public \DateTimeInterface $occurredAt,
public \DateTimeInterface|null $deliveredAt,
public \DateTimeInterface|null $replayedAt,
Expand All @@ -32,7 +33,7 @@ public static function from(array $data): self
$data['id'],
EventTypeName::from($data['type']),
NotificationStatus::from($data['status']),
Event::from($data['payload']),
Event::notificationFrom($data['payload']),
DateTime::from($data['occurred_at']),
isset($data['delivered_at']) ? DateTime::from($data['delivered_at']) : null,
isset($data['replayed_at']) ? DateTime::from($data['replayed_at']) : null,
Expand Down
10 changes: 5 additions & 5 deletions src/Notifications/Events/AddressCreated.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
use Paddle\SDK\Notifications\Entities\Address;
use Paddle\SDK\Notifications\Entities\Entity;

final class AddressCreated extends Event
class AddressCreated extends Event
{
private function __construct(
protected function __construct(
string $eventId,
EventTypeName $eventType,
\DateTimeInterface $occurredAt,
Address $data,
public readonly Address $address,
) {
parent::__construct($eventId, $eventType, $occurredAt, $data);
parent::__construct($eventId, $eventType, $occurredAt, $address);
}

/**
Expand All @@ -28,7 +28,7 @@ public static function fromEvent(
EventTypeName $eventType,
\DateTimeInterface $occurredAt,
Entity $data,
): static {
): self {
return new self($eventId, $eventType, $occurredAt, $data);
}
}
10 changes: 5 additions & 5 deletions src/Notifications/Events/AddressUpdated.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
use Paddle\SDK\Notifications\Entities\Address;
use Paddle\SDK\Notifications\Entities\Entity;

final class AddressUpdated extends Event
class AddressUpdated extends Event
{
private function __construct(
protected function __construct(
string $eventId,
EventTypeName $eventType,
\DateTimeInterface $occurredAt,
Address $data,
public readonly Address $address,
) {
parent::__construct($eventId, $eventType, $occurredAt, $data);
parent::__construct($eventId, $eventType, $occurredAt, $address);
}

/**
Expand All @@ -28,7 +28,7 @@ public static function fromEvent(
EventTypeName $eventType,
\DateTimeInterface $occurredAt,
Entity $data,
): static {
): self {
return new self($eventId, $eventType, $occurredAt, $data);
}
}
10 changes: 5 additions & 5 deletions src/Notifications/Events/AdjustmentCreated.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
use Paddle\SDK\Notifications\Entities\Adjustment;
use Paddle\SDK\Notifications\Entities\Entity;

final class AdjustmentCreated extends Event
class AdjustmentCreated extends Event
{
private function __construct(
protected function __construct(
string $eventId,
EventTypeName $eventType,
\DateTimeInterface $occurredAt,
Adjustment $data,
public readonly Adjustment $adjustment,
) {
parent::__construct($eventId, $eventType, $occurredAt, $data);
parent::__construct($eventId, $eventType, $occurredAt, $adjustment);
}

/**
Expand All @@ -28,7 +28,7 @@ public static function fromEvent(
EventTypeName $eventType,
\DateTimeInterface $occurredAt,
Entity $data,
): static {
): self {
return new self($eventId, $eventType, $occurredAt, $data);
}
}
10 changes: 5 additions & 5 deletions src/Notifications/Events/AdjustmentUpdated.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
use Paddle\SDK\Notifications\Entities\Adjustment;
use Paddle\SDK\Notifications\Entities\Entity;

final class AdjustmentUpdated extends Event
class AdjustmentUpdated extends Event
{
private function __construct(
protected function __construct(
string $eventId,
EventTypeName $eventType,
\DateTimeInterface $occurredAt,
Adjustment $data,
public readonly Adjustment $adjustment,
) {
parent::__construct($eventId, $eventType, $occurredAt, $data);
parent::__construct($eventId, $eventType, $occurredAt, $adjustment);
}

/**
Expand All @@ -28,7 +28,7 @@ public static function fromEvent(
EventTypeName $eventType,
\DateTimeInterface $occurredAt,
Entity $data,
): static {
): self {
return new self($eventId, $eventType, $occurredAt, $data);
}
}
10 changes: 5 additions & 5 deletions src/Notifications/Events/BusinessCreated.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
use Paddle\SDK\Notifications\Entities\Business;
use Paddle\SDK\Notifications\Entities\Entity;

final class BusinessCreated extends Event
class BusinessCreated extends Event
{
private function __construct(
protected function __construct(
string $eventId,
EventTypeName $eventType,
\DateTimeInterface $occurredAt,
Business $data,
public readonly Business $business,
) {
parent::__construct($eventId, $eventType, $occurredAt, $data);
parent::__construct($eventId, $eventType, $occurredAt, $business);
}

/**
Expand All @@ -28,7 +28,7 @@ public static function fromEvent(
EventTypeName $eventType,
\DateTimeInterface $occurredAt,
Entity $data,
): static {
): self {
return new self($eventId, $eventType, $occurredAt, $data);
}
}
10 changes: 5 additions & 5 deletions src/Notifications/Events/BusinessUpdated.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
use Paddle\SDK\Notifications\Entities\Business;
use Paddle\SDK\Notifications\Entities\Entity;

final class BusinessUpdated extends Event
class BusinessUpdated extends Event
{
private function __construct(
protected function __construct(
string $eventId,
EventTypeName $eventType,
\DateTimeInterface $occurredAt,
Business $data,
public readonly Business $business,
) {
parent::__construct($eventId, $eventType, $occurredAt, $data);
parent::__construct($eventId, $eventType, $occurredAt, $business);
}

/**
Expand All @@ -28,7 +28,7 @@ public static function fromEvent(
EventTypeName $eventType,
\DateTimeInterface $occurredAt,
Entity $data,
): static {
): self {
return new self($eventId, $eventType, $occurredAt, $data);
}
}
10 changes: 5 additions & 5 deletions src/Notifications/Events/CustomerCreated.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
use Paddle\SDK\Notifications\Entities\Customer;
use Paddle\SDK\Notifications\Entities\Entity;

final class CustomerCreated extends Event
class CustomerCreated extends Event
{
private function __construct(
protected function __construct(
string $eventId,
EventTypeName $eventType,
\DateTimeInterface $occurredAt,
Customer $data,
public readonly Customer $customer,
) {
parent::__construct($eventId, $eventType, $occurredAt, $data);
parent::__construct($eventId, $eventType, $occurredAt, $customer);
}

/**
Expand All @@ -28,7 +28,7 @@ public static function fromEvent(
EventTypeName $eventType,
\DateTimeInterface $occurredAt,
Entity $data,
): static {
): self {
return new self($eventId, $eventType, $occurredAt, $data);
}
}
Loading
Loading