mirror of
https://github.com/shlinkio/shlink.git
synced 2025-03-29 04:52:54 +03:00
Wrap visits tracking in transaction
This commit is contained in:
parent
cef30c8e2d
commit
4a05c4be40
2 changed files with 12 additions and 8 deletions
|
@ -12,12 +12,12 @@ use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
|
||||||
use Shlinkio\Shlink\Core\Visit\Entity\Visit;
|
use Shlinkio\Shlink\Core\Visit\Entity\Visit;
|
||||||
use Shlinkio\Shlink\Core\Visit\Model\Visitor;
|
use Shlinkio\Shlink\Core\Visit\Model\Visitor;
|
||||||
|
|
||||||
class VisitsTracker implements VisitsTrackerInterface
|
readonly class VisitsTracker implements VisitsTrackerInterface
|
||||||
{
|
{
|
||||||
public function __construct(
|
public function __construct(
|
||||||
private readonly ORM\EntityManagerInterface $em,
|
private ORM\EntityManagerInterface $em,
|
||||||
private readonly EventDispatcherInterface $eventDispatcher,
|
private EventDispatcherInterface $eventDispatcher,
|
||||||
private readonly TrackingOptions $options,
|
private TrackingOptions $options,
|
||||||
) {
|
) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -71,10 +71,12 @@ class VisitsTracker implements VisitsTrackerInterface
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$visit = $createVisit($visitor->normalizeForTrackingOptions($this->options));
|
$this->em->wrapInTransaction(function () use ($createVisit, $visitor): void {
|
||||||
$this->em->persist($visit);
|
$visit = $createVisit($visitor->normalizeForTrackingOptions($this->options));
|
||||||
$this->em->flush();
|
$this->em->persist($visit);
|
||||||
|
$this->em->flush();
|
||||||
|
|
||||||
$this->eventDispatcher->dispatch(new UrlVisited($visit->getId(), $visitor->remoteAddress));
|
$this->eventDispatcher->dispatch(new UrlVisited($visit->getId(), $visitor->remoteAddress));
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,6 +25,8 @@ class VisitsTrackerTest extends TestCase
|
||||||
protected function setUp(): void
|
protected function setUp(): void
|
||||||
{
|
{
|
||||||
$this->em = $this->createMock(EntityManager::class);
|
$this->em = $this->createMock(EntityManager::class);
|
||||||
|
$this->em->method('wrapInTransaction')->willReturnCallback(fn (callable $callback) => $callback());
|
||||||
|
|
||||||
$this->eventDispatcher = $this->createMock(EventDispatcherInterface::class);
|
$this->eventDispatcher = $this->createMock(EventDispatcherInterface::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue