| Total Complexity | 40 |
| Total Lines | 343 |
| 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 |
||
| 44 | class RequestHandler |
||
| 45 | { |
||
| 46 | /** |
||
| 47 | * @var Container |
||
| 48 | */ |
||
| 49 | private $di; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var ConfigManager |
||
| 53 | */ |
||
| 54 | protected $xConfigManager; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * The plugin manager. |
||
| 58 | * |
||
| 59 | * @var PluginManager |
||
| 60 | */ |
||
| 61 | private $xPluginManager; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * The response manager. |
||
| 65 | * |
||
| 66 | * @var ResponseManager |
||
| 67 | */ |
||
| 68 | private $xResponseManager; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * The callbacks to run while processing the request |
||
| 72 | * |
||
| 73 | * @var CallbackManager |
||
| 74 | */ |
||
| 75 | private $xCallbackManager; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var UploadHandler |
||
| 79 | */ |
||
| 80 | private $xUploadHandler; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * The data bag response plugin |
||
| 84 | * |
||
| 85 | * @var DataBagPlugin |
||
| 86 | */ |
||
| 87 | private $xDataBagPlugin; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @var Translator |
||
| 91 | */ |
||
| 92 | private $xTranslator; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * The request plugin that is able to process the current request |
||
| 96 | * |
||
| 97 | * @var RequestHandlerInterface |
||
| 98 | */ |
||
| 99 | private $xRequestPlugin = null; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @var ServerRequestInterface |
||
| 103 | */ |
||
| 104 | private $xRequest; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * The constructor |
||
| 108 | * |
||
| 109 | * @param Container $di |
||
| 110 | * @param ConfigManager $xConfigManager |
||
| 111 | * @param PluginManager $xPluginManager |
||
| 112 | * @param ResponseManager $xResponseManager |
||
| 113 | * @param CallbackManager $xCallbackManager |
||
| 114 | * @param ServerRequestInterface $xRequest |
||
| 115 | * @param UploadHandler|null $xUploadHandler |
||
| 116 | * @param DataBagPlugin $xDataBagPlugin |
||
| 117 | * @param Translator $xTranslator |
||
| 118 | */ |
||
| 119 | public function __construct(Container $di, ConfigManager $xConfigManager, PluginManager $xPluginManager, |
||
| 120 | ResponseManager $xResponseManager, CallbackManager $xCallbackManager, ServerRequestInterface $xRequest, |
||
| 121 | ?UploadHandler $xUploadHandler, DataBagPlugin $xDataBagPlugin, Translator $xTranslator) |
||
| 122 | { |
||
| 123 | $this->di = $di; |
||
| 124 | $this->xConfigManager = $xConfigManager; |
||
| 125 | $this->xPluginManager = $xPluginManager; |
||
| 126 | $this->xResponseManager = $xResponseManager; |
||
| 127 | $this->xCallbackManager = $xCallbackManager; |
||
| 128 | $this->xRequest = $xRequest; |
||
| 129 | $this->xUploadHandler = $xUploadHandler; |
||
| 130 | $this->xDataBagPlugin = $xDataBagPlugin; |
||
| 131 | $this->xTranslator = $xTranslator; |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * These callbacks are called whenever an invalid request is processed. |
||
| 136 | * |
||
| 137 | * @return void |
||
| 138 | */ |
||
| 139 | public function onBoot() |
||
| 140 | { |
||
| 141 | foreach($this->xCallbackManager->getBootCallbacks() as $xCallback) |
||
| 142 | { |
||
| 143 | call_user_func($xCallback); |
||
| 144 | } |
||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * These are the pre-request processing callbacks passed to the Jaxon library. |
||
| 149 | * |
||
| 150 | * @param bool $bEndRequest If set to true, the request processing is interrupted. |
||
| 151 | * |
||
| 152 | * @return void |
||
| 153 | */ |
||
| 154 | public function onBefore(bool &$bEndRequest) |
||
| 155 | { |
||
| 156 | $xTarget = $this->xRequestPlugin->getTarget(); |
||
| 157 | // Call the user defined callback |
||
| 158 | foreach($this->xCallbackManager->getBeforeCallbacks() as $xCallback) |
||
| 159 | { |
||
| 160 | $xReturn = call_user_func_array($xCallback, [$xTarget, &$bEndRequest]); |
||
| 161 | if($bEndRequest) |
||
| 162 | { |
||
| 163 | return; |
||
| 164 | } |
||
| 165 | if($xReturn instanceof AbstractResponse) |
||
| 166 | { |
||
| 167 | $this->xResponseManager->append($xReturn); |
||
| 168 | } |
||
| 169 | } |
||
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * These are the post-request processing callbacks passed to the Jaxon library. |
||
| 174 | * |
||
| 175 | * @return void |
||
| 176 | */ |
||
| 177 | public function onAfter(bool $bEndRequest) |
||
| 178 | { |
||
| 179 | foreach($this->xCallbackManager->getAfterCallbacks() as $xCallback) |
||
| 180 | { |
||
| 181 | $xReturn = call_user_func_array($xCallback, |
||
| 182 | [$this->xRequestPlugin->getTarget(), $bEndRequest]); |
||
| 183 | if($xReturn instanceof AbstractResponse) |
||
| 184 | { |
||
| 185 | $this->xResponseManager->append($xReturn); |
||
| 186 | } |
||
| 187 | } |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * These callbacks are called whenever an invalid request is processed. |
||
| 192 | * |
||
| 193 | * @return void |
||
| 194 | */ |
||
| 195 | public function onInvalid(string $sMessage) |
||
| 196 | { |
||
| 197 | foreach($this->xCallbackManager->getInvalidCallbacks() as $xCallback) |
||
| 198 | { |
||
| 199 | $xReturn = call_user_func($xCallback, $sMessage); |
||
| 200 | if($xReturn instanceof AbstractResponse) |
||
| 201 | { |
||
| 202 | $this->xResponseManager->append($xReturn); |
||
| 203 | } |
||
| 204 | } |
||
| 205 | } |
||
| 206 | |||
| 207 | /** |
||
| 208 | * These callbacks are called whenever an invalid request is processed. |
||
| 209 | * |
||
| 210 | * @var Exception $xException |
||
| 211 | * |
||
| 212 | * @return void |
||
| 213 | */ |
||
| 214 | public function onError(Exception $xException) |
||
| 215 | { |
||
| 216 | foreach($this->xCallbackManager->getErrorCallbacks() as $xCallback) |
||
| 217 | { |
||
| 218 | $xReturn = call_user_func($xCallback, $xException); |
||
| 219 | if($xReturn instanceof AbstractResponse) |
||
| 220 | { |
||
| 221 | $this->xResponseManager->append($xReturn); |
||
| 222 | } |
||
| 223 | } |
||
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Check if the current request can be processed |
||
| 228 | * |
||
| 229 | * Calls each of the request plugins and determines if the current request can be processed by one of them. |
||
| 230 | * |
||
| 231 | * @return bool |
||
| 232 | */ |
||
| 233 | public function canProcessRequest(): bool |
||
| 234 | { |
||
| 235 | // Return true if the request plugin was already found |
||
| 236 | if($this->xRequestPlugin !== null) |
||
| 237 | { |
||
| 238 | return true; |
||
| 239 | } |
||
| 240 | |||
| 241 | // Find a plugin to process the request |
||
| 242 | foreach($this->xPluginManager->getRequestHandlers() as $sClassName) |
||
| 243 | { |
||
| 244 | if($sClassName::canProcessRequest($this->xRequest)) |
||
| 245 | { |
||
| 246 | $this->xRequestPlugin = $this->di->g($sClassName); |
||
| 247 | return true; |
||
| 248 | } |
||
| 249 | } |
||
| 250 | |||
| 251 | // Check if the upload plugin is enabled |
||
| 252 | if($this->xUploadHandler === null) |
||
| 253 | { |
||
| 254 | return false; |
||
| 255 | } |
||
| 256 | |||
| 257 | // If no other plugin than the upload plugin can process the request, |
||
| 258 | // then it is an HTTP (not ajax) upload request |
||
| 259 | $this->xUploadHandler->isHttpUpload(); |
||
| 260 | return $this->xUploadHandler->canProcessRequest($this->xRequest); |
||
| 261 | } |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Process the current request and handle errors and exceptions. |
||
| 265 | * |
||
| 266 | * @return void |
||
| 267 | * @throws RequestException |
||
| 268 | * @throws SetupException |
||
| 269 | */ |
||
| 270 | private function _processRequest() |
||
| 317 | } |
||
| 318 | } |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Clean output buffers. |
||
| 322 | * |
||
| 323 | * @return void |
||
| 324 | */ |
||
| 325 | private function _cleanOutputBuffers() |
||
| 333 | } |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Process the current request. |
||
| 337 | * |
||
| 338 | * Calls each of the request plugins to request that they process the current request. |
||
| 339 | * If any plugin processes the request, it will return true. |
||
| 340 | * |
||
| 341 | * @return void |
||
| 342 | * @throws RequestException |
||
| 343 | * @throws SetupException |
||
| 344 | */ |
||
| 345 | public function processRequest() |
||
| 387 | } |
||
| 388 | } |
||
| 389 | } |
||
| 390 | } |
||
| 391 |