From 1074af64d9265b8fc16a6b235f0dc809fc37a150 Mon Sep 17 00:00:00 2001 From: mroloux Date: Wed, 17 Jan 2024 15:40:02 +0100 Subject: [PATCH] Matti/list event log items (#106) * Added method to list event log items * Check event log item properties --- src/EventLog/EventLog.php | 47 ++++++++++++++++++++ src/EventLog/EventLogItem.php | 31 +++++++++++++ src/EventLog/EventLogItemLister.php | 36 +++++++++++++++ src/EventLog/EventLogItemPage.php | 14 ++++++ src/EventLog/EventLogItemPagedIterator.php | 15 +++++++ src/SeatsioClient.php | 7 +++ tests/EventLog/ListAllEventLogItemsTest.php | 49 +++++++++++++++++++++ 7 files changed, 199 insertions(+) create mode 100644 src/EventLog/EventLog.php create mode 100644 src/EventLog/EventLogItem.php create mode 100644 src/EventLog/EventLogItemLister.php create mode 100644 src/EventLog/EventLogItemPage.php create mode 100644 src/EventLog/EventLogItemPagedIterator.php create mode 100644 tests/EventLog/ListAllEventLogItemsTest.php diff --git a/src/EventLog/EventLog.php b/src/EventLog/EventLog.php new file mode 100644 index 0000000..dca12af --- /dev/null +++ b/src/EventLog/EventLog.php @@ -0,0 +1,47 @@ +client = $client; + } + + public function listAll(): EventLogItemPagedIterator + { + return $this->iterator()->all(); + } + + public function listFirstPage(int $pageSize = null): EventLogItemPage + { + return $this->iterator()->firstPage($pageSize); + } + + public function listPageAfter(int $afterId, int $pageSize = null): EventLogItemPage + { + return $this->iterator()->pageAfter($afterId, $pageSize); + } + + public function listPageBefore(int $beforeId, int $pageSize = null): EventLogItemPage + { + return $this->iterator()->pageBefore($beforeId, $pageSize); + } + + private function iterator(): EventLogItemLister + { + return new EventLogItemLister(new PageFetcher('/event-log', $this->client, function () { + return new EventLogItemPage(); + })); + } +} diff --git a/src/EventLog/EventLogItem.php b/src/EventLog/EventLogItem.php new file mode 100644 index 0000000..bab2f0f --- /dev/null +++ b/src/EventLog/EventLogItem.php @@ -0,0 +1,31 @@ +pageFetcher = $pageFetcher; + } + + public function all(): EventLogItemPagedIterator + { + return new EventLogItemPagedIterator($this->pageFetcher); + } + + public function firstPage(int $pageSize = null): EventLogItemPage + { + return $this->pageFetcher->fetchAfter(null, [], $pageSize); + } + + public function pageAfter(int $afterId, int $pageSize = null): EventLogItemPage + { + return $this->pageFetcher->fetchAfter($afterId, [], $pageSize); + } + + public function pageBefore(int $beforeId, int $pageSize = null): EventLogItemPage + { + return $this->pageFetcher->fetchBefore($beforeId, [], $pageSize); + } +} diff --git a/src/EventLog/EventLogItemPage.php b/src/EventLog/EventLogItemPage.php new file mode 100644 index 0000000..372a12b --- /dev/null +++ b/src/EventLog/EventLogItemPage.php @@ -0,0 +1,14 @@ +clientConfig($secretKey, $workspaceKey, $region->url(), $maxRetries)); @@ -71,6 +77,7 @@ public function __construct(Region $region, string $secretKey, string $workspace $this->usageReports = new UsageReports($client); $this->workspaces = new Workspaces($client); $this->holdTokens = new HoldTokens($client); + $this->eventLog = new EventLog($client); } private function clientConfig($secretKey, $workspaceKey, $baseUrl, $maxRetries) diff --git a/tests/EventLog/ListAllEventLogItemsTest.php b/tests/EventLog/ListAllEventLogItemsTest.php new file mode 100644 index 0000000..a017868 --- /dev/null +++ b/tests/EventLog/ListAllEventLogItemsTest.php @@ -0,0 +1,49 @@ +seatsioClient->charts->create(); + $this->seatsioClient->charts->update($chart->key, "a chart"); + + $this->waitUntilEventLogItemsAvailable(); + + $eventLogItems = $this->seatsioClient->eventLog->listAll(); + $types = map($eventLogItems, function ($eventLogItem) { + return $eventLogItem->type; + }); + + self::assertEquals(["chart.created", "chart.published"], array_values($types)); + } + + public function testProperties() + { + $chart = $this->seatsioClient->charts->create(); + $this->seatsioClient->charts->update($chart->key, "a chart"); + + $this->waitUntilEventLogItemsAvailable(); + + $eventLogItem = $this->seatsioClient->eventLog->listAll()->current(); + + self::assertEquals("chart.created", $eventLogItem->type); + self::assertEquals($this->workspace->key, $eventLogItem->workspaceKey); + self::assertNotNull($eventLogItem->date); + self::assertGreaterThan(0, $eventLogItem->id); + self::assertEquals((object)["key" => $chart->key], $eventLogItem->data); + } + + private function waitUntilEventLogItemsAvailable() + { + sleep(2); + } +}