Complex classes like RestrictionService 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 RestrictionService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 47 | class RestrictionService |
||
| 48 | { |
||
| 49 | /** |
||
| 50 | * @var boolean |
||
| 51 | */ |
||
| 52 | protected static $preventFailureCount = false; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var RestrictionIdentifierInterface |
||
| 56 | */ |
||
| 57 | protected $restrictionIdentifier; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var string |
||
| 61 | */ |
||
| 62 | protected $clientIdentifier; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var \Aoe\FeloginBruteforceProtection\System\Configuration |
||
| 66 | */ |
||
| 67 | protected $configuration; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var \Aoe\FeloginBruteforceProtection\Domain\Repository\EntryRepository |
||
| 71 | */ |
||
| 72 | protected $entryRepository; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var \TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager |
||
| 76 | */ |
||
| 77 | protected $persistenceManager; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface |
||
| 81 | */ |
||
| 82 | protected $objectManager; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var Entry |
||
| 86 | */ |
||
| 87 | protected $entry; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @var boolean |
||
| 91 | */ |
||
| 92 | protected $clientRestricted; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @var Logger |
||
| 96 | */ |
||
| 97 | protected $logger; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @var FeLoginBruteForceApi |
||
| 101 | */ |
||
| 102 | protected $feLoginBruteForceApi; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @param RestrictionIdentifierInterface $restrictionIdentifier |
||
| 106 | */ |
||
| 107 | public function __construct(RestrictionIdentifierInterface $restrictionIdentifier) |
||
| 108 | { |
||
| 109 | $this->objectManager = GeneralUtility::makeInstance(ObjectManager::class); |
||
| 110 | $this->restrictionIdentifier = $restrictionIdentifier; |
||
| 111 | |||
| 112 | $this->configuration = $this->objectManager->get(Configuration::class); |
||
| 113 | $this->persistenceManager = $this->objectManager->get(PersistenceManager::class); |
||
| 114 | $this->entryRepository = $this->objectManager->get(EntryRepository::class); |
||
| 115 | |||
| 116 | |||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @param boolean $preventFailureCount |
||
| 121 | * @return void |
||
| 122 | */ |
||
| 123 | public static function setPreventFailureCount($preventFailureCount) |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @return boolean |
||
| 130 | */ |
||
| 131 | public function isClientRestricted() |
||
| 132 | { |
||
| 133 | if (false === isset($this->clientRestricted)) { |
||
| 134 | if ($this->hasEntry() && $this->isRestricted($this->getEntry())) { |
||
|
|
|||
| 135 | $this->clientRestricted = true; |
||
| 136 | } else { |
||
| 137 | $this->clientRestricted = false; |
||
| 138 | } |
||
| 139 | } |
||
| 140 | return $this->clientRestricted; |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @return void |
||
| 145 | */ |
||
| 146 | public function removeEntry() |
||
| 147 | { |
||
| 148 | if ($this->hasEntry()) { |
||
| 149 | $this->entryRepository->remove($this->entry); |
||
| 150 | $this->persistenceManager->persistAll(); |
||
| 151 | |||
| 152 | $this->log('Bruteforce Counter removed', LoggerInterface::SEVERITY_INFO); |
||
| 153 | } |
||
| 154 | $this->clientRestricted = false; |
||
| 155 | unset($this->entry); |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @return void |
||
| 160 | */ |
||
| 161 | public function checkAndHandleRestriction() |
||
| 185 | |||
| 186 | /** |
||
| 187 | * @return void |
||
| 188 | */ |
||
| 189 | protected function restrictionLog() |
||
| 204 | |||
| 205 | /** |
||
| 206 | * @param $message |
||
| 207 | * @param $severity |
||
| 208 | */ |
||
| 209 | private function log($message, $severity) |
||
| 210 | { |
||
| 211 | $failureCount = 0; |
||
| 212 | if ($this->hasEntry()) { |
||
| 213 | $failureCount = $this->getEntry()->getFailures(); |
||
| 214 | } |
||
| 215 | if ($this->isClientRestricted()) { |
||
| 216 | $restricted = 'Yes'; |
||
| 217 | } else { |
||
| 218 | $restricted = 'No'; |
||
| 219 | } |
||
| 220 | $additionalData = array( |
||
| 221 | 'FAILURE_COUNT' => $failureCount, |
||
| 222 | 'RESTRICTED' => $restricted, |
||
| 223 | 'REMOTE_ADDR' => GeneralUtility::getIndpEnv('REMOTE_ADDR'), |
||
| 224 | 'REQUEST_URI' => GeneralUtility::getIndpEnv('REQUEST_URI'), |
||
| 225 | 'HTTP_USER_AGENT' => GeneralUtility::getIndpEnv('HTTP_USER_AGENT') |
||
| 226 | ); |
||
| 227 | |||
| 228 | $this->getLogger()->log($message, $severity, $additionalData, 'felogin_bruteforce_protection'); |
||
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * @return Logger |
||
| 233 | */ |
||
| 234 | private function getLogger() |
||
| 235 | { |
||
| 236 | if (!isset($this->logger)) { |
||
| 237 | $this->logger = new Logger(); |
||
| 238 | } |
||
| 239 | return $this->logger; |
||
| 240 | } |
||
| 241 | |||
| 242 | /** |
||
| 243 | * @return void |
||
| 244 | */ |
||
| 245 | private function createEntry() |
||
| 257 | |||
| 258 | /** |
||
| 259 | * @return void |
||
| 260 | */ |
||
| 261 | private function saveEntry() |
||
| 272 | |||
| 273 | /** |
||
| 274 | * @param Entry $entry |
||
| 275 | * @return boolean |
||
| 276 | */ |
||
| 277 | private function isRestricted(Entry $entry) |
||
| 278 | { |
||
| 279 | if ($this->hasMaximumNumberOfFailuresReached($entry)) { |
||
| 280 | if (false === $this->isRestrictionTimeReached($entry)) { |
||
| 281 | return true; |
||
| 282 | } |
||
| 283 | } |
||
| 284 | return false; |
||
| 285 | } |
||
| 286 | |||
| 287 | /** |
||
| 288 | * @return boolean |
||
| 289 | */ |
||
| 290 | public function hasEntry() |
||
| 291 | { |
||
| 292 | return ($this->getEntry() instanceof Entry); |
||
| 293 | } |
||
| 294 | |||
| 295 | /** |
||
| 296 | * @return Entry|null |
||
| 297 | */ |
||
| 298 | public function getEntry() |
||
| 299 | { |
||
| 300 | if (false === isset($this->entry)) { |
||
| 301 | $entry = $this->entryRepository->findByIdentifier($this->getClientIdentifier()); |
||
| 302 | if ($entry instanceof Entry) { |
||
| 303 | $this->entry = $entry; |
||
| 304 | if ($this->isOutdated($entry)) { |
||
| 305 | $this->removeEntry(); |
||
| 306 | } |
||
| 307 | } |
||
| 308 | } |
||
| 309 | return $this->entry; |
||
| 310 | } |
||
| 311 | |||
| 312 | /** |
||
| 313 | * @param Entry $entry |
||
| 314 | * @return boolean |
||
| 315 | */ |
||
| 316 | private function isOutdated(Entry $entry) |
||
| 317 | { |
||
| 318 | return ( |
||
| 319 | ($this->hasMaximumNumberOfFailuresReached($entry) && $this->isRestrictionTimeReached($entry)) || |
||
| 320 | (false === $this->hasMaximumNumberOfFailuresReached($entry) && $this->isResetTimeOver($entry)) |
||
| 321 | ); |
||
| 322 | } |
||
| 323 | |||
| 324 | /** |
||
| 325 | * @param Entry $entry |
||
| 326 | * @return boolean |
||
| 327 | */ |
||
| 328 | private function isResetTimeOver(Entry $entry) |
||
| 329 | { |
||
| 330 | return ($entry->getCrdate() < time() - $this->configuration->getResetTime()); |
||
| 331 | } |
||
| 332 | |||
| 333 | /** |
||
| 334 | * @param Entry $entry |
||
| 335 | * @return boolean |
||
| 336 | */ |
||
| 337 | private function hasMaximumNumberOfFailuresReached(Entry $entry) |
||
| 338 | { |
||
| 339 | return ($entry->getFailures() >= $this->configuration->getMaximumNumberOfFailures()); |
||
| 340 | } |
||
| 341 | |||
| 342 | /** |
||
| 343 | * @param Entry $entry |
||
| 344 | * @return boolean |
||
| 345 | */ |
||
| 346 | private function isRestrictionTimeReached(Entry $entry) |
||
| 347 | { |
||
| 348 | return ($entry->getTstamp() < time() - $this->configuration->getRestrictionTime()); |
||
| 349 | } |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Returns the client identifier based on the clients IP address. |
||
| 353 | * |
||
| 354 | * @return string |
||
| 355 | */ |
||
| 356 | private function getClientIdentifier() |
||
| 357 | { |
||
| 358 | if (false === isset($this->clientIdentifier)) { |
||
| 359 | $this->clientIdentifier = md5( |
||
| 360 | $this->restrictionIdentifier->getIdentifierValue() . $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] |
||
| 361 | ); |
||
| 362 | } |
||
| 363 | return $this->clientIdentifier; |
||
| 364 | } |
||
| 365 | |||
| 366 | /** |
||
| 367 | * @return FeLoginBruteForceApi |
||
| 368 | */ |
||
| 369 | protected function getFeLoginBruteForceApi() |
||
| 378 | } |
||
| 379 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: