| Total Complexity | 78 |
| Total Lines | 367 |
| Duplicated Lines | 0 % |
| Changes | 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 |
||
| 33 | class RegistrationService |
||
| 34 | { |
||
| 35 | protected EventDispatcherInterface $eventDispatcher; |
||
| 36 | protected RegistrationRepository $registrationRepository; |
||
| 37 | protected FrontendUserRepository $frontendUserRepository; |
||
| 38 | protected HashService $hashService; |
||
| 39 | protected PaymentService $paymentService; |
||
| 40 | protected NotificationService $notificationService; |
||
| 41 | |||
| 42 | public function injectFrontendUserRepository(FrontendUserRepository $frontendUserRepository): void |
||
| 43 | { |
||
| 44 | $this->frontendUserRepository = $frontendUserRepository; |
||
| 45 | } |
||
| 46 | |||
| 47 | public function injectHashService(HashService $hashService): void |
||
| 48 | { |
||
| 49 | $this->hashService = $hashService; |
||
| 50 | } |
||
| 51 | |||
| 52 | public function injectNotificationService(NotificationService $notificationService): void |
||
| 55 | } |
||
| 56 | |||
| 57 | public function injectEventDispatcher(EventDispatcherInterface $eventDispatcher): void |
||
| 58 | { |
||
| 59 | $this->eventDispatcher = $eventDispatcher; |
||
| 60 | } |
||
| 61 | |||
| 62 | public function injectPaymentService(PaymentService $paymentService): void |
||
| 63 | { |
||
| 64 | $this->paymentService = $paymentService; |
||
| 65 | } |
||
| 66 | |||
| 67 | public function injectRegistrationRepository(RegistrationRepository $registrationRepository): void |
||
| 68 | { |
||
| 69 | $this->registrationRepository = $registrationRepository; |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Duplicates the given registration (all public accessible properties) the |
||
| 74 | * amount of times configured in amountOfRegistrations |
||
| 75 | */ |
||
| 76 | public function createDependingRegistrations(Registration $registration): void |
||
| 77 | { |
||
| 78 | $registrations = $registration->getAmountOfRegistrations(); |
||
| 79 | for ($i = 1; $i <= $registrations - 1; $i++) { |
||
| 80 | $newReg = GeneralUtility::makeInstance(Registration::class); |
||
| 81 | $properties = ObjectAccess::getGettableProperties($registration); |
||
| 82 | foreach ($properties as $propertyName => $propertyValue) { |
||
| 83 | ObjectAccess::setProperty($newReg, $propertyName, $propertyValue); |
||
| 84 | } |
||
| 85 | $newReg->setMainRegistration($registration); |
||
| 86 | $newReg->setAmountOfRegistrations(1); |
||
| 87 | $newReg->setIgnoreNotifications(true); |
||
| 88 | $this->registrationRepository->add($newReg); |
||
| 89 | } |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Confirms all depending registrations based on the given main registration |
||
| 94 | */ |
||
| 95 | public function confirmDependingRegistrations(Registration $registration): void |
||
| 102 | } |
||
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Checks if the registration can be confirmed and returns an array of variables |
||
| 107 | */ |
||
| 108 | public function checkConfirmRegistration(int $regUid, string $hmac): array |
||
| 158 | ]; |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Cancels all depending registrations based on the given main registration |
||
| 163 | */ |
||
| 164 | public function cancelDependingRegistrations(Registration $registration): void |
||
| 165 | { |
||
| 166 | $registrations = $this->registrationRepository->findBy(['mainRegistration' => $registration]); |
||
| 167 | foreach ($registrations as $foundRegistration) { |
||
| 168 | $this->registrationRepository->remove($foundRegistration); |
||
| 169 | } |
||
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Checks if the registration can be cancelled and returns an array of variables |
||
| 174 | */ |
||
| 175 | public function checkCancelRegistration(int $regUid, string $hmac): array |
||
| 228 | ]; |
||
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Returns the current frontend user object if available |
||
| 233 | */ |
||
| 234 | public function getCurrentFeUserObject(): ?FrontendUser |
||
| 244 | } |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Checks, if the registration can successfully be created. |
||
| 248 | */ |
||
| 249 | public function checkRegistrationSuccess(Event $event, Registration $registration, int $result): array |
||
| 250 | { |
||
| 251 | $success = true; |
||
| 252 | if ($event->getEnableRegistration() === false) { |
||
| 253 | $success = false; |
||
| 254 | $result = RegistrationResult::REGISTRATION_NOT_ENABLED; |
||
| 255 | } elseif ($event->getRegistrationDeadline() != null && $event->getRegistrationDeadline() < new DateTime()) { |
||
| 256 | $success = false; |
||
| 257 | $result = RegistrationResult::REGISTRATION_FAILED_DEADLINE_EXPIRED; |
||
| 258 | } elseif (!$event->getAllowRegistrationUntilEnddate() && $event->getStartdate() < new DateTime()) { |
||
| 259 | $success = false; |
||
| 260 | $result = RegistrationResult::REGISTRATION_FAILED_EVENT_EXPIRED; |
||
| 261 | } elseif ($event->getAllowRegistrationUntilEnddate() && $event->getEnddate() && $event->getEnddate() < new DateTime()) { |
||
| 262 | $success = false; |
||
| 263 | $result = RegistrationResult::REGISTRATION_FAILED_EVENT_ENDED; |
||
| 264 | } elseif ($event->getRegistrations()->count() >= $event->getMaxParticipants() |
||
| 265 | && $event->getMaxParticipants() > 0 && !$event->getEnableWaitlist() |
||
| 266 | ) { |
||
| 267 | $success = false; |
||
| 268 | $result = RegistrationResult::REGISTRATION_FAILED_MAX_PARTICIPANTS; |
||
| 269 | } elseif ($event->getFreePlaces() < $registration->getAmountOfRegistrations() |
||
| 270 | && $event->getMaxParticipants() > 0 && !$event->getEnableWaitlist() |
||
| 271 | ) { |
||
| 272 | $success = false; |
||
| 273 | $result = RegistrationResult::REGISTRATION_FAILED_NOT_ENOUGH_FREE_PLACES; |
||
| 274 | } elseif ($event->getMaxRegistrationsPerUser() < $registration->getAmountOfRegistrations()) { |
||
| 275 | $success = false; |
||
| 276 | $result = RegistrationResult::REGISTRATION_FAILED_MAX_AMOUNT_REGISTRATIONS_EXCEEDED; |
||
| 277 | } elseif ($event->getUniqueEmailCheck() && |
||
| 278 | $this->emailNotUnique($event, $registration->getEmail()) |
||
| 279 | ) { |
||
| 280 | $success = false; |
||
| 281 | $result = RegistrationResult::REGISTRATION_FAILED_EMAIL_NOT_UNIQUE; |
||
| 282 | } elseif ($event->getRegistrations()->count() >= $event->getMaxParticipants() |
||
| 283 | && $event->getMaxParticipants() > 0 && $event->getEnableWaitlist() |
||
| 284 | ) { |
||
| 285 | $result = RegistrationResult::REGISTRATION_SUCCESSFUL_WAITLIST; |
||
| 286 | } |
||
| 287 | |||
| 288 | $modifyCheckRegistrationSuccessEvent = new ModifyCheckRegistrationSuccessEvent($success, $result); |
||
| 289 | $this->eventDispatcher->dispatch($modifyCheckRegistrationSuccessEvent); |
||
| 290 | |||
| 291 | return [$modifyCheckRegistrationSuccessEvent->getSuccess(), $modifyCheckRegistrationSuccessEvent->getResult()]; |
||
| 292 | } |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Returns if the given email is registered to the given event |
||
| 296 | */ |
||
| 297 | protected function emailNotUnique(Event $event, string $email): bool |
||
| 298 | { |
||
| 299 | $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class) |
||
| 300 | ->getQueryBuilderForTable('tx_sfeventmgt_domain_model_registration'); |
||
| 301 | $queryBuilder->getRestrictions()->removeByType(HiddenRestriction::class); |
||
| 302 | $registrations = $queryBuilder->count('email') |
||
| 303 | ->from('tx_sfeventmgt_domain_model_registration') |
||
| 304 | ->where( |
||
| 305 | $queryBuilder->expr()->eq( |
||
| 306 | 'event', |
||
| 307 | $queryBuilder->createNamedParameter($event->getUid(), Connection::PARAM_INT) |
||
| 308 | ), |
||
| 309 | $queryBuilder->expr()->eq( |
||
| 310 | 'email', |
||
| 311 | $queryBuilder->createNamedParameter($email, Connection::PARAM_STR) |
||
| 312 | ) |
||
| 313 | ) |
||
| 314 | ->executeQuery() |
||
| 315 | ->fetchOne(); |
||
| 316 | |||
| 317 | return $registrations >= 1; |
||
| 318 | } |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Returns, if payment redirect for the payment method is enabled |
||
| 322 | */ |
||
| 323 | public function redirectPaymentEnabled(Registration $registration): bool |
||
| 324 | { |
||
| 325 | if ($registration->getEvent()->getEnablePayment() === false) { |
||
| 326 | return false; |
||
| 327 | } |
||
| 328 | |||
| 329 | /** @var AbstractPayment $paymentInstance */ |
||
| 330 | $paymentInstance = $this->paymentService->getPaymentInstance($registration->getPaymentmethod()); |
||
| 331 | |||
| 332 | return $paymentInstance !== null && $paymentInstance->isRedirectEnabled(); |
||
| 333 | } |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Returns if the given amount of registrations for the event will be registrations for the waitlist |
||
| 337 | * (depending on the total amount of registrations and free places) |
||
| 338 | */ |
||
| 339 | public function isWaitlistRegistration(Event $event, int $amountOfRegistrations): bool |
||
| 340 | { |
||
| 341 | if ($event->getMaxParticipants() === 0 || !$event->getEnableWaitlist()) { |
||
| 342 | return false; |
||
| 343 | } |
||
| 344 | |||
| 345 | $result = false; |
||
| 346 | if (($event->getFreePlaces() > 0 && $event->getFreePlaces() < $amountOfRegistrations) |
||
| 347 | || $event->getFreePlaces() <= 0) { |
||
| 348 | $result = true; |
||
| 349 | } |
||
| 350 | |||
| 351 | return $result; |
||
| 352 | } |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Handles the process of moving registration up from the waitlist. |
||
| 356 | */ |
||
| 357 | public function moveUpWaitlistRegistrations(Event $event, array $settings): void |
||
| 400 | } |
||
| 401 | } |
||
| 402 | } |
||
| 403 | } |
||
| 404 |
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