From 87dee24a76edcd66f97abf1cd9b716cfb90ab940 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Wed, 11 Oct 2023 22:40:43 +0200 Subject: [PATCH] Fix doctrine deprecation warnings --- phpunit.xml.dist | 19 ++++++++---------- src/Entity/PackageRepository.php | 4 ++-- src/Entity/VersionRepository.php | 4 ++-- src/EventListener/PackageListener.php | 28 +++++++++++++++++++++------ 4 files changed, 34 insertions(+), 21 deletions(-) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index fae5a5269..fa3838332 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,28 +1,25 @@ - - - - - + + + + - tests - - + + src - - + diff --git a/src/Entity/PackageRepository.php b/src/Entity/PackageRepository.php index b14bf96c1..bee35bbf5 100644 --- a/src/Entity/PackageRepository.php +++ b/src/Entity/PackageRepository.php @@ -563,7 +563,7 @@ public function getTotal(): int $sql, [], [], - new QueryCacheProfile(86400, 'total_packages', $this->getEntityManager()->getConfiguration()->getResultCacheImpl()) + new QueryCacheProfile(86400, 'total_packages', $this->getEntityManager()->getConfiguration()->getResultCache()) ); $result = $stmt->fetchAllAssociative(); $stmt->free(); @@ -583,7 +583,7 @@ public function getCountByYearMonth(): array $sql, [], [], - new QueryCacheProfile(3600, 'package_count_by_year_month', $this->getEntityManager()->getConfiguration()->getResultCacheImpl()) + new QueryCacheProfile(3600, 'package_count_by_year_month', $this->getEntityManager()->getConfiguration()->getResultCache()) ); $result = $stmt->fetchAllAssociative(); $stmt->free(); diff --git a/src/Entity/VersionRepository.php b/src/Entity/VersionRepository.php index dc2288baf..484502289 100644 --- a/src/Entity/VersionRepository.php +++ b/src/Entity/VersionRepository.php @@ -261,7 +261,7 @@ public function getTotal(): int $sql, [], [], - new QueryCacheProfile(86400, 'total_package_versions', $this->getEntityManager()->getConfiguration()->getResultCacheImpl()) + new QueryCacheProfile(86400, 'total_package_versions', $this->getEntityManager()->getConfiguration()->getResultCache()) ); $result = $stmt->fetchAllAssociative(); $stmt->free(); @@ -281,7 +281,7 @@ public function getCountByYearMonth(): array $sql, [], [], - new QueryCacheProfile(3600, 'package_versions_count_by_year_month', $this->getEntityManager()->getConfiguration()->getResultCacheImpl()) + new QueryCacheProfile(3600, 'package_versions_count_by_year_month', $this->getEntityManager()->getConfiguration()->getResultCache()) ); $result = $stmt->fetchAllAssociative(); $stmt->free(); diff --git a/src/EventListener/PackageListener.php b/src/EventListener/PackageListener.php index c4845125a..7a4698928 100644 --- a/src/EventListener/PackageListener.php +++ b/src/EventListener/PackageListener.php @@ -17,10 +17,12 @@ use App\Entity\User; use App\Util\DoctrineTrait; use Doctrine\Bundle\DoctrineBundle\Attribute\AsEntityListener; +use Doctrine\DBAL\Types\Types; use Doctrine\ORM\EntityManager; use Doctrine\Persistence\Event\LifecycleEventArgs; use Doctrine\ORM\Event\PreUpdateEventArgs; use Doctrine\Persistence\ManagerRegistry; +use Symfony\Bridge\Doctrine\Types\UlidType; use Symfony\Bundle\SecurityBundle\Security; #[AsEntityListener(event: 'postPersist', entity: Package::class)] @@ -45,9 +47,7 @@ public function __construct( */ public function postPersist(Package $package, LifecycleEventArgs $event): void { - $record = AuditRecord::packageCreated($package, $this->getUser()); - $this->getEM()->persist($record); - $this->getEM()->flush(); + $this->insert(AuditRecord::packageCreated($package, $this->getUser())); } /** @@ -63,7 +63,7 @@ public function preRemove(Package $package, LifecycleEventArgs $event): void public function preUpdate(Package $package, PreUpdateEventArgs $event): void { if ($event->hasChangedField('repository')) { - // buffering things to be flushed in postUpdate as flushing here results in an infinite loop + // buffering things to be inserted in postUpdate once we can confirm it is done $this->buffered[] = AuditRecord::canonicalUrlChange($package, $this->getUser(), $event->getOldValue('repository')); } } @@ -75,9 +75,8 @@ public function postUpdate(Package $package, LifecycleEventArgs $event): void { if ($this->buffered) { foreach ($this->buffered as $record) { - $this->getEM()->persist($record); + $this->insert($record); } - $this->getEM()->flush(); $this->buffered = []; } } @@ -88,4 +87,21 @@ private function getUser(): User|null return $user instanceof User ? $user : null; } + + private function insert(AuditRecord $record): void + { + $this->getEM()->getConnection()->insert('audit_log', [ + 'id' => $record->id, + 'datetime' => $record->datetime, + 'type' => $record->type->value, + 'attributes' => $record->attributes, + 'userId' => $record->userId, + 'vendor' => $record->vendor, + 'packageId' => $record->packageId, + ], [ + 'id' => UlidType::NAME, + 'datetime' => Types::DATETIME_IMMUTABLE, + 'attributes' => Types::JSON + ]); + } }