Skip to content

Commit

Permalink
add: Event objects
Browse files Browse the repository at this point in the history
  • Loading branch information
joshbruce committed May 2, 2023
1 parent 710f67a commit 237b8c5
Show file tree
Hide file tree
Showing 5 changed files with 196 additions and 0 deletions.
103 changes: 103 additions & 0 deletions src/EventHandling/Event.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php
declare(strict_types=1);

namespace Eightfold\Printify\EventHandling;

use StdClass;
use DateTime;

use Psr\Http\Message\ServerRequestInterface;

use Eightfold\Printify\EventHandling\EventType;

use Eightfold\Printify\Client;

use Eightfold\Printify\EventHandling\EventError;

use Eightfold\Printify\Shops\Products\Product;

class Event
{
public const TIMESTAMP_FORMAT = 'Y-m-d H:i:sP';

public static function fromRequest(
Client $client,
ServerRequestInterface $request
): self|EventError {
$json = $request->getBody()->getContents();
return self::fromJson($client, $json);
}

public static function fromJson(Client $client, string $json): self|EventError
{
$object = json_decode($json);
if (
is_object($object) == false or
is_a($object, StdClass::class) === false
) {
return EventError::InvalidJson;
}

return self::fromObject($client, $object);
}

public static function fromObject(Client $client, StdClass $object): self
{
return new self($client, $object);
}

final private function __construct(
private readonly Client $client,
private readonly StdClass $object
) {
}

private function client(): Client
{
return $this->client;
}

private function object(): StdClass
{
return $this->object;
}

public function id(): string
{
return $this->object()->id;
}

public function type(): EventType
{
return match ($this->object()->type) {
'product:publish:started' => EventType::ProductPublishStarted,
default => EventType::Unsupported
};
}

public function createdAt(): DateTime|false
{
return DateTime::createFromFormat(
self::TIMESTAMP_FORMAT,
$this->object()->created_at
);
}

public function resourceRaw(): StdClass
{
return $this->object()->resource;
}

public function resource(): Product|EventError
{
$meta = $this->resourceRaw();
return match ($meta->type) {
'product' => Product::withId(
$this->client(),
$meta->data->shop_id,
$meta->id
),
default => EventError::UnsupportedResourceType
};
}
}
10 changes: 10 additions & 0 deletions src/EventHandling/EventError.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
declare(strict_types=1);

namespace Eightfold\Printify\EventHandling;

enum EventError
{
case InvalidJson;
case UnsupportedResourceType;
}
10 changes: 10 additions & 0 deletions src/EventHandling/EventType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
declare(strict_types=1);

namespace Eightfold\Printify\EventHandling;

enum EventType
{
case Unsupported;
case ProductPublishStarted;
}
50 changes: 50 additions & 0 deletions tests/EventHandling/EventTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
declare(strict_types=1);

namespace Eightfold\Printify\Tests\EventHandling;

use Eightfold\Printify\Tests\TestCase;

use Eightfold\Printify\EventHandling\Event;

use Eightfold\Printify\EventHandling\EventType;

use Eightfold\Printify\Shops\Products\Product;

class EventTest extends TestCase
{
/**
* @test
*/
public function can_get_resource(): void
{
$json = file_get_contents(
__DIR__ . '/../api-responses/event-product-publish-started.json'
);

$result = Event::fromJson(parent::nonApiClient(), $json)->resource();

$this->assertTrue(
is_a($result, Product::class)
);
}

/**
* @test
*/
public function has_expected_event_type(): void
{
$json = file_get_contents(
__DIR__ . '/../api-responses/event-product-publish-started.json'
);

$expected = EventType::ProductPublishStarted;

$result = Event::fromJson(parent::nonApiClient(), $json)->type();

$this->assertSame(
$expected,
$result
);
}
}
23 changes: 23 additions & 0 deletions tests/api-responses/event-product-publish-started.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"id": "653b6be8-2ff7-4ab5-a7a6-6889a8b3bbf5",
"type": "product:publish:started",
"created_at": "2022-05-17 15:00:00+00:00",
"resource": {
"id": "5cb87a8cd490a2ccb256cec4",
"type": "product",
"data": {
"shop_id": 815256,
"publish_details": {
"title": true,
"variants": false,
"description": true,
"tags": true,
"images": false,
"key_features": false,
"shipping_template": true
},
"action": "create",
"out_of_stock_publishing": 0
}
}
}

0 comments on commit 237b8c5

Please sign in to comment.