Skip to content

Commit

Permalink
[TASK] Make the RegistrationManager stateless (#3776)
Browse files Browse the repository at this point in the history
Fixes #2865
  • Loading branch information
oliverklee authored Oct 4, 2024
1 parent 4d1a889 commit ffeda55
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 79 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
45 changes: 15 additions & 30 deletions Classes/Service/RegistrationManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ class RegistrationManager

private static ?RegistrationManager $instance = null;

private ?LegacyRegistration $registration = null;

private ?Template $emailTemplate = null;

protected ?HookProvider $registrationEmailHookProvider = null;
Expand Down Expand Up @@ -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);
}
}

Expand All @@ -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;
}

Expand All @@ -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;
}
Expand Down Expand Up @@ -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.
*/
Expand Down
6 changes: 2 additions & 4 deletions Classes/Service/RegistrationProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
29 changes: 0 additions & 29 deletions Tests/Unit/Service/RegistrationManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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();
Expand Down Expand Up @@ -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());
}
}
4 changes: 1 addition & 3 deletions Tests/Unit/Service/RegistrationProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
16 changes: 3 additions & 13 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

-
Expand Down

0 comments on commit ffeda55

Please sign in to comment.