Skip to content

Commit

Permalink
Viewer page size can now be configured (#229)
Browse files Browse the repository at this point in the history
  • Loading branch information
DamienHarper authored Oct 31, 2024
1 parent 4452695 commit 0b9929a
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 7 deletions.
51 changes: 47 additions & 4 deletions src/Provider/Doctrine/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use DH\Auditor\Provider\ConfigurationInterface;
use DH\Auditor\Provider\Doctrine\Persistence\Helper\DoctrineHelper;
use DH\Auditor\Provider\Doctrine\Persistence\Reader\Reader;
use DH\Auditor\Provider\Doctrine\Persistence\Schema\SchemaManager;
use DH\Auditor\Provider\Doctrine\Service\AuditingService;
use DH\Auditor\Tests\Provider\Doctrine\ConfigurationTest;
Expand All @@ -31,6 +32,8 @@ final class Configuration implements ConfigurationInterface

private bool $isViewerEnabled;

private int $viewerPageSize;

private bool $initialized = false;

/**
Expand All @@ -57,10 +60,38 @@ public function __construct(array $options)
}
}

$this->isViewerEnabled = $config['viewer'];
$this->isViewerEnabled = self::isViewerEnabledInConfig($config['viewer']);
$this->viewerPageSize = self::getViewerPageSizeFromConfig($config['viewer']);
$this->storageMapper = $config['storage_mapper'];
}

public static function isViewerEnabledInConfig(mixed $config): bool
{
if (\is_array($config)) {
if (!\array_key_exists('enabled', $config) || !\is_bool($config['enabled'])) {
return false;
}

return $config['enabled'];
}

// "viewer" is disabled by default
return \is_bool($config) ? $config : false;
}

public static function getViewerPageSizeFromConfig(mixed $config): int
{
if (\is_array($config)) {
if (!\array_key_exists('page_size', $config) || !\is_int($config['page_size'])) {
return Reader::PAGE_SIZE;
}

return abs($config['page_size']);
}

return \is_int($config) ? abs($config) : Reader::PAGE_SIZE;
}

/**
* Set the value of entities.
*
Expand Down Expand Up @@ -101,6 +132,18 @@ public function disableViewer(): self
return $this;
}

public function setViewerPageSize(int $pageSize): self
{
$this->viewerPageSize = abs($pageSize);

return $this;
}

public function getViewerPageSize(): int
{
return $this->viewerPageSize;
}

/**
* Get enabled flag.
*/
Expand Down Expand Up @@ -168,11 +211,11 @@ public function getEntities(): array
$computedTableName = $schemaManager->resolveTableName($entityTableName, $namespaceName, $platform);
$this->entities[$entity]['table_schema'] = $namespaceName;
$this->entities[$entity]['table_name'] = $entityTableName;
// $this->entities[$entity]['computed_table_name'] = $entityTableName;
// $this->entities[$entity]['computed_table_name'] = $entityTableName;
$this->entities[$entity]['computed_table_name'] = $computedTableName;
$this->entities[$entity]['audit_table_schema'] = $namespaceName;
$this->entities[$entity]['audit_table_name'] = $schemaManager->computeAuditTablename($entityTableName, $this);
// $this->entities[$entity]['computed_audit_table_name'] = $schemaManager->computeAuditTablename($this->entities[$entity], $this, $platform);
// $this->entities[$entity]['computed_audit_table_name'] = $schemaManager->computeAuditTablename($this->entities[$entity], $this, $platform);
$this->entities[$entity]['computed_audit_table_name'] = $schemaManager->computeAuditTablename(
$computedTableName,
$this
Expand Down Expand Up @@ -264,7 +307,7 @@ private function configureOptions(OptionsResolver $resolver): void
->setAllowedTypes('entities', 'array')
->setAllowedTypes('storage_services', 'array')
->setAllowedTypes('auditing_services', 'array')
->setAllowedTypes('viewer', 'bool')
->setAllowedTypes('viewer', ['bool', 'array'])
->setAllowedTypes('storage_mapper', ['null', 'string', 'callable'])
;
}
Expand Down
7 changes: 5 additions & 2 deletions src/Provider/Doctrine/Persistence/Reader/Reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public function getAuditsByTransactionHash(string $transactionHash): array
$entities = $configuration->getEntities();
foreach (array_keys($entities) as $entity) {
try {
$audits = $this->createQuery($entity, ['transaction_hash' => $transactionHash])->execute();
$audits = $this->createQuery($entity, ['transaction_hash' => $transactionHash, 'page_size' => null])->execute();
if ([] !== $audits) {
$results[$entity] = $audits;
}
Expand All @@ -118,8 +118,11 @@ public function getAuditsByTransactionHash(string $transactionHash): array
/**
* @return array{results: \ArrayIterator<int|string, \DH\Auditor\Model\Entry>, currentPage: int, hasPreviousPage: bool, hasNextPage: bool, previousPage: null|int, nextPage: null|int, numPages: int, haveToPaginate: bool, numResults: int, pageSize: int}
*/
public function paginate(Query $query, int $page = 1, int $pageSize = self::PAGE_SIZE): array
public function paginate(Query $query, int $page = 1, ?int $pageSize = null): array
{
/** @var Configuration $configuration */
$configuration = $this->provider->getConfiguration();
$pageSize ??= $configuration->getViewerPageSize();
$numResults = $query->count();
$currentPage = max(1, $page);
$hasPreviousPage = $currentPage > 1;
Expand Down
17 changes: 16 additions & 1 deletion tests/Provider/Doctrine/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace DH\Auditor\Tests\Provider\Doctrine;

use DH\Auditor\Provider\Doctrine\Auditing\Annotation\Security;
use DH\Auditor\Provider\Doctrine\Persistence\Reader\Reader;
use DH\Auditor\Tests\Provider\Doctrine\Fixtures\Entity\Annotation\AuditableButUnauditedEntity;
use DH\Auditor\Tests\Provider\Doctrine\Fixtures\Entity\Annotation\AuditedEntity;
use DH\Auditor\Tests\Provider\Doctrine\Fixtures\Entity\Standard\Blog\Comment;
Expand Down Expand Up @@ -86,12 +87,26 @@ public function testDisableViewer(): void
public function testEnableViewer(): void
{
$configuration = $this->createProviderConfiguration();
$configuration->disableViewer();
$configuration->enableViewer();

$this->assertTrue($configuration->isViewerEnabled(), 'Viewer is enabled.');
}

public function testDefaultViewerPageSize(): void
{
$configuration = $this->createProviderConfiguration();

$this->assertSame(Reader::PAGE_SIZE, $configuration->getViewerPageSize(), \sprintf('Viewer pageSize is %d by default.', Reader::PAGE_SIZE));
}

public function testChangeViewerPageSize(): void
{
$configuration = $this->createProviderConfiguration();
$configuration->setViewerPageSize(25);

$this->assertSame(25, $configuration->getViewerPageSize(), 'Viewer pageSize is 25.');
}

public function testGloballyIgnoredColumns(): void
{
$configuration = $this->createProviderConfiguration([
Expand Down
10 changes: 10 additions & 0 deletions tests/Provider/Doctrine/Persistence/Reader/ReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use DH\Auditor\Provider\Doctrine\Persistence\Reader\Filter\DateRangeFilter;
use DH\Auditor\Provider\Doctrine\Persistence\Reader\Filter\SimpleFilter;
use DH\Auditor\Provider\Doctrine\Persistence\Reader\Query;
use DH\Auditor\Provider\Doctrine\Persistence\Reader\Reader;
use DH\Auditor\Provider\Doctrine\Service\StorageService;
use DH\Auditor\Tests\Provider\Doctrine\Fixtures\Entity\Standard\Blog\Author;
use DH\Auditor\Tests\Provider\Doctrine\Fixtures\Entity\Standard\Blog\Comment;
Expand Down Expand Up @@ -197,8 +198,17 @@ public function testGetAuditsPager(): void
{
$reader = $this->createReader();

$pager = $reader->paginate($reader->createQuery(Author::class));
$this->assertIsArray($pager);
$this->assertSame(Reader::PAGE_SIZE, $pager['pageSize'], \sprintf('PageSize is %d.', Reader::PAGE_SIZE));
$this->assertFalse($pager['hasPreviousPage'], 'Pager is at page 1.');
$this->assertFalse($pager['hasNextPage'], 'Pager has next page.');
$this->assertFalse($pager['haveToPaginate'], 'Pager has to paginate.');
$this->assertSame(1, $pager['numPages'], 'Pager has 1 page.');

$pager = $reader->paginate($reader->createQuery(Author::class), 1, 2);
$this->assertIsArray($pager);
$this->assertSame(2, $pager['pageSize'], 'PageSize is 2.');
$this->assertFalse($pager['hasPreviousPage'], 'Pager is at page 1.');
$this->assertTrue($pager['hasNextPage'], 'Pager has next page.');
$this->assertTrue($pager['haveToPaginate'], 'Pager has to paginate.');
Expand Down

0 comments on commit 0b9929a

Please sign in to comment.