mirror of
https://github.com/shlinkio/shlink.git
synced 2025-02-25 18:45:27 -06:00
Move from preFlush to onFlush + postFlush
This commit is contained in:
@@ -5,7 +5,7 @@ declare(strict_types=1);
|
|||||||
use Doctrine\ORM\Events;
|
use Doctrine\ORM\Events;
|
||||||
use Happyr\DoctrineSpecification\Repository\EntitySpecificationRepository;
|
use Happyr\DoctrineSpecification\Repository\EntitySpecificationRepository;
|
||||||
use Shlinkio\Shlink\Core\Config\EnvVars;
|
use Shlinkio\Shlink\Core\Config\EnvVars;
|
||||||
use Shlinkio\Shlink\Core\Visit\Listener\ShortUrlVisitsCountPreFlushListener;
|
use Shlinkio\Shlink\Core\Visit\Listener\ShortUrlVisitsCountTracker;
|
||||||
|
|
||||||
use function Shlinkio\Shlink\Core\ArrayUtils\contains;
|
use function Shlinkio\Shlink\Core\ArrayUtils\contains;
|
||||||
|
|
||||||
@@ -63,9 +63,8 @@ return (static function (): array {
|
|||||||
'load_mappings_using_functional_style' => true,
|
'load_mappings_using_functional_style' => true,
|
||||||
'default_repository_classname' => EntitySpecificationRepository::class,
|
'default_repository_classname' => EntitySpecificationRepository::class,
|
||||||
'listeners' => [
|
'listeners' => [
|
||||||
Events::preFlush => [
|
Events::onFlush => [ShortUrlVisitsCountTracker::class],
|
||||||
ShortUrlVisitsCountPreFlushListener::class,
|
Events::postFlush => [ShortUrlVisitsCountTracker::class],
|
||||||
],
|
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
'connection' => $resolveConnection(),
|
'connection' => $resolveConnection(),
|
||||||
|
|||||||
@@ -76,7 +76,7 @@ return [
|
|||||||
EntityRepositoryFactory::class,
|
EntityRepositoryFactory::class,
|
||||||
Visit\Entity\Visit::class,
|
Visit\Entity\Visit::class,
|
||||||
],
|
],
|
||||||
Visit\Listener\ShortUrlVisitsCountPreFlushListener::class => InvokableFactory::class,
|
Visit\Listener\ShortUrlVisitsCountTracker::class => InvokableFactory::class,
|
||||||
|
|
||||||
Util\DoctrineBatchHelper::class => ConfigAbstractFactory::class,
|
Util\DoctrineBatchHelper::class => ConfigAbstractFactory::class,
|
||||||
Util\RedirectResponseHelper::class => ConfigAbstractFactory::class,
|
Util\RedirectResponseHelper::class => ConfigAbstractFactory::class,
|
||||||
|
|||||||
@@ -10,20 +10,33 @@ use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
|
|||||||
use Doctrine\DBAL\Platforms\SQLitePlatform;
|
use Doctrine\DBAL\Platforms\SQLitePlatform;
|
||||||
use Doctrine\DBAL\Platforms\SQLServerPlatform;
|
use Doctrine\DBAL\Platforms\SQLServerPlatform;
|
||||||
use Doctrine\ORM\EntityManagerInterface;
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
use Doctrine\ORM\Event\PreFlushEventArgs;
|
use Doctrine\ORM\Event\OnFlushEventArgs;
|
||||||
|
use Doctrine\ORM\Event\PostFlushEventArgs;
|
||||||
use Shlinkio\Shlink\Core\Visit\Entity\Visit;
|
use Shlinkio\Shlink\Core\Visit\Entity\Visit;
|
||||||
|
|
||||||
use function rand;
|
use function rand;
|
||||||
|
|
||||||
final readonly class ShortUrlVisitsCountPreFlushListener
|
final class ShortUrlVisitsCountTracker
|
||||||
{
|
{
|
||||||
|
/** @var object[] */
|
||||||
|
private array $entitiesToBeCreated = [];
|
||||||
|
|
||||||
|
public function onFlush(OnFlushEventArgs $args): void
|
||||||
|
{
|
||||||
|
// Track entities that are going to be created during this flush operation
|
||||||
|
$this->entitiesToBeCreated = $args->getObjectManager()->getUnitOfWork()->getScheduledEntityInsertions();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function preFlush(PreFlushEventArgs $args): void
|
public function postFlush(PostFlushEventArgs $args): void
|
||||||
{
|
{
|
||||||
$em = $args->getObjectManager();
|
$em = $args->getObjectManager();
|
||||||
$entitiesToBeCreated = $em->getUnitOfWork()->getScheduledEntityInsertions();
|
$entitiesToBeCreated = $this->entitiesToBeCreated;
|
||||||
|
|
||||||
|
// Reset tracked entities until next flush operation
|
||||||
|
$this->entitiesToBeCreated = [];
|
||||||
|
|
||||||
foreach ($entitiesToBeCreated as $entity) {
|
foreach ($entitiesToBeCreated as $entity) {
|
||||||
$this->trackVisitCount($em, $entity);
|
$this->trackVisitCount($em, $entity);
|
||||||
@@ -62,18 +62,6 @@ class ShortUrlListRepositoryTest extends DatabaseTestCase
|
|||||||
$this->getEntityManager()->persist($foo);
|
$this->getEntityManager()->persist($foo);
|
||||||
|
|
||||||
$bar = ShortUrl::withLongUrl('https://bar');
|
$bar = ShortUrl::withLongUrl('https://bar');
|
||||||
$this->getEntityManager()->persist($bar);
|
|
||||||
|
|
||||||
$foo2 = ShortUrl::withLongUrl('https://foo_2');
|
|
||||||
$ref = new ReflectionObject($foo2);
|
|
||||||
$dateProp = $ref->getProperty('dateCreated');
|
|
||||||
$dateProp->setAccessible(true);
|
|
||||||
$dateProp->setValue($foo2, Chronos::now()->subDays(5));
|
|
||||||
$this->getEntityManager()->persist($foo2);
|
|
||||||
|
|
||||||
// Flush short URLs first
|
|
||||||
$this->getEntityManager()->flush();
|
|
||||||
|
|
||||||
$visits = array_map(function () use ($bar) {
|
$visits = array_map(function () use ($bar) {
|
||||||
$visit = Visit::forValidShortUrl($bar, Visitor::botInstance());
|
$visit = Visit::forValidShortUrl($bar, Visitor::botInstance());
|
||||||
$this->getEntityManager()->persist($visit);
|
$this->getEntityManager()->persist($visit);
|
||||||
@@ -81,6 +69,9 @@ class ShortUrlListRepositoryTest extends DatabaseTestCase
|
|||||||
return $visit;
|
return $visit;
|
||||||
}, range(0, 5));
|
}, range(0, 5));
|
||||||
$bar->setVisits(new ArrayCollection($visits));
|
$bar->setVisits(new ArrayCollection($visits));
|
||||||
|
$this->getEntityManager()->persist($bar);
|
||||||
|
|
||||||
|
$foo2 = ShortUrl::withLongUrl('https://foo_2');
|
||||||
$visits2 = array_map(function () use ($foo2) {
|
$visits2 = array_map(function () use ($foo2) {
|
||||||
$visit = Visit::forValidShortUrl($foo2, Visitor::emptyInstance());
|
$visit = Visit::forValidShortUrl($foo2, Visitor::emptyInstance());
|
||||||
$this->getEntityManager()->persist($visit);
|
$this->getEntityManager()->persist($visit);
|
||||||
@@ -88,8 +79,12 @@ class ShortUrlListRepositoryTest extends DatabaseTestCase
|
|||||||
return $visit;
|
return $visit;
|
||||||
}, range(0, 3));
|
}, range(0, 3));
|
||||||
$foo2->setVisits(new ArrayCollection($visits2));
|
$foo2->setVisits(new ArrayCollection($visits2));
|
||||||
|
$ref = new ReflectionObject($foo2);
|
||||||
|
$dateProp = $ref->getProperty('dateCreated');
|
||||||
|
$dateProp->setAccessible(true);
|
||||||
|
$dateProp->setValue($foo2, Chronos::now()->subDays(5));
|
||||||
|
$this->getEntityManager()->persist($foo2);
|
||||||
|
|
||||||
// Flush visits afterwards
|
|
||||||
$this->getEntityManager()->flush();
|
$this->getEntityManager()->flush();
|
||||||
|
|
||||||
$result = $this->repo->findList(new ShortUrlsListFiltering(searchTerm: 'foo', tags: ['bar']));
|
$result = $this->repo->findList(new ShortUrlsListFiltering(searchTerm: 'foo', tags: ['bar']));
|
||||||
|
|||||||
Reference in New Issue
Block a user