generated from 8fold/github-repo-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from 8fold/initial
Initial
- Loading branch information
Showing
13 changed files
with
248 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.