Skip to content

Commit

Permalink
Matti/list event log items (#106)
Browse files Browse the repository at this point in the history
* Added method to list event log items

* Check event log item properties
  • Loading branch information
mroloux authored Jan 17, 2024
1 parent 30df127 commit 1074af6
Show file tree
Hide file tree
Showing 7 changed files with 199 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/EventLog/EventLog.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Seatsio\EventLog;

use GuzzleHttp\Client;
use Seatsio\PageFetcher;

class EventLog
{

/**
* @var Client
*/
private $client;

public function __construct(Client $client)
{
$this->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();
}));
}
}
31 changes: 31 additions & 0 deletions src/EventLog/EventLogItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Seatsio\EventLog;

class EventLogItem
{
/**
* @var int
*/
public $id;

/**
* @var string
*/
public $type;

/**
* @var \Seatsio\LocalDate
*/
public $date;

/**
* @var string
*/
public $workspaceKey;

/**
* @var object
*/
public $data;
}
36 changes: 36 additions & 0 deletions src/EventLog/EventLogItemLister.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Seatsio\EventLog;

use Seatsio\PageFetcher;

class EventLogItemLister
{

protected $pageFetcher;

public function __construct(PageFetcher $pageFetcher)
{
$this->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);
}
}
14 changes: 14 additions & 0 deletions src/EventLog/EventLogItemPage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Seatsio\EventLog;

use Seatsio\Page;

class EventLogItemPage extends Page
{
/**
* @var EventLogItem[]
*/
public $items;

}
15 changes: 15 additions & 0 deletions src/EventLog/EventLogItemPagedIterator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Seatsio\EventLog;

use Seatsio\PagedIterator;

class EventLogItemPagedIterator extends PagedIterator
{

public function current(): EventLogItem
{
return parent::current();
}

}
7 changes: 7 additions & 0 deletions src/SeatsioClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use GuzzleHttp\Middleware;
use Psr\Http\Message\ResponseInterface;
use Seatsio\Charts\Charts;
use Seatsio\EventLog\EventLog;
use Seatsio\Events\Events;
use Seatsio\Seasons\Seasons;
use Seatsio\HoldTokens\HoldTokens;
Expand Down Expand Up @@ -59,6 +60,11 @@ class SeatsioClient
*/
public $holdTokens;

/**
* @var EventLog
*/
public $eventLog;

public function __construct(Region $region, string $secretKey, string $workspaceKey = null, int $maxRetries = 5)
{
$client = new Client($this->clientConfig($secretKey, $workspaceKey, $region->url(), $maxRetries));
Expand All @@ -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)
Expand Down
49 changes: 49 additions & 0 deletions tests/EventLog/ListAllEventLogItemsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Seatsio\EventLog;

use Seatsio\Charts\ChartListParams;
use Seatsio\Events\CreateEventParams;
use Seatsio\Events\ForSaleConfig;
use Seatsio\SeatsioClientTest;
use function Functional\map;

class ListAllEventLogItemsTest extends SeatsioClientTest
{

public function testListAll()
{
$chart = $this->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);
}
}

0 comments on commit 1074af6

Please sign in to comment.