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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 |
||
| 27 | class RegistrationService |
||
| 28 | { |
||
| 29 | |||
| 30 | /** |
||
| 31 | * The object manager |
||
| 32 | * |
||
| 33 | * @var \TYPO3\CMS\Extbase\Object\ObjectManager |
||
| 34 | * @inject |
||
| 35 | */ |
||
| 36 | protected $objectManager; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * RegistrationRepository |
||
| 40 | * |
||
| 41 | * @var \DERHANSEN\SfEventMgt\Domain\Repository\RegistrationRepository |
||
| 42 | * @inject |
||
| 43 | */ |
||
| 44 | protected $registrationRepository; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * FrontendUserRepository |
||
| 48 | * |
||
| 49 | * @var \TYPO3\CMS\Extbase\Domain\Repository\FrontendUserRepository |
||
| 50 | * @inject |
||
| 51 | */ |
||
| 52 | protected $frontendUserRepository; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Hash Service |
||
| 56 | * |
||
| 57 | * @var \TYPO3\CMS\Extbase\Security\Cryptography\HashService |
||
| 58 | * @inject |
||
| 59 | */ |
||
| 60 | protected $hashService; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Handles expired registrations. If the $delete parameter is set, then |
||
| 64 | * registrations are deleted, else just hidden |
||
| 65 | * |
||
| 66 | * @param bool $delete Delete |
||
| 67 | * |
||
| 68 | * @return void |
||
| 69 | */ |
||
| 70 | 2 | public function handleExpiredRegistrations($delete = false) |
|
| 85 | |||
| 86 | /** |
||
| 87 | * Duplicates (all public accessable properties) the given registration the |
||
| 88 | * amount of times configured in amountOfRegistrations |
||
| 89 | * |
||
| 90 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration |
||
| 91 | * |
||
| 92 | * @return void |
||
| 93 | */ |
||
| 94 | 1 | public function createDependingRegistrations($registration) |
|
| 95 | { |
||
| 96 | 1 | $registrations = $registration->getAmountOfRegistrations(); |
|
| 97 | 1 | for ($i = 1; $i <= $registrations - 1; $i++) { |
|
| 98 | /** @var \DERHANSEN\SfEventMgt\Domain\Model\Registration $newReg */ |
||
| 99 | 1 | $newReg = $this->objectManager->get('DERHANSEN\SfEventMgt\Domain\Model\Registration'); |
|
| 100 | 1 | $properties = ObjectAccess::getGettableProperties($registration); |
|
| 101 | 1 | foreach ($properties as $propertyName => $propertyValue) { |
|
| 102 | 1 | ObjectAccess::setProperty($newReg, $propertyName, $propertyValue); |
|
| 103 | 1 | } |
|
| 104 | 1 | $newReg->setMainRegistration($registration); |
|
| 105 | 1 | $newReg->setAmountOfRegistrations(1); |
|
| 106 | 1 | $newReg->setIgnoreNotifications(true); |
|
| 107 | 1 | $this->registrationRepository->add($newReg); |
|
| 108 | 1 | } |
|
| 109 | 1 | } |
|
| 110 | |||
| 111 | /** |
||
| 112 | * Confirms all depending registrations based on the given main registration |
||
| 113 | * |
||
| 114 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration |
||
| 115 | * |
||
| 116 | * @return void |
||
| 117 | */ |
||
| 118 | 1 | public function confirmDependingRegistrations($registration) |
|
| 119 | { |
||
| 120 | 1 | $registrations = $this->registrationRepository->findByMainRegistration($registration); |
|
| 121 | 1 | foreach ($registrations as $foundRegistration) { |
|
| 122 | /** @var \DERHANSEN\SfEventMgt\Domain\Model\Registration $foundRegistration */ |
||
| 123 | 1 | $foundRegistration->setConfirmed(true); |
|
| 124 | 1 | $this->registrationRepository->update($foundRegistration); |
|
| 125 | 1 | } |
|
| 126 | 1 | } |
|
| 127 | |||
| 128 | /** |
||
| 129 | * Checks if the registration can be confirmed and returns an array of variables |
||
| 130 | * |
||
| 131 | * @param int $reguid UID of registration |
||
| 132 | * @param string $hmac HMAC for parameters |
||
| 133 | * |
||
| 134 | * @return array |
||
| 135 | */ |
||
| 136 | 4 | public function checkConfirmRegistration($reguid, $hmac) |
|
| 137 | { |
||
| 138 | /* @var $registration Registration */ |
||
| 139 | 4 | $registration = null; |
|
| 140 | 4 | $failed = false; |
|
| 141 | 4 | $messageKey = 'event.message.confirmation_successful'; |
|
| 142 | 4 | $titleKey = 'confirmRegistration.title.successful'; |
|
| 143 | |||
| 144 | 4 | if (!$this->hashService->validateHmac('reg-' . $reguid, $hmac)) { |
|
| 145 | 1 | $failed = true; |
|
| 146 | 1 | $messageKey = 'event.message.confirmation_failed_wrong_hmac'; |
|
| 147 | 1 | $titleKey = 'confirmRegistration.title.failed'; |
|
| 148 | 1 | } else { |
|
| 149 | 3 | $registration = $this->registrationRepository->findByUid($reguid); |
|
| 150 | } |
||
| 151 | |||
| 152 | 4 | if (!$failed && is_null($registration)) { |
|
| 153 | 1 | $failed = true; |
|
| 154 | 1 | $messageKey = 'event.message.confirmation_failed_registration_not_found'; |
|
| 155 | 1 | $titleKey = 'confirmRegistration.title.failed'; |
|
| 156 | 1 | } |
|
| 157 | |||
| 158 | 4 | if (!$failed && $registration->getConfirmationUntil() < new \DateTime()) { |
|
| 159 | 1 | $failed = true; |
|
| 160 | 1 | $messageKey = 'event.message.confirmation_failed_confirmation_until_expired'; |
|
| 161 | 1 | $titleKey = 'confirmRegistration.title.failed'; |
|
| 162 | 1 | } |
|
| 163 | |||
| 164 | 4 | if (!$failed && $registration->getConfirmed() === true) { |
|
| 165 | 1 | $failed = true; |
|
| 166 | 1 | $messageKey = 'event.message.confirmation_failed_already_confirmed'; |
|
| 167 | 1 | $titleKey = 'confirmRegistration.title.failed'; |
|
| 168 | 1 | } |
|
| 169 | |||
| 170 | return [ |
||
| 171 | 4 | $failed, |
|
| 172 | 4 | $registration, |
|
| 173 | 4 | $messageKey, |
|
| 174 | $titleKey |
||
| 175 | 4 | ]; |
|
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Cancels all depending registrations based on the given main registration |
||
| 180 | * |
||
| 181 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration |
||
| 182 | * |
||
| 183 | * @return void |
||
| 184 | */ |
||
| 185 | 1 | public function cancelDependingRegistrations($registration) |
|
| 192 | |||
| 193 | /** |
||
| 194 | * Checks if the registration can be cancelled and returns an array of variables |
||
| 195 | * |
||
| 196 | * @param int $reguid UID of registration |
||
| 197 | * @param string $hmac HMAC for parameters |
||
| 198 | * |
||
| 199 | * @return array |
||
| 200 | */ |
||
| 201 | 4 | public function checkCancelRegistration($reguid, $hmac) |
|
| 202 | { |
||
| 203 | /* @var $registration Registration */ |
||
| 204 | 4 | $registration = null; |
|
| 205 | 4 | $failed = false; |
|
| 206 | 4 | $messageKey = 'event.message.cancel_successful'; |
|
| 207 | 4 | $titleKey = 'cancelRegistration.title.successful'; |
|
| 208 | |||
| 209 | 4 | if (!$this->hashService->validateHmac('reg-' . $reguid, $hmac)) { |
|
| 210 | 1 | $failed = true; |
|
| 211 | 1 | $messageKey = 'event.message.cancel_failed_wrong_hmac'; |
|
| 212 | 1 | $titleKey = 'cancelRegistration.title.failed'; |
|
| 213 | 1 | } else { |
|
| 214 | 3 | $registration = $this->registrationRepository->findByUid($reguid); |
|
| 215 | } |
||
| 216 | |||
| 217 | 4 | if (!$failed && is_null($registration)) { |
|
| 218 | 1 | $failed = true; |
|
| 219 | 1 | $messageKey = 'event.message.cancel_failed_registration_not_found_or_cancelled'; |
|
| 220 | 1 | $titleKey = 'cancelRegistration.title.failed'; |
|
| 221 | 1 | } |
|
| 222 | |||
| 223 | 4 | if (!$failed && $registration->getEvent()->getEnableCancel() === false) { |
|
| 224 | 1 | $failed = true; |
|
| 225 | 1 | $messageKey = 'event.message.confirmation_failed_cancel_disabled'; |
|
| 226 | 1 | $titleKey = 'cancelRegistration.title.failed'; |
|
| 227 | 1 | } |
|
| 228 | |||
| 229 | 4 | if (!$failed && $registration->getEvent()->getCancelDeadline() > 0 |
|
| 230 | 4 | && $registration->getEvent()->getCancelDeadline() < new \DateTime() |
|
| 231 | 4 | ) { |
|
| 232 | 1 | $failed = true; |
|
| 233 | 1 | $messageKey = 'event.message.cancel_failed_deadline_expired'; |
|
| 234 | 1 | $titleKey = 'cancelRegistration.title.failed'; |
|
| 235 | 1 | } |
|
| 236 | |||
| 237 | return [ |
||
| 238 | 4 | $failed, |
|
| 239 | 4 | $registration, |
|
| 240 | 4 | $messageKey, |
|
| 241 | $titleKey |
||
| 242 | 4 | ]; |
|
| 243 | } |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Returns the current frontend user object if available |
||
| 247 | * |
||
| 248 | * @return \TYPO3\CMS\Extbase\Domain\Model\FrontendUser|null |
||
| 249 | */ |
||
| 250 | 4 | public function getCurrentFeUserObject() |
|
| 258 | |||
| 259 | /** |
||
| 260 | * Checks, if the registration can successfully be created. Note, that |
||
| 261 | * $result is passed by reference! |
||
| 262 | * |
||
| 263 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event |
||
| 264 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration |
||
| 265 | * @param RegistrationResult $result Result |
||
| 266 | * |
||
| 267 | * @return bool |
||
| 268 | */ |
||
| 269 | 18 | public function checkRegistrationSuccess($event, $registration, &$result) |
|
| 302 | |||
| 303 | /** |
||
| 304 | * Returns if the given e-mail is registered to the given event |
||
| 305 | * |
||
| 306 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event |
||
| 307 | * @param string $email |
||
| 308 | * @return bool |
||
| 309 | */ |
||
| 310 | 2 | protected function emailNotUnique($event, $email) |
|
| 315 | } |