Total Complexity | 77 |
Total Lines | 362 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like RegistrationService often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use RegistrationService, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
34 | class RegistrationService |
||
35 | { |
||
36 | protected EventDispatcherInterface $eventDispatcher; |
||
37 | protected RegistrationRepository $registrationRepository; |
||
38 | protected FrontendUserRepository $frontendUserRepository; |
||
39 | protected HashService $hashService; |
||
40 | protected PaymentService $paymentService; |
||
41 | protected NotificationService $notificationService; |
||
42 | |||
43 | public function injectFrontendUserRepository(FrontendUserRepository $frontendUserRepository): void |
||
44 | { |
||
45 | $this->frontendUserRepository = $frontendUserRepository; |
||
46 | } |
||
47 | |||
48 | public function injectHashService(HashService $hashService): void |
||
49 | { |
||
50 | $this->hashService = $hashService; |
||
51 | } |
||
52 | |||
53 | public function injectNotificationService(NotificationService $notificationService): void |
||
56 | } |
||
57 | |||
58 | public function injectEventDispatcher(EventDispatcherInterface $eventDispatcher): void |
||
59 | { |
||
60 | $this->eventDispatcher = $eventDispatcher; |
||
61 | } |
||
62 | |||
63 | public function injectPaymentService(PaymentService $paymentService): void |
||
66 | } |
||
67 | |||
68 | public function injectRegistrationRepository(RegistrationRepository $registrationRepository): void |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * Duplicates (all public accessable properties) the given registration the |
||
75 | * amount of times configured in amountOfRegistrations |
||
76 | */ |
||
77 | public function createDependingRegistrations(Registration $registration): void |
||
78 | { |
||
79 | $registrations = $registration->getAmountOfRegistrations(); |
||
80 | for ($i = 1; $i <= $registrations - 1; $i++) { |
||
81 | $newReg = GeneralUtility::makeInstance(Registration::class); |
||
82 | $properties = ObjectAccess::getGettableProperties($registration); |
||
83 | foreach ($properties as $propertyName => $propertyValue) { |
||
84 | ObjectAccess::setProperty($newReg, $propertyName, $propertyValue); |
||
85 | } |
||
86 | $newReg->setMainRegistration($registration); |
||
87 | $newReg->setAmountOfRegistrations(1); |
||
88 | $newReg->setIgnoreNotifications(true); |
||
89 | $this->registrationRepository->add($newReg); |
||
90 | } |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * Confirms all depending registrations based on the given main registration |
||
95 | */ |
||
96 | public function confirmDependingRegistrations(Registration $registration): void |
||
97 | { |
||
98 | $registrations = $this->registrationRepository->findByMainRegistration($registration); |
||
|
|||
99 | foreach ($registrations as $foundRegistration) { |
||
100 | /** @var Registration $foundRegistration */ |
||
101 | $foundRegistration->setConfirmed(true); |
||
102 | $this->registrationRepository->update($foundRegistration); |
||
103 | } |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * Checks if the registration can be confirmed and returns an array of variables |
||
108 | */ |
||
109 | public function checkConfirmRegistration(int $reguid, string $hmac): array |
||
153 | ]; |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * Cancels all depending registrations based on the given main registration |
||
158 | */ |
||
159 | public function cancelDependingRegistrations(Registration $registration): void |
||
160 | { |
||
161 | $registrations = $this->registrationRepository->findByMainRegistration($registration); |
||
162 | foreach ($registrations as $foundRegistration) { |
||
163 | $this->registrationRepository->remove($foundRegistration); |
||
164 | } |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * Checks if the registration can be cancelled and returns an array of variables |
||
169 | */ |
||
170 | public function checkCancelRegistration(int $reguid, string $hmac): array |
||
223 | ]; |
||
224 | } |
||
225 | |||
226 | /** |
||
227 | * Returns the current frontend user object if available |
||
228 | */ |
||
229 | public function getCurrentFeUserObject(): ?FrontendUser |
||
239 | } |
||
240 | |||
241 | /** |
||
242 | * Checks, if the registration can successfully be created. Note, that |
||
243 | * $result is passed by reference! |
||
244 | */ |
||
245 | public function checkRegistrationSuccess(Event $event, Registration $registration, int $result): array |
||
246 | { |
||
247 | $success = true; |
||
248 | if ($event->getEnableRegistration() === false) { |
||
249 | $success = false; |
||
250 | $result = RegistrationResult::REGISTRATION_NOT_ENABLED; |
||
251 | } elseif ($event->getRegistrationDeadline() != null && $event->getRegistrationDeadline() < new \DateTime()) { |
||
252 | $success = false; |
||
253 | $result = RegistrationResult::REGISTRATION_FAILED_DEADLINE_EXPIRED; |
||
254 | } elseif (!$event->getAllowRegistrationUntilEnddate() && $event->getStartdate() < new \DateTime()) { |
||
255 | $success = false; |
||
256 | $result = RegistrationResult::REGISTRATION_FAILED_EVENT_EXPIRED; |
||
257 | } elseif ($event->getAllowRegistrationUntilEnddate() && $event->getEnddate() && $event->getEnddate() < new \DateTime()) { |
||
258 | $success = false; |
||
259 | $result = RegistrationResult::REGISTRATION_FAILED_EVENT_ENDED; |
||
260 | } elseif ($event->getRegistrations()->count() >= $event->getMaxParticipants() |
||
261 | && $event->getMaxParticipants() > 0 && !$event->getEnableWaitlist() |
||
262 | ) { |
||
263 | $success = false; |
||
264 | $result = RegistrationResult::REGISTRATION_FAILED_MAX_PARTICIPANTS; |
||
265 | } elseif ($event->getFreePlaces() < $registration->getAmountOfRegistrations() |
||
266 | && $event->getMaxParticipants() > 0 && !$event->getEnableWaitlist() |
||
267 | ) { |
||
268 | $success = false; |
||
269 | $result = RegistrationResult::REGISTRATION_FAILED_NOT_ENOUGH_FREE_PLACES; |
||
270 | } elseif ($event->getMaxRegistrationsPerUser() < $registration->getAmountOfRegistrations()) { |
||
271 | $success = false; |
||
272 | $result = RegistrationResult::REGISTRATION_FAILED_MAX_AMOUNT_REGISTRATIONS_EXCEEDED; |
||
273 | } elseif ($event->getUniqueEmailCheck() && |
||
274 | $this->emailNotUnique($event, $registration->getEmail()) |
||
275 | ) { |
||
276 | $success = false; |
||
277 | $result = RegistrationResult::REGISTRATION_FAILED_EMAIL_NOT_UNIQUE; |
||
278 | } elseif ($event->getRegistrations()->count() >= $event->getMaxParticipants() |
||
279 | && $event->getMaxParticipants() > 0 && $event->getEnableWaitlist() |
||
280 | ) { |
||
281 | $result = RegistrationResult::REGISTRATION_SUCCESSFUL_WAITLIST; |
||
282 | } |
||
283 | |||
284 | return [$success, $result]; |
||
285 | } |
||
286 | |||
287 | /** |
||
288 | * Returns if the given email is registered to the given event |
||
289 | */ |
||
290 | protected function emailNotUnique(Event $event, string $email): bool |
||
291 | { |
||
292 | $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class) |
||
293 | ->getQueryBuilderForTable('tx_sfeventmgt_domain_model_registration'); |
||
294 | $queryBuilder->getRestrictions()->removeByType(HiddenRestriction::class); |
||
295 | $registrations = $queryBuilder->count('email') |
||
296 | ->from('tx_sfeventmgt_domain_model_registration') |
||
297 | ->where( |
||
298 | $queryBuilder->expr()->eq( |
||
299 | 'event', |
||
300 | $queryBuilder->createNamedParameter($event->getUid(), Connection::PARAM_INT) |
||
301 | ), |
||
302 | $queryBuilder->expr()->eq( |
||
303 | 'email', |
||
304 | $queryBuilder->createNamedParameter($email, Connection::PARAM_STR) |
||
305 | ) |
||
306 | ) |
||
307 | ->execute() |
||
308 | ->fetchOne(); |
||
309 | |||
310 | return $registrations >= 1; |
||
311 | } |
||
312 | |||
313 | /** |
||
314 | * Returns, if payment redirect for the payment method is enabled |
||
315 | */ |
||
316 | public function redirectPaymentEnabled(Registration $registration): bool |
||
317 | { |
||
318 | if ($registration->getEvent()->getEnablePayment() === false) { |
||
319 | return false; |
||
320 | } |
||
321 | |||
322 | /** @var AbstractPayment $paymentInstance */ |
||
323 | $paymentInstance = $this->paymentService->getPaymentInstance($registration->getPaymentmethod()); |
||
324 | if ($paymentInstance !== null && $paymentInstance->isRedirectEnabled()) { |
||
325 | return true; |
||
326 | } |
||
327 | |||
328 | return false; |
||
329 | } |
||
330 | |||
331 | /** |
||
332 | * Returns if the given amount of registrations for the event will be registrations for the waitlist |
||
333 | * (depending on the total amount of registrations and free places) |
||
334 | */ |
||
335 | public function isWaitlistRegistration(Event $event, int $amountOfRegistrations): bool |
||
336 | { |
||
337 | if ($event->getMaxParticipants() === 0 || !$event->getEnableWaitlist()) { |
||
338 | return false; |
||
339 | } |
||
340 | |||
341 | $result = false; |
||
342 | if (($event->getFreePlaces() > 0 && $event->getFreePlaces() < $amountOfRegistrations) |
||
343 | || $event->getFreePlaces() <= 0) { |
||
344 | $result = true; |
||
345 | } |
||
346 | |||
347 | return $result; |
||
348 | } |
||
349 | |||
350 | /** |
||
351 | * Handles the process of moving registration up from the waitlist. |
||
352 | */ |
||
353 | public function moveUpWaitlistRegistrations(Event $event, array $settings): void |
||
396 | } |
||
397 | } |
||
398 | } |
||
399 | } |
||
400 |