From ffeda55e840ad79c86a05113a75759b247da6955 Mon Sep 17 00:00:00 2001 From: Oliver Klee Date: Fri, 4 Oct 2024 19:06:35 +0200 Subject: [PATCH] [TASK] Make the `RegistrationManager` stateless (#3776) Fixes #2865 --- CHANGELOG.md | 1 + Classes/Service/RegistrationManager.php | 45 +++++++------------ Classes/Service/RegistrationProcessor.php | 6 +-- .../Unit/Service/RegistrationManagerTest.php | 29 ------------ .../Service/RegistrationProcessorTest.php | 4 +- phpstan-baseline.neon | 16 ++----- 6 files changed, 22 insertions(+), 79 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ef46156df..dbc3460e7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ This project adheres to [Semantic Versioning](https://semver.org/). ### Changed +- Make the `RegistrationManager` stateless (#3776) - Make `DataHandlerHook` stateless (#3774) - Merge `Csv/AbstractListView` into `Csv/AbstractRegistrationListView` (#3771) - Convert the manual to the new PHP-based rendering (#3692, #3696) diff --git a/Classes/Service/RegistrationManager.php b/Classes/Service/RegistrationManager.php index 1a94dd5b5..752bf11d2 100644 --- a/Classes/Service/RegistrationManager.php +++ b/Classes/Service/RegistrationManager.php @@ -46,8 +46,6 @@ class RegistrationManager private static ?RegistrationManager $instance = null; - private ?LegacyRegistration $registration = null; - private ?Template $emailTemplate = null; protected ?HookProvider $registrationEmailHookProvider = null; @@ -268,18 +266,18 @@ public function isUserRegistered(LegacyEvent $event): bool /** * Sends the emails for a new registration. */ - public function sendEmailsForNewRegistration(TemplateHelper $plugin): void + public function sendEmailsForNewRegistration(TemplateHelper $plugin, LegacyRegistration $registration): void { - if ($this->registration->isOnRegistrationQueue()) { - $this->notifyAttendee($this->registration, $plugin, 'confirmationOnRegistrationForQueue'); - $this->notifyOrganizers($this->registration, 'notificationOnRegistrationForQueue'); + if ($registration->isOnRegistrationQueue()) { + $this->notifyAttendee($registration, $plugin, 'confirmationOnRegistrationForQueue'); + $this->notifyOrganizers($registration, 'notificationOnRegistrationForQueue'); } else { - $this->notifyAttendee($this->registration, $plugin); - $this->notifyOrganizers($this->registration); + $this->notifyAttendee($registration, $plugin); + $this->notifyOrganizers($registration); } if ($this->getSharedConfiguration()->getAsBoolean('sendAdditionalNotificationEmails')) { - $this->sendAdditionalNotification($this->registration); + $this->sendAdditionalNotification($registration); } } @@ -291,12 +289,12 @@ public function sendEmailsForNewRegistration(TemplateHelper $plugin): void */ public function removeRegistration(int $uid, TemplateHelper $plugin): void { - $this->registration = LegacyRegistration::fromUid($uid); - if (!($this->registration instanceof LegacyRegistration)) { + $unregistration = LegacyRegistration::fromUid($uid); + if (!($unregistration instanceof LegacyRegistration)) { return; } - if ($this->registration->getUser() !== $this->getLoggedInFrontEndUserUid()) { + if ($unregistration->getUser() !== $this->getLoggedInFrontEndUserUid()) { return; } @@ -306,18 +304,18 @@ public function removeRegistration(int $uid, TemplateHelper $plugin): void ['uid' => $uid] ); - $this->notifyAttendee($this->registration, $plugin, 'confirmationOnUnregistration'); - $this->notifyOrganizers($this->registration, 'notificationOnUnregistration'); + $this->notifyAttendee($unregistration, $plugin, 'confirmationOnUnregistration'); + $this->notifyOrganizers($unregistration, 'notificationOnUnregistration'); - $this->fillVacancies($plugin); + $this->fillVacancies($plugin, $unregistration); } /** * Fills vacancies created through a unregistration with attendees from the registration queue. */ - private function fillVacancies(TemplateHelper $plugin): void + private function fillVacancies(TemplateHelper $plugin, LegacyRegistration $unregistration): void { - $seminar = $this->registration->getSeminarObject(); + $seminar = $unregistration->getSeminarObject(); if (!$seminar->hasVacancies()) { return; } @@ -1142,19 +1140,6 @@ protected function getUnregistrationNotice(LegacyEvent $event): string ); } - /** - * Returns the registration created via `createRegistration` or `setRegistration`. - */ - public function getRegistration(): ?LegacyRegistration - { - return $this->registration; - } - - public function setRegistration(LegacyRegistration $registration): void - { - $this->registration = $registration; - } - /** * Gets the hook provider for the registration emails. */ diff --git a/Classes/Service/RegistrationProcessor.php b/Classes/Service/RegistrationProcessor.php index f27aa5282..875a55404 100644 --- a/Classes/Service/RegistrationProcessor.php +++ b/Classes/Service/RegistrationProcessor.php @@ -207,11 +207,9 @@ public function sendEmails(Registration $registration): void throw new \RuntimeException('The registration has not been persisted yet.', 1668939288); } - $legacyRegistration = GeneralUtility::makeInstance(LegacyRegistration::class, $registrationUid); - $this->registrationManager->setRegistration($legacyRegistration); - $configuration = GeneralUtility::makeInstance(LegacyConfiguration::class); + $legacyRegistration = GeneralUtility::makeInstance(LegacyRegistration::class, $registrationUid); - $this->registrationManager->sendEmailsForNewRegistration($configuration); + $this->registrationManager->sendEmailsForNewRegistration($configuration, $legacyRegistration); } } diff --git a/Tests/Unit/Service/RegistrationManagerTest.php b/Tests/Unit/Service/RegistrationManagerTest.php index 715e3d6ed..ceadfc643 100644 --- a/Tests/Unit/Service/RegistrationManagerTest.php +++ b/Tests/Unit/Service/RegistrationManagerTest.php @@ -4,7 +4,6 @@ namespace OliverKlee\Seminars\Tests\Unit\Service; -use OliverKlee\Seminars\OldModel\LegacyRegistration; use OliverKlee\Seminars\Service\RegistrationManager; use TYPO3\TestingFramework\Core\Unit\UnitTestCase; @@ -16,15 +15,6 @@ final class RegistrationManagerTest extends UnitTestCase { protected bool $resetSingletonInstances = true; - private RegistrationManager $subject; - - protected function setUp(): void - { - parent::setUp(); - - $this->subject = new RegistrationManager(); - } - protected function tearDown(): void { RegistrationManager::purgeInstance(); @@ -61,23 +51,4 @@ public function purgeInstanceCausesGetInstanceToReturnNewInstance(): void self::assertNotSame($firstInstance, $secondInstance); } - - /** - * @test - */ - public function getRegistrationInitiallyReturnsNull(): void - { - self::assertNull($this->subject->getRegistration()); - } - - /** - * @test - */ - public function setRegistrationSetsRegistration(): void - { - $model = new LegacyRegistration(); - $this->subject->setRegistration($model); - - self::assertSame($model, $this->subject->getRegistration()); - } } diff --git a/Tests/Unit/Service/RegistrationProcessorTest.php b/Tests/Unit/Service/RegistrationProcessorTest.php index fd1ab7f39..8081440e4 100644 --- a/Tests/Unit/Service/RegistrationProcessorTest.php +++ b/Tests/Unit/Service/RegistrationProcessorTest.php @@ -581,10 +581,8 @@ public function sendEmailsSendsEmailsWithLegacyRegistrationAndLegacyConfiguratio $configurationMock = $this->createMock(LegacyConfiguration::class); GeneralUtility::addInstance(LegacyConfiguration::class, $configurationMock); - $this->registrationManagerMock->expects(self::once())->method('setRegistration') - ->with($legacyRegistrationMock); $this->registrationManagerMock->expects(self::once())->method('sendEmailsForNewRegistration') - ->with($configurationMock); + ->with($configurationMock, $legacyRegistrationMock); $this->subject->sendEmails($registration); } diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 4ebe28576..0db77df35 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -1460,21 +1460,11 @@ parameters: count: 2 path: Classes/Service/RegistrationManager.php - - - message: "#^Cannot call method getSeminarObject\\(\\) on OliverKlee\\\\Seminars\\\\OldModel\\\\LegacyRegistration\\|null\\.$#" - count: 1 - path: Classes/Service/RegistrationManager.php - - message: "#^Cannot call method getUid\\(\\) on OliverKlee\\\\Seminars\\\\OldModel\\\\LegacyRegistration\\|null\\.$#" count: 1 path: Classes/Service/RegistrationManager.php - - - message: "#^Cannot call method isOnRegistrationQueue\\(\\) on OliverKlee\\\\Seminars\\\\OldModel\\\\LegacyRegistration\\|null\\.$#" - count: 1 - path: Classes/Service/RegistrationManager.php - - message: "#^Cannot cast mixed to int\\.$#" count: 2 @@ -1487,17 +1477,17 @@ parameters: - message: "#^Parameter \\#1 \\$oldRegistration of method OliverKlee\\\\Seminars\\\\Service\\\\RegistrationManager\\:\\:notifyAttendee\\(\\) expects OliverKlee\\\\Seminars\\\\OldModel\\\\LegacyRegistration, OliverKlee\\\\Seminars\\\\OldModel\\\\LegacyRegistration\\|null given\\.$#" - count: 3 + count: 1 path: Classes/Service/RegistrationManager.php - message: "#^Parameter \\#1 \\$registration of method OliverKlee\\\\Seminars\\\\Service\\\\RegistrationManager\\:\\:notifyOrganizers\\(\\) expects OliverKlee\\\\Seminars\\\\OldModel\\\\LegacyRegistration, OliverKlee\\\\Seminars\\\\OldModel\\\\LegacyRegistration\\|null given\\.$#" - count: 4 + count: 1 path: Classes/Service/RegistrationManager.php - message: "#^Parameter \\#1 \\$registration of method OliverKlee\\\\Seminars\\\\Service\\\\RegistrationManager\\:\\:sendAdditionalNotification\\(\\) expects OliverKlee\\\\Seminars\\\\OldModel\\\\LegacyRegistration, OliverKlee\\\\Seminars\\\\OldModel\\\\LegacyRegistration\\|null given\\.$#" - count: 2 + count: 1 path: Classes/Service/RegistrationManager.php -