Total Complexity | 80 |
Total Lines | 392 |
Duplicated Lines | 0 % |
Changes | 3 | ||
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 |
||
40 | class RegistrationService |
||
41 | { |
||
42 | public function __construct( |
||
43 | protected readonly Context $context, |
||
44 | protected readonly EventDispatcherInterface $eventDispatcher, |
||
45 | protected readonly RegistrationRepository $registrationRepository, |
||
46 | protected readonly FrontendUserRepository $frontendUserRepository, |
||
47 | protected readonly HashService $hashService, |
||
48 | protected readonly PaymentService $paymentService, |
||
49 | protected readonly NotificationService $notificationService, |
||
50 | ) { |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * Duplicates the given registration (all public accessible properties) the |
||
55 | * amount of times configured in amountOfRegistrations |
||
56 | */ |
||
57 | public function createDependingRegistrations(Registration $registration): void |
||
58 | { |
||
59 | $registrations = $registration->getAmountOfRegistrations(); |
||
60 | for ($i = 1; $i <= $registrations - 1; $i++) { |
||
61 | $newReg = GeneralUtility::makeInstance(Registration::class); |
||
62 | $properties = ObjectAccess::getGettableProperties($registration); |
||
63 | foreach ($properties as $propertyName => $propertyValue) { |
||
64 | ObjectAccess::setProperty($newReg, $propertyName, $propertyValue); |
||
65 | } |
||
66 | $newReg->setMainRegistration($registration); |
||
67 | $newReg->setAmountOfRegistrations(1); |
||
68 | $newReg->setIgnoreNotifications(true); |
||
69 | $this->registrationRepository->add($newReg); |
||
70 | } |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * Confirms all depending registrations based on the given main registration |
||
75 | */ |
||
76 | public function confirmDependingRegistrations(Registration $registration): void |
||
77 | { |
||
78 | $registrations = $this->registrationRepository->findBy(['mainRegistration' => $registration]); |
||
79 | foreach ($registrations as $foundRegistration) { |
||
80 | /** @var Registration $foundRegistration */ |
||
81 | $foundRegistration->setConfirmed(true); |
||
82 | $this->registrationRepository->update($foundRegistration); |
||
83 | } |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * Checks if the registration can be confirmed and returns an array of variables |
||
88 | */ |
||
89 | public function checkConfirmRegistration(int $regUid, string $hmac): array |
||
90 | { |
||
91 | /* @var $registration Registration */ |
||
92 | $registration = null; |
||
93 | $failed = false; |
||
94 | $messageKey = 'event.message.confirmation_successful'; |
||
95 | $titleKey = 'confirmRegistration.title.successful'; |
||
96 | |||
97 | $isValidHmac = $this->hashService->validateHmac('reg-' . $regUid, HashScope::RegistrationUid->value, $hmac); |
||
98 | if (!$isValidHmac) { |
||
99 | $failed = true; |
||
100 | $messageKey = 'event.message.confirmation_failed_wrong_hmac'; |
||
101 | $titleKey = 'confirmRegistration.title.failed'; |
||
102 | } else { |
||
103 | $registration = $this->registrationRepository->findByUid($regUid); |
||
104 | } |
||
105 | |||
106 | if (!$failed && is_null($registration)) { |
||
107 | $failed = true; |
||
108 | $messageKey = 'event.message.confirmation_failed_registration_not_found'; |
||
109 | $titleKey = 'confirmRegistration.title.failed'; |
||
110 | } |
||
111 | |||
112 | if (!$failed && !$registration->getEvent()) { |
||
113 | $failed = true; |
||
114 | $messageKey = 'event.message.confirmation_failed_registration_event_not_found'; |
||
115 | $titleKey = 'confirmRegistration.title.failed'; |
||
116 | } |
||
117 | |||
118 | if (!$failed && !$registration->getConfirmed() && $registration->getConfirmationUntil() < new DateTime()) { |
||
119 | $failed = true; |
||
120 | $messageKey = 'event.message.confirmation_failed_confirmation_until_expired'; |
||
121 | $titleKey = 'confirmRegistration.title.failed'; |
||
122 | } |
||
123 | |||
124 | if (!$failed && $registration->getConfirmed() === true) { |
||
125 | $failed = true; |
||
126 | $messageKey = 'event.message.confirmation_failed_already_confirmed'; |
||
127 | $titleKey = 'confirmRegistration.title.failed'; |
||
128 | } |
||
129 | |||
130 | if (!$failed && $registration->getWaitlist()) { |
||
131 | $messageKey = 'event.message.confirmation_waitlist_successful'; |
||
132 | $titleKey = 'confirmRegistrationWaitlist.title.successful'; |
||
133 | } |
||
134 | |||
135 | return [ |
||
136 | $failed, |
||
137 | $registration, |
||
138 | $messageKey, |
||
139 | $titleKey, |
||
140 | ]; |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * Cancels all depending registrations based on the given main registration |
||
145 | */ |
||
146 | public function cancelDependingRegistrations(Registration $registration): void |
||
147 | { |
||
148 | $registrations = $this->registrationRepository->findBy(['mainRegistration' => $registration]); |
||
149 | foreach ($registrations as $foundRegistration) { |
||
150 | $this->registrationRepository->remove($foundRegistration); |
||
151 | } |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * Checks if the registration can be cancelled and returns an array of variables |
||
156 | */ |
||
157 | public function checkCancelRegistration(int $regUid, string $hmac): array |
||
211 | ]; |
||
212 | } |
||
213 | |||
214 | /** |
||
215 | * Returns the current frontend user object if available |
||
216 | */ |
||
217 | public function getCurrentFeUserObject(): ?FrontendUser |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * Checks, if the registration can successfully be created. |
||
232 | */ |
||
233 | public function checkRegistrationSuccess(Event $event, Registration $registration): array |
||
234 | { |
||
235 | $result = RegistrationResult::REGISTRATION_SUCCESSFUL; |
||
236 | $success = true; |
||
237 | if ($event->getEnableRegistration() === false) { |
||
238 | $success = false; |
||
239 | $result = RegistrationResult::REGISTRATION_NOT_ENABLED; |
||
240 | } elseif ($event->getRegistrationDeadline() != null && $event->getRegistrationDeadline() < new DateTime()) { |
||
241 | $success = false; |
||
242 | $result = RegistrationResult::REGISTRATION_FAILED_DEADLINE_EXPIRED; |
||
243 | } elseif (!$event->getAllowRegistrationUntilEnddate() && $event->getStartdate() < new DateTime()) { |
||
244 | $success = false; |
||
245 | $result = RegistrationResult::REGISTRATION_FAILED_EVENT_EXPIRED; |
||
246 | } elseif ($event->getAllowRegistrationUntilEnddate() && $event->getEnddate() && $event->getEnddate() < new DateTime()) { |
||
247 | $success = false; |
||
248 | $result = RegistrationResult::REGISTRATION_FAILED_EVENT_ENDED; |
||
249 | } elseif ($event->getRegistrations()->count() >= $event->getMaxParticipants() |
||
250 | && $event->getMaxParticipants() > 0 && !$event->getEnableWaitlist() |
||
251 | ) { |
||
252 | $success = false; |
||
253 | $result = RegistrationResult::REGISTRATION_FAILED_MAX_PARTICIPANTS; |
||
254 | } elseif ($event->getFreePlaces() < $registration->getAmountOfRegistrations() |
||
255 | && $event->getMaxParticipants() > 0 && !$event->getEnableWaitlist() |
||
256 | ) { |
||
257 | $success = false; |
||
258 | $result = RegistrationResult::REGISTRATION_FAILED_NOT_ENOUGH_FREE_PLACES; |
||
259 | } elseif ($event->getMaxRegistrationsPerUser() < $registration->getAmountOfRegistrations()) { |
||
260 | $success = false; |
||
261 | $result = RegistrationResult::REGISTRATION_FAILED_MAX_AMOUNT_REGISTRATIONS_EXCEEDED; |
||
262 | } elseif ($event->getUniqueEmailCheck() && |
||
263 | $this->emailNotUnique($event, $registration->getEmail()) |
||
264 | ) { |
||
265 | $success = false; |
||
266 | $result = RegistrationResult::REGISTRATION_FAILED_EMAIL_NOT_UNIQUE; |
||
267 | } elseif ($event->getRegistrations()->count() >= $event->getMaxParticipants() |
||
268 | && $event->getMaxParticipants() > 0 && $event->getEnableWaitlist() |
||
269 | ) { |
||
270 | $result = RegistrationResult::REGISTRATION_SUCCESSFUL_WAITLIST; |
||
271 | } |
||
272 | |||
273 | $modifyCheckRegistrationSuccessEvent = new ModifyCheckRegistrationSuccessEvent( |
||
274 | $success, |
||
275 | $result, |
||
276 | $registration |
||
277 | ); |
||
278 | $this->eventDispatcher->dispatch($modifyCheckRegistrationSuccessEvent); |
||
279 | |||
280 | return [$modifyCheckRegistrationSuccessEvent->getSuccess(), $modifyCheckRegistrationSuccessEvent->getResult()]; |
||
281 | } |
||
282 | |||
283 | /** |
||
284 | * Returns if the given email is registered to the given event |
||
285 | */ |
||
286 | protected function emailNotUnique(Event $event, string $email): bool |
||
287 | { |
||
288 | $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class) |
||
289 | ->getQueryBuilderForTable('tx_sfeventmgt_domain_model_registration'); |
||
290 | $queryBuilder->getRestrictions()->removeByType(HiddenRestriction::class); |
||
291 | $registrations = $queryBuilder->count('email') |
||
292 | ->from('tx_sfeventmgt_domain_model_registration') |
||
293 | ->where( |
||
294 | $queryBuilder->expr()->eq( |
||
295 | 'event', |
||
296 | $queryBuilder->createNamedParameter($event->getUid(), Connection::PARAM_INT) |
||
297 | ), |
||
298 | $queryBuilder->expr()->eq( |
||
299 | 'email', |
||
300 | $queryBuilder->createNamedParameter($email, Connection::PARAM_STR) |
||
301 | ) |
||
302 | ) |
||
303 | ->executeQuery() |
||
304 | ->fetchOne(); |
||
305 | |||
306 | return $registrations >= 1; |
||
307 | } |
||
308 | |||
309 | /** |
||
310 | * Returns, if payment redirect for the payment method is enabled |
||
311 | */ |
||
312 | public function redirectPaymentEnabled(Registration $registration): bool |
||
313 | { |
||
314 | if ($registration->getEvent()->getEnablePayment() === false) { |
||
315 | return false; |
||
316 | } |
||
317 | |||
318 | /** @var AbstractPayment $paymentInstance */ |
||
319 | $paymentInstance = $this->paymentService->getPaymentInstance($registration->getPaymentmethod()); |
||
320 | |||
321 | return $paymentInstance !== null && $paymentInstance->isRedirectEnabled(); |
||
322 | } |
||
323 | |||
324 | /** |
||
325 | * Returns if the given amount of registrations for the event will be registrations for the waitlist |
||
326 | * (depending on the total amount of registrations and free places) |
||
327 | */ |
||
328 | public function isWaitlistRegistration(Event $event, int $amountOfRegistrations): bool |
||
329 | { |
||
330 | if ($event->getMaxParticipants() === 0 || !$event->getEnableWaitlist()) { |
||
331 | return false; |
||
332 | } |
||
333 | |||
334 | $result = false; |
||
335 | if (($event->getFreePlaces() > 0 && $event->getFreePlaces() < $amountOfRegistrations) |
||
336 | || $event->getFreePlaces() <= 0) { |
||
337 | $result = true; |
||
338 | } |
||
339 | |||
340 | return $result; |
||
341 | } |
||
342 | |||
343 | /** |
||
344 | * Handles the process of moving registration up from the waitlist. |
||
345 | */ |
||
346 | public function moveUpWaitlistRegistrations(RequestInterface $request, Event $event, array $settings): void |
||
391 | } |
||
392 | } |
||
393 | } |
||
394 | |||
395 | /** |
||
396 | * Checks, if the given registration belongs to the current logged in frontend user. If not, a |
||
397 | * page not found response is thrown. |
||
398 | */ |
||
399 | public function checkRegistrationAccess(ServerRequestInterface $request, Registration $registration): void |
||
412 | } |
||
413 | } |
||
414 | |||
415 | /** |
||
416 | * Evaluates and returns the price for the given registration to the given event. |
||
417 | */ |
||
418 | public function evaluateRegistrationPrice( |
||
432 | } |
||
433 | } |
||
434 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths