Skip to content

Commit

Permalink
Have notifications extend Event to return on notifcation endpoint in …
Browse files Browse the repository at this point in the history
…non-breaking way
  • Loading branch information
davidgrayston-paddle committed Sep 16, 2024
1 parent fc59e37 commit 9bc5ce9
Show file tree
Hide file tree
Showing 83 changed files with 1,103 additions and 577 deletions.
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,
'nullable_type_declaration' => ['syntax' => 'union'],
'ordered_types' => [
'null_adjustment' => 'always_last',
Expand Down
6 changes: 3 additions & 3 deletions examples/webhook_verification_PSR7.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/

use GuzzleHttp\Psr7\ServerRequest;
use Paddle\SDK\Notifications\Notification;
use Paddle\SDK\Entities\Event;
use Paddle\SDK\Notifications\Notification\TransactionUpdatedNotification;
use Paddle\SDK\Notifications\Secret;
use Paddle\SDK\Notifications\Verifier;
Expand All @@ -24,8 +24,8 @@
if ($isVerified) {
echo "Webhook is verified\n";

$notification = Notification::fromRequest($request);
$id = $notification->id;
$notification = Event::notificationFromRequest($request);
$notification_id = $notification->getNotificationId();
$eventId = $notification->eventId;
$eventType = $notification->eventType;
$occurredAt = $notification->occurredAt;
Expand Down
65 changes: 57 additions & 8 deletions src/Entities/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

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
{
Expand All @@ -17,38 +19,85 @@ protected function __construct(
) {
}

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
6 changes: 3 additions & 3 deletions src/Notifications/Events/AddressCreated.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
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,
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);
}
}
6 changes: 3 additions & 3 deletions src/Notifications/Events/AddressUpdated.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
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,
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);
}
}
6 changes: 3 additions & 3 deletions src/Notifications/Events/AdjustmentCreated.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
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,
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);
}
}
6 changes: 3 additions & 3 deletions src/Notifications/Events/AdjustmentUpdated.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
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,
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);
}
}
6 changes: 3 additions & 3 deletions src/Notifications/Events/BusinessCreated.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
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,
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);
}
}
6 changes: 3 additions & 3 deletions src/Notifications/Events/BusinessUpdated.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
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,
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);
}
}
6 changes: 3 additions & 3 deletions src/Notifications/Events/CustomerCreated.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
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,
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);
}
}
6 changes: 3 additions & 3 deletions src/Notifications/Events/CustomerUpdated.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
use Paddle\SDK\Notifications\Entities\Customer;
use Paddle\SDK\Notifications\Entities\Entity;

final class CustomerUpdated extends Event
class CustomerUpdated extends Event
{
private function __construct(
protected function __construct(
string $eventId,
EventTypeName $eventType,
\DateTimeInterface $occurredAt,
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);
}
}
6 changes: 3 additions & 3 deletions src/Notifications/Events/DiscountCreated.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
use Paddle\SDK\Notifications\Entities\Discount;
use Paddle\SDK\Notifications\Entities\Entity;

final class DiscountCreated extends Event
class DiscountCreated extends Event
{
private function __construct(
protected function __construct(
string $eventId,
EventTypeName $eventType,
\DateTimeInterface $occurredAt,
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);
}
}
6 changes: 3 additions & 3 deletions src/Notifications/Events/DiscountImported.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
use Paddle\SDK\Notifications\Entities\Discount;
use Paddle\SDK\Notifications\Entities\Entity;

final class DiscountImported extends Event
class DiscountImported extends Event
{
private function __construct(
protected function __construct(
string $eventId,
EventTypeName $eventType,
\DateTimeInterface $occurredAt,
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);
}
}
6 changes: 3 additions & 3 deletions src/Notifications/Events/DiscountUpdated.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
use Paddle\SDK\Notifications\Entities\Discount;
use Paddle\SDK\Notifications\Entities\Entity;

final class DiscountUpdated extends Event
class DiscountUpdated extends Event
{
private function __construct(
protected function __construct(
string $eventId,
EventTypeName $eventType,
\DateTimeInterface $occurredAt,
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

0 comments on commit 9bc5ce9

Please sign in to comment.