Add ShortUrlVisitsCountTrackerTest

This commit is contained in:
Alejandro Celaya
2024-03-28 09:43:54 +01:00
parent 4a05c4be40
commit da922fb2a7
5 changed files with 91 additions and 15 deletions

View File

@@ -12,8 +12,8 @@ class ShortUrlVisitsCount extends AbstractEntity
public function __construct(
private readonly ShortUrl $shortUrl,
private readonly bool $potentialBot = false,
private readonly int $slotId = 1,
private readonly string $count = '1',
public readonly int $slotId = 1,
public readonly string $count = '1',
) {
}
}

View File

@@ -136,8 +136,9 @@ final class ShortUrlVisitsCountTracker
$qb->forUpdate();
}
$resultSet = $qb->executeQuery()->fetchOne();
$writeQb = ! $resultSet
$visitsCountId = $qb->executeQuery()->fetchOne();
$writeQb = ! $visitsCountId
? $conn->createQueryBuilder()
->insert('short_url_visits_counts')
->values([
@@ -145,18 +146,15 @@ final class ShortUrlVisitsCountTracker
'potential_bot' => ':potential_bot',
'slot_id' => ':slot_id',
])
: $conn->createQueryBuilder()
->update('short_url_visits_counts')
->set('count', 'count + 1')
->where($qb->expr()->and(
$qb->expr()->eq('short_url_id', ':short_url_id'),
$qb->expr()->eq('potential_bot', ':potential_bot'),
$qb->expr()->eq('slot_id', ':slot_id'),
));
$writeQb->setParameter('short_url_id', $shortUrlId)
->setParameter('short_url_id', $shortUrlId)
->setParameter('potential_bot', $potentialBot ? '1' : '0')
->setParameter('slot_id', $slotId)
->executeStatement();
: $conn->createQueryBuilder()
->update('short_url_visits_counts')
->set('count', 'count + 1')
->where($qb->expr()->eq('id', ':visits_count_id'))
->setParameter('visits_count_id', $visitsCountId);
$writeQb->executeStatement();
}
}