From 6bfc1187d5c08b29397161725d72348567545a48 Mon Sep 17 00:00:00 2001 From: Damien Harper Date: Mon, 2 Dec 2019 21:20:56 +0100 Subject: [PATCH] Restored `AuditReader::getAuditsCount()` which was removed by error in `3.2.0` --- CHANGELOG.md | 6 +++++ .../Reader/AuditReader.php | 26 +++++++++++++++++++ .../Reader/AuditReaderTest.php | 10 +++++++ 3 files changed, 42 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 38d0df7d..1013367c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. The format is inspired by [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [3.2.1] - 2019-12-02 + +### Changes +* Restored `AuditReader::getAuditsCount()` which was removed by error in `3.2.0` + + ## [3.2.0] - 2019-12-02 ### Changes diff --git a/src/DoctrineAuditBundle/Reader/AuditReader.php b/src/DoctrineAuditBundle/Reader/AuditReader.php index ba756cbe..9eb386d9 100644 --- a/src/DoctrineAuditBundle/Reader/AuditReader.php +++ b/src/DoctrineAuditBundle/Reader/AuditReader.php @@ -216,6 +216,32 @@ public function getAuditsPager(string $entity, $id = null, int $page = 1, int $p ]; } + /** + * Returns the amount of audited entries/operations. + * + * @param string $entity + * @param null|int|string $id + * + * @throws AccessDeniedException + * @throws InvalidArgumentException + * + * @return int + */ + public function getAuditsCount(string $entity, $id = null): int + { + $queryBuilder = $this->getAuditsQueryBuilder($entity, $id); + + $result = $queryBuilder + ->resetQueryPart('select') + ->resetQueryPart('orderBy') + ->select('COUNT(id)') + ->execute() + ->fetchColumn(0) + ; + + return false === $result ? 0 : $result; + } + /** * @param string $entity * @param string $id diff --git a/tests/DoctrineAuditBundle/Reader/AuditReaderTest.php b/tests/DoctrineAuditBundle/Reader/AuditReaderTest.php index 250307f4..2f43245b 100644 --- a/tests/DoctrineAuditBundle/Reader/AuditReaderTest.php +++ b/tests/DoctrineAuditBundle/Reader/AuditReaderTest.php @@ -216,6 +216,16 @@ public function testGetAuditsPager(): void self::assertTrue($pager['haveToPaginate'], 'pager has to paginate.'); } + public function testGetAuditsCount(): void + { + $reader = $this->getReader($this->getAuditConfiguration()); + + /** @var AuditEntry[] $audits */ + $count = $reader->getAuditsCount(Author::class, null); + + self::assertSame(5, $count, 'count is ok.'); + } + /** * @depends testGetAudits */