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

fix!: Privatise entities and improve typing #40

Merged
merged 1 commit into from
Feb 20, 2024
Merged
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
5 changes: 1 addition & 4 deletions src/Entities/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@

class Address implements Entity
{
/**
* @internal
*/
protected function __construct(
private function __construct(
public string $id,
public string|null $description,
public string|null $firstLine,
Expand Down
4 changes: 1 addition & 3 deletions src/Entities/Adjustment.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@
class Adjustment implements Entity
{
/**
* @internal
*
* @param array<AdjustmentItem> $items
*/
protected function __construct(
private function __construct(
public string $id,
public Action $action,
public string $transactionId,
Expand Down
2 changes: 1 addition & 1 deletion src/Entities/Adjustment/AdjustmentCustomerBalance.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

class AdjustmentCustomerBalance
{
public function __construct(
private function __construct(
public string $available,
public string $reserved,
public string $used,
Expand Down
2 changes: 1 addition & 1 deletion src/Entities/Adjustment/AdjustmentItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

class AdjustmentItem
{
public function __construct(
private function __construct(
public string $id,
public string $itemId,
public AdjustmentType $type,
Expand Down
4 changes: 1 addition & 3 deletions src/Entities/Business.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@
class Business implements Entity
{
/**
* @internal
*
* @param array<Contacts> $contacts
*/
protected function __construct(
private function __construct(
public string $id,
public string $name,
public string|null $companyNumber,
Expand Down
21 changes: 0 additions & 21 deletions src/Entities/Business/BusinessesContacts.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/Entities/Collections/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ abstract class Collection implements \Iterator
{
private int $pointer = 0;

public function __construct(
protected function __construct(
protected array $items,
protected Paginator|null $paginator = null,
) {
Expand Down
5 changes: 1 addition & 4 deletions src/Entities/CreditBalance.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@

class CreditBalance implements Entity
{
/**
* @internal
*/
protected function __construct(
private function __construct(
public string $customerId,
public CurrencyCode $currencyCode,
public AdjustmentCustomerBalance $balance,
Expand Down
5 changes: 1 addition & 4 deletions src/Entities/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@

class Customer implements Entity
{
/**
* @internal
*/
protected function __construct(
private function __construct(
public string $id,
public string|null $name,
public string $email,
Expand Down
2 changes: 1 addition & 1 deletion src/Entities/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class DateTime extends \DateTimeImmutable
{
final public const PADDLE_RFC3339 = 'Y-m-d\TH:i:s.up';

public function __construct(string $datetime = 'now')
private function __construct(string $datetime = 'now')
{
// Ensure formatted dates are in UTC
parent::__construct(datetime: $datetime, timezone: new \DateTimeZone('UTC'));
Expand Down
5 changes: 1 addition & 4 deletions src/Entities/Discount.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@

class Discount implements Entity
{
/**
* @internal
*/
protected function __construct(
private function __construct(
public string $id,
public DiscountStatus $status,
public string $description,
Expand Down
2 changes: 1 addition & 1 deletion src/Entities/Discount/DiscountStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* @method static DiscountStatus Expired()
* @method static DiscountStatus Used()
*/
class DiscountStatus extends PaddleEnum
final class DiscountStatus extends PaddleEnum
{
private const Active = 'active';
private const Archived = 'archived';
Expand Down
2 changes: 1 addition & 1 deletion src/Entities/Discount/DiscountType.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* @method static DiscountType FlatPerSeat()
* @method static DiscountType Percentage()
*/
class DiscountType extends PaddleEnum
final class DiscountType extends PaddleEnum
{
private const Flat = 'flat';
private const FlatPerSeat = 'flat_per_seat';
Expand Down
26 changes: 21 additions & 5 deletions src/Entities/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Paddle\SDK\Entities\Event\EventTypeName;
use Paddle\SDK\Notifications\Entities\Entity as NotificationEntity;

class Event implements Entity
abstract class Event implements Entity
{
/**
* @internal
Expand All @@ -22,20 +22,36 @@ protected function __construct(

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

/** @var class-string<Event> $entity */
$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($type));
$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 '{$type}' cannot be mapped to an object");
throw new \UnexpectedValueException("Event type '{$identifier}' cannot be mapped to an object");
}

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

abstract public static function fromEvent(
string $eventId,
EventTypeName $eventType,
\DateTimeInterface $occurredAt,
NotificationEntity $data,
): static;
}
2 changes: 1 addition & 1 deletion src/Entities/Event/EventTypeName.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
* @method static EventTypeName ReportCreated()
* @method static EventTypeName ReportUpdated()
*/
class EventTypeName extends PaddleEnum
final class EventTypeName extends PaddleEnum
{
private const AddressCreated = 'address.created';
private const AddressImported = 'address.imported';
Expand Down
5 changes: 1 addition & 4 deletions src/Entities/EventType.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@

class EventType implements Entity
{
/**
* @internal
*/
protected function __construct(
private function __construct(
public EventTypeName $name,
public string $description,
public string $group,
Expand Down
5 changes: 1 addition & 4 deletions src/Entities/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@

class Notification implements Entity
{
/**
* @internal
*/
protected function __construct(
private function __construct(
public string $id,
public EventTypeName $type,
public NotificationStatus $status,
Expand Down
2 changes: 1 addition & 1 deletion src/Entities/Notification/NotificationOrigin.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* @method static NotificationOrigin Event()
* @method static NotificationOrigin Replay()
*/
class NotificationOrigin extends PaddleEnum
final class NotificationOrigin extends PaddleEnum
{
private const Event = 'event';
private const Replay = 'replay';
Expand Down
2 changes: 1 addition & 1 deletion src/Entities/Notification/NotificationPayout.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class NotificationPayout implements Entity
{
public function __construct(
private function __construct(
public string $id,
public NotificationPayoutStatus $status,
public string $amount,
Expand Down
2 changes: 1 addition & 1 deletion src/Entities/Notification/NotificationPayoutStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* @method static NotificationPayoutStatus Unpaid()
* @method static NotificationPayoutStatus Paid()
*/
class NotificationPayoutStatus extends PaddleEnum
final class NotificationPayoutStatus extends PaddleEnum
{
private const Unpaid = 'unpaid';
private const Paid = 'paid';
Expand Down
2 changes: 1 addition & 1 deletion src/Entities/Notification/NotificationStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* @method static NotificationStatus Delivered()
* @method static NotificationStatus Failed()
*/
class NotificationStatus extends PaddleEnum
final class NotificationStatus extends PaddleEnum
{
private const NotAttempted = 'not_attempted';
private const NeedsRetry = 'needs_retry';
Expand Down
5 changes: 1 addition & 4 deletions src/Entities/NotificationLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@

class NotificationLog implements Entity
{
/**
* @internal
*/
protected function __construct(
private function __construct(
public string $id,
public int $responseCode,
public string|null $responseContentType,
Expand Down
4 changes: 1 addition & 3 deletions src/Entities/NotificationSetting.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@
class NotificationSetting implements Entity
{
/**
* @internal
*
* @param array<EventType> $subscribedEvents
*/
protected function __construct(
private function __construct(
public string $id,
public string $description,
public NotificationSettingType $type,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* @method static NotificationSettingType Email()
* @method static NotificationSettingType Url()
*/
class NotificationSettingType extends PaddleEnum
final class NotificationSettingType extends PaddleEnum
{
private const Email = 'email';
private const Url = 'url';
Expand Down
4 changes: 1 addition & 3 deletions src/Entities/Price.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,9 @@
class Price implements Entity
{
/**
* @internal
*
* @param array<UnitPriceOverride> $unitPriceOverrides
*/
protected function __construct(
private function __construct(
public string $id,
public string $productId,
public string|null $name,
Expand Down
4 changes: 1 addition & 3 deletions src/Entities/PricePreview.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@
class PricePreview implements Entity
{
/**
* @internal
*
* @param array<AvailablePaymentMethods> $availablePaymentMethods
*/
protected function __construct(
private function __construct(
public string|null $customerId,
public string|null $addressId,
public string|null $businessId,
Expand Down
2 changes: 1 addition & 1 deletion src/Entities/PricingPreview/PricePreviewDetails.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class PricePreviewDetails
/**
* @param array<PricePreviewLineItem> $lineItems
*/
public function __construct(
private function __construct(
public array $lineItems,
) {
}
Expand Down
2 changes: 1 addition & 1 deletion src/Entities/PricingPreview/PricePreviewDiscounts.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

class PricePreviewDiscounts
{
public function __construct(
private function __construct(
public Discount $discount,
public string $total,
public string $formattedTotal,
Expand Down
2 changes: 1 addition & 1 deletion src/Entities/PricingPreview/PricePreviewLineItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class PricePreviewLineItem
/**
* @param PricePreviewDiscounts[] $discounts
*/
public function __construct(
private function __construct(
public Price $price,
public int $quantity,
public string $taxRate,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

class PricePreviewTotalsFormatted
{
public function __construct(
private function __construct(
public string $subtotal,
public string $discount,
public string $tax,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

class PricePreviewUnitTotalsFormatted
{
public function __construct(
private function __construct(
public string $subtotal,
public string $discount,
public string $tax,
Expand Down
4 changes: 1 addition & 3 deletions src/Entities/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@
class Product implements Entity
{
/**
* @internal
*
* @param array<Price> $prices
*/
protected function __construct(
private function __construct(
public string $id,
public string $name,
public string|null $description,
Expand Down
4 changes: 1 addition & 3 deletions src/Entities/Report.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@
class Report implements Entity
{
/**
* @internal
*
* @param array<ReportFilter> $filters
*/
protected function __construct(
private function __construct(
public string $id,
public ReportStatus $status,
public int|null $rows,
Expand Down
Loading
Loading