| Total Complexity | 42 |
| Total Lines | 378 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like RequestHandler 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 RequestHandler, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 43 | class RequestHandler |
||
| 44 | { |
||
| 45 | /** |
||
| 46 | * @var Container |
||
| 47 | */ |
||
| 48 | private $di; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var ConfigManager |
||
| 52 | */ |
||
| 53 | protected $xConfigManager; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * The plugin manager. |
||
| 57 | * |
||
| 58 | * @var PluginManager |
||
| 59 | */ |
||
| 60 | private $xPluginManager; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * The response manager. |
||
| 64 | * |
||
| 65 | * @var ResponseManager |
||
| 66 | */ |
||
| 67 | private $xResponseManager; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * The argument manager. |
||
| 71 | * |
||
| 72 | * @var ArgumentManager |
||
| 73 | */ |
||
| 74 | private $xArgumentManager; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * The callbacks to run while processing the request |
||
| 78 | * |
||
| 79 | * @var CallbackManager |
||
| 80 | */ |
||
| 81 | private $xCallbackManager; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var UploadHandler |
||
| 85 | */ |
||
| 86 | private $xUploadHandler; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * The data bag response plugin |
||
| 90 | * |
||
| 91 | * @var DataBagPlugin |
||
| 92 | */ |
||
| 93 | private $xDataBagPlugin; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @var Translator |
||
| 97 | */ |
||
| 98 | private $xTranslator; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * The request plugin that is able to process the current request |
||
| 102 | * |
||
| 103 | * @var RequestPlugin |
||
| 104 | */ |
||
| 105 | private $xTargetRequestPlugin = null; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * The constructor |
||
| 109 | * |
||
| 110 | * @param Container $di |
||
| 111 | * @param ConfigManager $xConfigManager |
||
| 112 | * @param ArgumentManager $xArgument |
||
| 113 | * @param PluginManager $xPluginManager |
||
| 114 | * @param ResponseManager $xResponseManager |
||
| 115 | * @param CallbackManager $xCallbackManager |
||
| 116 | * @param UploadHandler|null $xUploadHandler |
||
| 117 | * @param DataBagPlugin $xDataBagPlugin |
||
| 118 | * @param Translator $xTranslator |
||
| 119 | */ |
||
| 120 | public function __construct(Container $di, ConfigManager $xConfigManager, ArgumentManager $xArgument, |
||
| 121 | PluginManager $xPluginManager, ResponseManager $xResponseManager, CallbackManager $xCallbackManager, |
||
| 122 | ?UploadHandler $xUploadHandler, DataBagPlugin $xDataBagPlugin, Translator $xTranslator) |
||
| 123 | { |
||
| 124 | $this->di = $di; |
||
| 125 | $this->xConfigManager = $xConfigManager; |
||
| 126 | $this->xPluginManager = $xPluginManager; |
||
| 127 | $this->xResponseManager = $xResponseManager; |
||
| 128 | $this->xArgumentManager = $xArgument; |
||
| 129 | $this->xCallbackManager = $xCallbackManager; |
||
| 130 | $this->xUploadHandler = $xUploadHandler; |
||
| 131 | $this->xDataBagPlugin = $xDataBagPlugin; |
||
| 132 | $this->xTranslator = $xTranslator; |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Return the method that was used to send the arguments from the client |
||
| 137 | * |
||
| 138 | * The method is one of: ArgumentManager::METHOD_UNKNOWN, ArgumentManager::METHOD_GET, ArgumentManager::METHOD_POST. |
||
| 139 | * |
||
| 140 | * @return int |
||
| 141 | */ |
||
| 142 | public function getRequestMethod(): int |
||
| 143 | { |
||
| 144 | return $this->xArgumentManager->getRequestMethod(); |
||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Return the array of arguments that were extracted and parsed from the GET or POST data |
||
| 149 | * |
||
| 150 | * @return array |
||
| 151 | * @throws RequestException |
||
| 152 | */ |
||
| 153 | public function processArguments(): array |
||
| 154 | { |
||
| 155 | return $this->xArgumentManager->process(); |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Get the callback handler |
||
| 160 | * |
||
| 161 | * @return CallbackManager |
||
| 162 | */ |
||
| 163 | public function getCallbackManager(): CallbackManager |
||
| 164 | { |
||
| 165 | return $this->xCallbackManager; |
||
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * These callbacks are called whenever an invalid request is processed. |
||
| 170 | * |
||
| 171 | * @return void |
||
| 172 | */ |
||
| 173 | public function onBoot() |
||
| 174 | { |
||
| 175 | foreach($this->xCallbackManager->getBootCallbacks() as $xCallback) |
||
| 176 | { |
||
| 177 | call_user_func($xCallback); |
||
| 178 | } |
||
| 179 | } |
||
| 180 | |||
| 181 | /** |
||
| 182 | * These are the pre-request processing callbacks passed to the Jaxon library. |
||
| 183 | * |
||
| 184 | * @param bool $bEndRequest If set to true, the request processing is interrupted. |
||
| 185 | * |
||
| 186 | * @return void |
||
| 187 | */ |
||
| 188 | public function onBefore(bool &$bEndRequest) |
||
| 189 | { |
||
| 190 | $xTarget = $this->xTargetRequestPlugin->getTarget(); |
||
| 191 | // Call the user defined callback |
||
| 192 | foreach($this->xCallbackManager->getBeforeCallbacks() as $xCallback) |
||
| 193 | { |
||
| 194 | $xReturn = call_user_func_array($xCallback, [$xTarget, &$bEndRequest]); |
||
| 195 | if($bEndRequest) |
||
| 196 | { |
||
| 197 | return; |
||
| 198 | } |
||
| 199 | if($xReturn instanceof AbstractResponse) |
||
| 200 | { |
||
| 201 | $this->xResponseManager->append($xReturn); |
||
| 202 | } |
||
| 203 | } |
||
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * These are the post-request processing callbacks passed to the Jaxon library. |
||
| 208 | * |
||
| 209 | * @return void |
||
| 210 | */ |
||
| 211 | public function onAfter(bool $bEndRequest) |
||
| 212 | { |
||
| 213 | foreach($this->xCallbackManager->getAfterCallbacks() as $xCallback) |
||
| 214 | { |
||
| 215 | $xReturn = call_user_func_array($xCallback, |
||
| 216 | [$this->xTargetRequestPlugin->getTarget(), $bEndRequest]); |
||
| 217 | if($xReturn instanceof AbstractResponse) |
||
| 218 | { |
||
| 219 | $this->xResponseManager->append($xReturn); |
||
| 220 | } |
||
| 221 | } |
||
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * These callbacks are called whenever an invalid request is processed. |
||
| 226 | * |
||
| 227 | * @return void |
||
| 228 | */ |
||
| 229 | public function onInvalid(string $sMessage) |
||
| 230 | { |
||
| 231 | foreach($this->xCallbackManager->getInvalidCallbacks() as $xCallback) |
||
| 232 | { |
||
| 233 | $xReturn = call_user_func($xCallback, $sMessage); |
||
| 234 | if($xReturn instanceof AbstractResponse) |
||
| 235 | { |
||
| 236 | $this->xResponseManager->append($xReturn); |
||
| 237 | } |
||
| 238 | } |
||
| 239 | } |
||
| 240 | |||
| 241 | /** |
||
| 242 | * These callbacks are called whenever an invalid request is processed. |
||
| 243 | * |
||
| 244 | * @var Exception $xException |
||
| 245 | * |
||
| 246 | * @return void |
||
| 247 | */ |
||
| 248 | public function onError(Exception $xException) |
||
| 249 | { |
||
| 250 | foreach($this->xCallbackManager->getErrorCallbacks() as $xCallback) |
||
| 251 | { |
||
| 252 | $xReturn = call_user_func($xCallback, $xException); |
||
| 253 | if($xReturn instanceof AbstractResponse) |
||
| 254 | { |
||
| 255 | $this->xResponseManager->append($xReturn); |
||
| 256 | } |
||
| 257 | } |
||
| 258 | } |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Check if the current request can be processed |
||
| 262 | * |
||
| 263 | * Calls each of the request plugins and determines if the current request can be processed by one of them. |
||
| 264 | * |
||
| 265 | * @return bool |
||
| 266 | */ |
||
| 267 | public function canProcessRequest(): bool |
||
| 268 | { |
||
| 269 | // Return true if the request plugin was already found |
||
| 270 | if($this->xTargetRequestPlugin !== null) |
||
| 271 | { |
||
| 272 | return true; |
||
| 273 | } |
||
| 274 | |||
| 275 | // Find a plugin to process the request |
||
| 276 | foreach($this->xPluginManager->getRequestPlugins() as $sClassName) |
||
| 277 | { |
||
| 278 | if($sClassName::canProcessRequest()) |
||
| 279 | { |
||
| 280 | $this->xTargetRequestPlugin = $this->di->get($sClassName); |
||
| 281 | return true; |
||
| 282 | } |
||
| 283 | } |
||
| 284 | |||
| 285 | // Check if the upload plugin is enabled |
||
| 286 | if($this->xUploadHandler === null) |
||
| 287 | { |
||
| 288 | return false; |
||
| 289 | } |
||
| 290 | |||
| 291 | // If no other plugin than the upload plugin can process the request, |
||
| 292 | // then it is an HTTP (not ajax) upload request |
||
| 293 | $this->xUploadHandler->isHttpUpload(); |
||
| 294 | return $this->xUploadHandler->canProcessRequest(); |
||
| 295 | } |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Process the current request and handle errors and exceptions. |
||
| 299 | * |
||
| 300 | * @return void |
||
| 301 | * @throws RequestException |
||
| 302 | * @throws SetupException |
||
| 303 | */ |
||
| 304 | private function _processRequest() |
||
| 351 | } |
||
| 352 | } |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Clean output buffers. |
||
| 356 | * |
||
| 357 | * @return void |
||
| 358 | */ |
||
| 359 | private function _cleanOutputBuffers() |
||
| 367 | } |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Process the current request. |
||
| 371 | * |
||
| 372 | * Calls each of the request plugins to request that they process the current request. |
||
| 373 | * If any plugin processes the request, it will return true. |
||
| 374 | * |
||
| 375 | * @return void |
||
| 376 | * @throws RequestException |
||
| 377 | * @throws SetupException |
||
| 378 | */ |
||
| 379 | public function processRequest() |
||
| 421 | } |
||
| 422 | } |
||
| 423 | } |
||
| 424 | } |
||
| 425 |