Created method to get non-bot visits count for a short URL

This commit is contained in:
Alejandro Celaya 2022-12-08 22:06:10 +01:00
parent 0c83dea8b7
commit 99f28b569b
2 changed files with 28 additions and 6 deletions

View File

@ -33,9 +33,9 @@ class ShortUrl extends AbstractEntity
private string $longUrl;
private string $shortCode;
private Chronos $dateCreated;
/** @var Collection|Visit[] */
/** @var Collection<int, Visit> */
private Collection $visits;
/** @var Collection|Tag[] */
/** @var Collection<int, Tag> */
private Collection $tags;
private ?Chronos $validSince = null;
private ?Chronos $validUntil = null;
@ -141,7 +141,7 @@ class ShortUrl extends AbstractEntity
}
/**
* @return Collection|Tag[]
* @return Collection<int, Tag>
*/
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<int, Visit> $visits
* @internal
*/
public function setVisits(Collection $visits): self

View File

@ -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,
];
}
}