Skip to content

Commit

Permalink
Add limit and offset to ActivityLogProvider (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
karlo-valentic authored Sep 18, 2024
1 parent cc3dee8 commit 70db9bc
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 14 deletions.
39 changes: 28 additions & 11 deletions src/DataProvider/ActivityLogProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@

final class ActivityLogProvider implements ActivityLogProviderInterface
{
public function __construct(private readonly ElasticsearchService $elasticsearchService, private readonly ElasticsearchContextFactoryInterface $elasticsearchContextFactory)
{
}
public function __construct(
private readonly ElasticsearchService $elasticsearchService,
private readonly ElasticsearchContextFactoryInterface $elasticsearchContextFactory
) { }

public function getActivityLogsByClass(string $className, array $sort = []): array
public function getActivityLogsByClass(string $className, array $sort = [], int $limit = 20, int $offset = 0): array
{
$elasticContext = $this->elasticsearchContextFactory->create($className);

Expand All @@ -25,7 +26,9 @@ public function getActivityLogsByClass(string $className, array $sort = []): arr
return $this->elasticsearchService->getCollection(
$elasticContext->getActivityLogIndex(),
ActivityLog::class,
$body
$body,
$limit,
$offset
);
}

Expand All @@ -45,8 +48,13 @@ public function getCurrentDataTrackerByClassAndId(string $className, $objectId):
);
}

public function getActivityLogsByClassAndId(string $className, mixed $objectId, array $sort = []): array
{
public function getActivityLogsByClassAndId(
string $className,
mixed $objectId,
array $sort = [],
int $limit = 20,
int $offset = 0
): array {
$elasticContext = $this->elasticsearchContextFactory->create($className);

$body = [
Expand All @@ -60,12 +68,19 @@ public function getActivityLogsByClassAndId(string $className, mixed $objectId,
return $this->elasticsearchService->getCollection(
$elasticContext->getActivityLogIndex(),
ActivityLog::class,
$body
$body,
$limit,
$offset
);
}

public function getActivityLogsByIndexAndId(string $index, $objectId, array $sort = []): array
{
public function getActivityLogsByIndexAndId(
string $index,
$objectId,
array $sort = [],
int $limit = 20,
int $offset = 0
): array {
$body = [
'sort' => $sort,
'query' => [
Expand All @@ -77,7 +92,9 @@ public function getActivityLogsByIndexAndId(string $index, $objectId, array $sor
return $this->elasticsearchService->getCollection(
$index,
ActivityLog::class,
$body
$body,
$limit,
$offset
);
}
}
6 changes: 3 additions & 3 deletions src/DataProvider/ActivityLogProviderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

interface ActivityLogProviderInterface
{
public function getActivityLogsByClass(string $className, array $sort = []): array;
public function getActivityLogsByClass(string $className, array $sort = [], int $limit = 20, int $offset = 0): array;

public function getActivityLogsByClassAndId(string $className, $objectId, array $sort = []): array;
public function getActivityLogsByClassAndId(string $className, $objectId, array $sort = [], int $limit = 20, int $offset = 0): array;

public function getActivityLogsByIndexAndId(string $index, $objectId, array $sort = []): array;
public function getActivityLogsByIndexAndId(string $index, $objectId, array $sort = [], int $limit = 20, int $offset = 0): array;
}
56 changes: 56 additions & 0 deletions tests/FunctionalTests/ActivityLogTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,62 @@ public function testLogEdit(): void
],
], $editedLog->getDataChanges());
}
public function testLogProviderByClassLimit(): void
{
$activityLogs = $this->activityLogProvider->getActivityLogsByClass(DummyBlogPost::class, [], 1);

self::assertCount(1, $activityLogs);
}

public function testLogProviderByClassAndIdLimit(): void
{
$activityLogs = $this->activityLogProvider->getActivityLogsByClassAndId(DummyBlogPost::class, 15, [], 1);

self::assertCount(1, $activityLogs);
}

public function testProviderByClassAndIndexLimit(): void
{
$activityLogs = $this->activityLogProvider->getActivityLogsByIndexAndId('dummy_blog_post_activity_log',15, [],1);

self::assertCount(1, $activityLogs);
}

public function testLogProviderByClassLimitAndOffset(): void
{
$activityLogs = $this->activityLogProvider->getActivityLogsByClass(DummyBlogPost::class, [], 20, 1);

$editedLog = reset($activityLogs);

self::assertCount(3, $activityLogs);
self::assertInstanceOf(ActivityLogInterface::class, $editedLog);
self::assertEquals(15, $editedLog->getObjectId());
self::assertEquals('Custom log name', $editedLog->getAction());
}

public function testLogProviderByClassAndIdLimitAndOffset(): void
{
$activityLogs = $this->activityLogProvider->getActivityLogsByClassAndId(DummyBlogPost::class, 15, [], 20, 2);

$editedLog = reset($activityLogs);

self::assertCount(2, $activityLogs);
self::assertInstanceOf(ActivityLogInterface::class, $editedLog);
self::assertEquals(15, $editedLog->getObjectId());
self::assertEquals(ActivityLogAction::EDITED, $editedLog->getAction());
}

public function testProviderByClassAndIndexLimitAndOffset(): void
{
$activityLogs = $this->activityLogProvider->getActivityLogsByIndexAndId('dummy_blog_post_activity_log',15, [],20, 3);

$editedLog = $activityLogs[0];

self::assertCount(1, $activityLogs);
self::assertInstanceOf(ActivityLogInterface::class, $editedLog);
self::assertEquals(15, $editedLog->getObjectId());
self::assertEquals(ActivityLogAction::EDITED, $editedLog->getAction());
}

public function testLogDelete(): void
{
Expand Down

0 comments on commit 70db9bc

Please sign in to comment.