Skip to content

Commit

Permalink
Updated payload type
Browse files Browse the repository at this point in the history
  • Loading branch information
vifer committed Oct 8, 2024
1 parent dc2e660 commit c8e4011
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 40 deletions.
25 changes: 0 additions & 25 deletions src/Entities/Shared/JSONObject.php

This file was deleted.

7 changes: 4 additions & 3 deletions src/Entities/Simulation.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@

namespace Paddle\SDK\Entities;

use Paddle\SDK\Entities\Shared\JSONObject;
use Paddle\SDK\Entities\Simulation\SimulationScenarioType;
use Paddle\SDK\Entities\Simulation\SimulationSingleEventType;
use Paddle\SDK\Entities\Simulation\SimulationStatus;
use Paddle\SDK\Notifications\Entities\Entity as NotificationEntity;
use Paddle\SDK\Notifications\Entities\EntityFactory;

class Simulation implements Entity
{
Expand All @@ -17,7 +18,7 @@ private function __construct(
public string $notificationSettingId,
public string $name,
public SimulationSingleEventType|SimulationScenarioType $type,
public JSONObject|null $payload,
public NotificationEntity|null $payload,
public \DateTimeInterface|null $lastRunAt,
public \DateTimeInterface $createdAt,
public \DateTimeInterface $updatedAt,
Expand All @@ -32,7 +33,7 @@ public static function from(array $data): self
notificationSettingId: $data['notification_setting_id'],
name: $data['name'],
type: SimulationSingleEventType::from($data['type'])->isKnown() ? SimulationSingleEventType::from($data['type']) : SimulationScenarioType::from($data['type']),
payload: $data['payload'] ? new JSONObject($data['payload']) : null,
payload: $data['payload'] ? EntityFactory::create($data['type'], $data['payload']) : null,
lastRunAt: isset($data['last_run_at']) ? DateTime::from($data['last_run_at']) : null,
createdAt: DateTime::from($data['created_at']),
updatedAt: DateTime::from($data['updated_at']),
Expand Down
7 changes: 4 additions & 3 deletions src/Entities/SimulationRunEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@
namespace Paddle\SDK\Entities;

use Paddle\SDK\Entities\Event\EventTypeName;
use Paddle\SDK\Entities\Shared\JSONObject;
use Paddle\SDK\Entities\SimulationRunEvent\SimulationRunEventRequest;
use Paddle\SDK\Entities\SimulationRunEvent\SimulationRunEventResponse;
use Paddle\SDK\Entities\SimulationRunEvent\SimulationRunEventStatus;
use Paddle\SDK\Notifications\Entities\Entity as NotificationEntity;
use Paddle\SDK\Notifications\Entities\EntityFactory;

class SimulationRunEvent implements Entity
{
private function __construct(
public string $id,
public SimulationRunEventStatus $status,
public EventTypeName $type,
public JSONObject $payload,
public NotificationEntity $payload,
public SimulationRunEventRequest|null $request,
public SimulationRunEventResponse|null $response,
public \DateTimeInterface $createdAt,
Expand All @@ -30,7 +31,7 @@ public static function from(array $data): self
id: $data['id'],
status: SimulationRunEventStatus::from($data['status']),
type: EventTypeName::from($data['event_type']),
payload: new JSONObject($data['payload']),
payload: $data['payload'] ? EntityFactory::create($data['event_type'], $data['payload']) : null,
request: isset($data['request']) ? SimulationRunEventRequest::from($data['request']) : null,
response: isset($data['response']) ? SimulationRunEventResponse::from($data['response']) : null,
createdAt: DateTime::from($data['created_at']),
Expand Down
24 changes: 24 additions & 0 deletions src/Notifications/Entities/EntityFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Paddle\SDK\Notifications\Entities;

class EntityFactory
{
public static function create(string $eventType, array $data): Entity
{
$type = explode('.', $eventType);
$entity = $type[0] ?? 'Unknown';
$identifier = str_replace('_', '', ucwords(implode('_', $type), '_'));

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

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

return $entity::from($data);
}
}
4 changes: 2 additions & 2 deletions src/Resources/Simulations/Operations/CreateSimulation.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

namespace Paddle\SDK\Resources\Simulations\Operations;

use Paddle\SDK\Entities\Shared\JSONObject;
use Paddle\SDK\Entities\Simulation\SimulationScenarioType;
use Paddle\SDK\Entities\Simulation\SimulationSingleEventType;
use Paddle\SDK\FiltersUndefined;
use Paddle\SDK\Notifications\Entities\Entity as NotificationEntity;
use Paddle\SDK\Undefined;

class CreateSimulation implements \JsonSerializable
Expand All @@ -18,7 +18,7 @@ public function __construct(
public readonly string $notificationSettingId,
public readonly SimulationSingleEventType|SimulationScenarioType $type,
public readonly string $name,
public readonly JSONObject|Undefined $payload = new Undefined(),
public readonly NotificationEntity|Undefined $payload = new Undefined(),
) {
}

Expand Down
4 changes: 2 additions & 2 deletions src/Resources/Simulations/Operations/UpdateSimulation.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

namespace Paddle\SDK\Resources\Simulations\Operations;

use Paddle\SDK\Entities\Shared\JSONObject;
use Paddle\SDK\Entities\Simulation\SimulationScenarioType;
use Paddle\SDK\Entities\Simulation\SimulationSingleEventType;
use Paddle\SDK\Entities\Simulation\SimulationStatus;
use Paddle\SDK\FiltersUndefined;
use Paddle\SDK\Notifications\Entities\Entity as NotificationEntity;
use Paddle\SDK\Undefined;

class UpdateSimulation implements \JsonSerializable
Expand All @@ -20,7 +20,7 @@ public function __construct(
public readonly SimulationSingleEventType|SimulationScenarioType|Undefined $type = new Undefined(),
public readonly string|Undefined $name = new Undefined(),
public readonly SimulationStatus|Undefined $status = new Undefined(),
public readonly JSONObject|Undefined|null $payload = new Undefined(),
public readonly NotificationEntity|Undefined|null $payload = new Undefined(),
) {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
use GuzzleHttp\Psr7\Response;
use Http\Mock\Client as MockClient;
use Paddle\SDK\Client;
use Paddle\SDK\Entities\Shared\JSONObject;
use Paddle\SDK\Entities\Simulation\SimulationSingleEventType;
use Paddle\SDK\Entities\Simulation\SimulationStatus;
use Paddle\SDK\Environment;
use Paddle\SDK\Notifications\Entities\EntityFactory;
use Paddle\SDK\Options;
use Paddle\SDK\Resources\Shared\Operations\List\OrderBy;
use Paddle\SDK\Resources\Shared\Operations\List\Pager;
Expand Down Expand Up @@ -65,8 +65,7 @@ public static function createOperationsProvider(): \Generator
notificationSettingId: 'ntfset_01j82d983j814ypzx7m1fw2jpz',
type: SimulationSingleEventType::AddressCreated(),
name: 'New US address created for CRM',
payload: new JSONObject(json_decode(self::readRawJsonFixture('request/address_created_payload'), true),
),
payload: EntityFactory::create('address.created', json_decode(self::readRawJsonFixture('request/address_created_payload'), true)),
),
new Response(200, body: self::readRawJsonFixture('response/full_entity')),
self::readRawJsonFixture('request/create_basic'),
Expand Down Expand Up @@ -116,8 +115,7 @@ public static function updateOperationsProvider(): \Generator
type: SimulationSingleEventType::AdjustmentUpdated(),
name: 'Refund approved',
status: SimulationStatus::Active(),
payload: new JSONObject(json_decode(self::readRawJsonFixture('request/adjustment_updated_payload'), true),
),
payload: EntityFactory::create('adjustment.updated', json_decode(self::readRawJsonFixture('request/adjustment_updated_payload'), true)),
),
new Response(200, body: self::readRawJsonFixture('response/full_entity_adjustment_updated')),
self::readRawJsonFixture('request/update_full'),
Expand Down

0 comments on commit c8e4011

Please sign in to comment.