From 99f28b569be82e6d82ef4b4beacda21e03426207 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Thu, 8 Dec 2022 22:06:10 +0100 Subject: [PATCH] Created method to get non-bot visits count for a short URL --- module/Core/src/ShortUrl/Entity/ShortUrl.php | 14 +++++++++---- .../Transformer/ShortUrlDataTransformer.php | 20 +++++++++++++++++-- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/module/Core/src/ShortUrl/Entity/ShortUrl.php b/module/Core/src/ShortUrl/Entity/ShortUrl.php index c7f10b75..0ebdeb24 100644 --- a/module/Core/src/ShortUrl/Entity/ShortUrl.php +++ b/module/Core/src/ShortUrl/Entity/ShortUrl.php @@ -33,9 +33,9 @@ class ShortUrl extends AbstractEntity private string $longUrl; private string $shortCode; private Chronos $dateCreated; - /** @var Collection|Visit[] */ + /** @var Collection */ private Collection $visits; - /** @var Collection|Tag[] */ + /** @var Collection */ private Collection $tags; private ?Chronos $validSince = null; private ?Chronos $validUntil = null; @@ -141,7 +141,7 @@ class ShortUrl extends AbstractEntity } /** - * @return Collection|Tag[] + * @return Collection */ public function getTags(): Collection { @@ -168,6 +168,12 @@ class ShortUrl extends AbstractEntity return count($this->visits); } + public function nonBotVisitsCount(): int + { + $criteria = Criteria::create()->where(Criteria::expr()->eq('potentialBot', false)); + return count($this->visits->matching($criteria)); + } + public function mostRecentImportedVisitDate(): ?Chronos { /** @var Selectable $visits */ @@ -183,7 +189,7 @@ class ShortUrl extends AbstractEntity } /** - * @param Collection|Visit[] $visits + * @param Collection $visits * @internal */ public function setVisits(Collection $visits): self diff --git a/module/Core/src/ShortUrl/Transformer/ShortUrlDataTransformer.php b/module/Core/src/ShortUrl/Transformer/ShortUrlDataTransformer.php index 262989ce..6d43f51d 100644 --- a/module/Core/src/ShortUrl/Transformer/ShortUrlDataTransformer.php +++ b/module/Core/src/ShortUrl/Transformer/ShortUrlDataTransformer.php @@ -13,7 +13,7 @@ use function Functional\invoke_if; class ShortUrlDataTransformer implements DataTransformerInterface { - public function __construct(private ShortUrlStringifierInterface $stringifier) + public function __construct(private readonly ShortUrlStringifierInterface $stringifier) { } @@ -27,13 +27,17 @@ class ShortUrlDataTransformer implements DataTransformerInterface 'shortUrl' => $this->stringifier->stringify($shortUrl), 'longUrl' => $shortUrl->getLongUrl(), 'dateCreated' => $shortUrl->getDateCreated()->toAtomString(), - 'visitsCount' => $shortUrl->getVisitsCount(), + 'nonBotVisitsCount' => $shortUrl->nonBotVisitsCount(), 'tags' => invoke($shortUrl->getTags(), '__toString'), 'meta' => $this->buildMeta($shortUrl), 'domain' => $shortUrl->getDomain(), 'title' => $shortUrl->title(), 'crawlable' => $shortUrl->crawlable(), 'forwardQuery' => $shortUrl->forwardQuery(), + 'visitsSummary' => $this->buildVisitsSummary($shortUrl), + + // Deprecated + 'visitsCount' => $shortUrl->getVisitsCount(), ]; } @@ -49,4 +53,16 @@ class ShortUrlDataTransformer implements DataTransformerInterface 'maxVisits' => $maxVisits, ]; } + + private function buildVisitsSummary(ShortUrl $shortUrl): array + { + $totalVisits = $shortUrl->getVisitsCount(); + $nonBotVisits = $shortUrl->nonBotVisitsCount(); + + return [ + 'total' => $totalVisits, + 'nonBots' => $nonBotVisits, + 'bots' => $totalVisits - $nonBotVisits, + ]; + } }