| Total Complexity | 54 |
| Total Lines | 394 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like TActiveFileUpload 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 TActiveFileUpload, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 61 | class TActiveFileUpload extends TFileUpload implements IActiveControl, ICallbackEventHandler, INamingContainer |
||
| 62 | { |
||
| 63 | const SCRIPT_PATH = 'activefileupload'; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var THiddenField a flag to tell which component is doing the callback. |
||
| 67 | */ |
||
| 68 | private $_flag; |
||
| 69 | /** |
||
| 70 | * @var TImage that spins to show that the file is being uploaded. |
||
| 71 | */ |
||
| 72 | private $_busy; |
||
| 73 | /** |
||
| 74 | * @var TImage that shows a green check mark for completed upload. |
||
| 75 | */ |
||
| 76 | private $_success; |
||
| 77 | /** |
||
| 78 | * @var TImage that shows a red X for incomplete upload. |
||
| 79 | */ |
||
| 80 | private $_error; |
||
| 81 | /** |
||
| 82 | * @var TInlineFrame used to submit the data in an "asynchronous" fashion. |
||
| 83 | */ |
||
| 84 | private $_target; |
||
| 85 | /** |
||
| 86 | * @var class name used to instantiate items for uploaded files: {@link TFileUploadItem} |
||
|
|
|||
| 87 | */ |
||
| 88 | protected static $fileUploadItemClass = '\Prado\Web\UI\ActiveControls\TActiveFileUploadItem'; |
||
| 89 | |||
| 90 | |||
| 91 | /** |
||
| 92 | * Creates a new callback control, sets the adapter to |
||
| 93 | * TActiveControlAdapter. If you override this class, be sure to set the |
||
| 94 | * adapter appropriately by, for example, by calling this constructor. |
||
| 95 | */ |
||
| 96 | public function __construct() |
||
| 97 | { |
||
| 98 | parent::__construct(); |
||
| 99 | $this->setAdapter(new TActiveControlAdapter($this)); |
||
| 100 | } |
||
| 101 | |||
| 102 | |||
| 103 | /** |
||
| 104 | * @param string $file asset file in the self::SCRIPT_PATH directory. |
||
| 105 | * @return string asset file url. |
||
| 106 | */ |
||
| 107 | protected function getAssetUrl($file = '') |
||
| 108 | { |
||
| 109 | $base = $this->getPage()->getClientScript()->getPradoScriptAssetUrl(); |
||
| 110 | return $base . '/' . self::SCRIPT_PATH . '/' . $file; |
||
| 111 | } |
||
| 112 | |||
| 113 | |||
| 114 | /** |
||
| 115 | * This method is invoked when a file is uploaded. |
||
| 116 | * If you override this method, be sure to call the parent implementation to ensure |
||
| 117 | * the invocation of the attached event handlers. |
||
| 118 | * @param TEventParameter $param event parameter to be passed to the event handlers |
||
| 119 | */ |
||
| 120 | public function onFileUpload($param) |
||
| 121 | { |
||
| 122 | if ($this->_flag->getValue() && $this->getPage()->getIsPostBack() && $param == $this->_target->getUniqueID()) { |
||
| 123 | $params = new TActiveFileUploadCallbackParams; |
||
| 124 | // save the files so that they will persist past the end of this return. |
||
| 125 | foreach ($this->getFiles() as $file) { |
||
| 126 | $localName = str_replace('\\', '/', tempnam(Prado::getPathOfNamespace($this->getTempPath()), '')); |
||
| 127 | $file->saveAs($localName); |
||
| 128 | $file->setLocalName($localName); |
||
| 129 | $params->files[] = $file->toArray(); |
||
| 130 | } |
||
| 131 | |||
| 132 | // return some javascript to display a completion status. |
||
| 133 | echo "<script> |
||
| 134 | Options = new Object(); |
||
| 135 | Options.clientID = '{$this->getClientID()}'; |
||
| 136 | Options.targetID = '{$this->_target->getUniqueID()}'; |
||
| 137 | Options.errorCode = '" . (int) !$this->getHasAllFiles() . "'; |
||
| 138 | Options.callbackToken = '{$this->pushParamsAndGetToken($params)}'; |
||
| 139 | Options.fileName = '" . TJavaScript::jsonEncode($this->getMultiple() ? array_column($params->files, 'fileName') : $this->getFileName(), JSON_HEX_APOS) . "'; |
||
| 140 | Options.fileSize = '" . TJavaScript::jsonEncode($this->getMultiple() ? array_column($params->files, 'fileSize') : $this->getFileSize(), JSON_HEX_APOS) . "'; |
||
| 141 | Options.fileType = '" . TJavaScript::jsonEncode($this->getMultiple() ? array_column($params->files, 'fileType') : $this->getFileType(), JSON_HEX_APOS) . "'; |
||
| 142 | Options.errorCode = '" . TJavaScript::jsonEncode($this->getMultiple() ? array_column($params->files, 'errorCode') : $this->getErrorCode(), JSON_HEX_APOS) . "'; |
||
| 143 | parent.Prado.WebUI.TActiveFileUpload.onFileUpload(Options); |
||
| 144 | </script>"; |
||
| 145 | |||
| 146 | exit(); |
||
| 147 | } |
||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @return string the path where the uploaded file will be stored temporarily, in namespace format |
||
| 152 | * default "Application.runtime.*" |
||
| 153 | */ |
||
| 154 | public function getTempPath() |
||
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * @param string $value the path where the uploaded file will be stored temporarily in namespace format |
||
| 161 | * default "Application.runtime.*" |
||
| 162 | */ |
||
| 163 | public function setTempPath($value) |
||
| 164 | { |
||
| 165 | $this->setViewState('TempPath', $value, 'Application.runtime.*'); |
||
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @return bool a value indicating whether an automatic callback to the server will occur whenever the user modifies the text in the TTextBox control and then tabs out of the component. Defaults to true. |
||
| 170 | * Note: When set to false, you will need to trigger the callback yourself. |
||
| 171 | */ |
||
| 172 | public function getAutoPostBack() |
||
| 173 | { |
||
| 174 | return $this->getViewState('AutoPostBack', true); |
||
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @param bool $value a value indicating whether an automatic callback to the server will occur whenever the user modifies the text in the TTextBox control and then tabs out of the component. Defaults to true. |
||
| 179 | * Note: When set to false, you will need to trigger the callback yourself. |
||
| 180 | */ |
||
| 181 | public function setAutoPostBack($value) |
||
| 182 | { |
||
| 183 | $this->setViewState('AutoPostBack', TPropertyValue::ensureBoolean($value), true); |
||
| 184 | } |
||
| 185 | |||
| 186 | /** |
||
| 187 | * @return string A chuck of javascript that will need to be called if {{@link getAutoPostBack AutoPostBack} is set to false} |
||
| 188 | */ |
||
| 189 | public function getCallbackJavascript() |
||
| 190 | { |
||
| 191 | return "Prado.WebUI.TActiveFileUpload.fileChanged(\"{$this->getClientID()}\")"; |
||
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @param mixed $sender |
||
| 196 | * @throws TInvalidDataValueException if the {@link getTempPath TempPath} is not writable. |
||
| 197 | */ |
||
| 198 | public function onInit($sender) |
||
| 199 | { |
||
| 200 | parent::onInit($sender); |
||
| 201 | |||
| 202 | if (!Prado::getApplication()->getCache()) { |
||
| 203 | if (!Prado::getApplication()->getSecurityManager()) { |
||
| 204 | throw new Exception('TActiveFileUpload needs either an application level cache or a security manager to work securely'); |
||
| 205 | } |
||
| 206 | } |
||
| 207 | |||
| 208 | if (!is_writable(Prado::getPathOfNamespace($this->getTempPath()))) { |
||
| 209 | throw new TInvalidDataValueException("activefileupload_temppath_invalid", $this->getTempPath()); |
||
| 210 | } |
||
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Raises <b>OnFileUpload</b> event. |
||
| 215 | * |
||
| 216 | * This method is required by {@link ICallbackEventHandler} interface. |
||
| 217 | * This method is mainly used by framework and control developers. |
||
| 218 | * @param TCallbackEventParameter $param the event parameter |
||
| 219 | */ |
||
| 220 | public function raiseCallbackEvent($param) |
||
| 221 | { |
||
| 222 | $cp = $param->getCallbackParameter(); |
||
| 223 | if ($key = $cp->targetID == $this->_target->getUniqueID()) { |
||
| 224 | $params = $this->popParamsByToken($cp->callbackToken); |
||
| 225 | foreach ($params->files as $index => $file) { |
||
| 226 | $_FILES[$key]['name'][$index] = stripslashes($file['fileName']); |
||
| 227 | $_FILES[$key]['size'][$index] = (int) ($file['fileSize']); |
||
| 228 | $_FILES[$key]['type'][$index] = $file['fileType']; |
||
| 229 | $_FILES[$key]['error'][$index] = (int) ($file['errorCode']); |
||
| 230 | $_FILES[$key]['tmp_name'][$index] = $file['localName']; |
||
| 231 | } |
||
| 232 | $this->loadPostData($key, null); |
||
| 233 | |||
| 234 | $this->raiseEvent('OnFileUpload', $this, $param); |
||
| 235 | } |
||
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Raises postdata changed event. |
||
| 240 | * This method calls {@link onFileUpload} method |
||
| 241 | * This method is primarily used by framework developers. |
||
| 242 | */ |
||
| 243 | public function raisePostDataChangedEvent() |
||
| 244 | { |
||
| 245 | $this->onFileUpload($this->getPage()->getRequest()->itemAt('TActiveFileUpload_TargetId')); |
||
| 246 | } |
||
| 247 | |||
| 248 | protected function pushParamsAndGetToken(TActiveFileUploadCallbackParams $params) |
||
| 249 | { |
||
| 250 | if ($cache = Prado::getApplication()->getCache()) { |
||
| 251 | // this is the most secure method, file info can't be forged from client side, no matter what |
||
| 252 | $token = md5('TActiveFileUpload::Params::' . $this->ClientID . '::' . rand(1000 * 1000, 9999 * 1000)); |
||
| 253 | $cache->set($token, serialize($params), 5 * 60); // expire in 5 minutes - the callback should arrive back in seconds, actually |
||
| 254 | } elseif ($mgr = Prado::getApplication()->getSecurityManager()) { |
||
| 255 | // this is a less secure method, file info can be still forged from client side, but only if attacker knows the secret application key |
||
| 256 | $token = urlencode(base64_encode($mgr->encrypt(serialize($params)))); |
||
| 257 | } else { |
||
| 258 | throw new Exception('TActiveFileUpload needs either an application level cache or a security manager to work securely'); |
||
| 259 | } |
||
| 260 | |||
| 261 | return $token; |
||
| 262 | } |
||
| 263 | |||
| 264 | protected function popParamsByToken($token) |
||
| 265 | { |
||
| 266 | if ($cache = Prado::getApplication()->getCache()) { |
||
| 267 | $v = $cache->get($token); |
||
| 268 | assert($v != ''); |
||
| 269 | $cache->delete($token); // remove it from cache so it can't be used again and won't take up space either |
||
| 270 | $params = unserialize($v); |
||
| 271 | } elseif ($mgr = Prado::getApplication()->getSecurityManager()) { |
||
| 272 | $v = $mgr->decrypt(base64_decode(urldecode($token))); |
||
| 273 | $params = unserialize($v); |
||
| 274 | } else { |
||
| 275 | throw new Exception('TActiveFileUpload needs either an application level cache or a security manager to work securely'); |
||
| 276 | } |
||
| 277 | |||
| 278 | assert($params instanceof TActiveFileUploadCallbackParams); |
||
| 279 | |||
| 280 | return $params; |
||
| 281 | } |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Publish the javascript |
||
| 285 | * @param mixed $param |
||
| 286 | */ |
||
| 287 | public function onPreRender($param) |
||
| 288 | { |
||
| 289 | parent::onPreRender($param); |
||
| 290 | |||
| 291 | if (!$this->getPage()->getIsPostBack() && isset($_GET['TActiveFileUpload_InputId']) && isset($_GET['TActiveFileUpload_TargetId']) && $_GET['TActiveFileUpload_InputId'] == $this->getClientID()) { |
||
| 292 | $params = new TActiveFileUploadCallbackParams; |
||
| 293 | foreach ($this->getFiles() as $file) { |
||
| 294 | $localName = str_replace('\\', '/', tempnam(Prado::getPathOfNamespace($this->getTempPath()), '')); |
||
| 295 | $file->setLocalName($localName); |
||
| 296 | // tricky workaround to intercept "uploaded file too big" error: real uploads happens in onFileUpload instead |
||
| 297 | $file->setErrorCode(UPLOAD_ERR_FORM_SIZE); |
||
| 298 | $params->files[] = $file->toArray(); |
||
| 299 | } |
||
| 300 | |||
| 301 | echo "<script> |
||
| 302 | Options = new Object(); |
||
| 303 | Options.clientID = '{$_GET['TActiveFileUpload_InputId']}'; |
||
| 304 | Options.targetID = '{$_GET['TActiveFileUpload_TargetId']}'; |
||
| 305 | Options.errorCode = '" . (int) !$this->getHasAllFiles() . "'; |
||
| 306 | Options.callbackToken = '{$this->pushParamsAndGetToken($params)}'; |
||
| 307 | Options.fileName = '" . TJavaScript::jsonEncode($this->getMultiple() ? array_column($params->files, 'fileName') : $this->getFileName(), JSON_HEX_APOS) . "'; |
||
| 308 | Options.fileSize = '" . TJavaScript::jsonEncode($this->getMultiple() ? array_column($params->files, 'fileSize') : $this->getFileSize(), JSON_HEX_APOS) . "'; |
||
| 309 | Options.fileType = '" . TJavaScript::jsonEncode($this->getMultiple() ? array_column($params->files, 'fileType') : $this->getFileType(), JSON_HEX_APOS) . "'; |
||
| 310 | Options.errorCode = '" . TJavaScript::jsonEncode($this->getMultiple() ? array_column($params->files, 'errorCode') : $this->getErrorCode(), JSON_HEX_APOS) . "'; |
||
| 311 | parent.Prado.WebUI.TActiveFileUpload.onFileUpload(Options); |
||
| 312 | </script>"; |
||
| 313 | } |
||
| 314 | } |
||
| 315 | |||
| 316 | public function createChildControls() |
||
| 317 | { |
||
| 318 | $this->getPage()->getClientScript()->registerPradoScript('activefileupload'); |
||
| 319 | |||
| 320 | $this->_flag = new THiddenField; |
||
| 321 | $this->_flag->setID('Flag'); |
||
| 322 | $this->getControls()->add($this->_flag); |
||
| 323 | |||
| 324 | $this->_busy = new TImage; |
||
| 325 | $this->_busy->setID('Busy'); |
||
| 326 | $this->_busy->setImageUrl($this->getAssetUrl('ActiveFileUploadIndicator.gif')); |
||
| 327 | $this->_busy->setStyle("display:none"); |
||
| 328 | $this->getControls()->add($this->_busy); |
||
| 329 | |||
| 330 | $this->_success = new TImage; |
||
| 331 | $this->_success->setID('Success'); |
||
| 332 | $this->_success->setImageUrl($this->getAssetUrl('ActiveFileUploadComplete.png')); |
||
| 333 | $this->_success->setStyle("display:none"); |
||
| 334 | $this->getControls()->add($this->_success); |
||
| 335 | |||
| 336 | $this->_error = new TImage; |
||
| 337 | $this->_error->setID('Error'); |
||
| 338 | $this->_error->setImageUrl($this->getAssetUrl('ActiveFileUploadError.png')); |
||
| 339 | $this->_error->setStyle("display:none"); |
||
| 340 | $this->getControls()->add($this->_error); |
||
| 341 | |||
| 342 | $this->_target = new TInlineFrame; |
||
| 343 | $this->_target->setID('Target'); |
||
| 344 | $this->_target->setFrameUrl('about:blank'); |
||
| 345 | $this->_target->setStyle("width:0px; height:0px; border:none"); |
||
| 346 | $this->getControls()->add($this->_target); |
||
| 347 | } |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Removes localfile on ending of the callback. |
||
| 351 | * @param mixed $param |
||
| 352 | */ |
||
| 353 | public function onUnload($param) |
||
| 354 | { |
||
| 355 | if ($this->getPage()->getIsCallback()) { |
||
| 356 | foreach ($this->getFiles() as $file) { |
||
| 357 | if ($file->getHasFile() && file_exists($file->getLocalName())) { |
||
| 358 | unlink($file->getLocalName()); |
||
| 359 | } |
||
| 360 | } |
||
| 361 | } |
||
| 362 | parent::onUnload($param); |
||
| 363 | } |
||
| 364 | |||
| 365 | /** |
||
| 366 | * @return TBaseActiveCallbackControl standard callback control options. |
||
| 367 | */ |
||
| 368 | public function getActiveControl() |
||
| 371 | } |
||
| 372 | |||
| 373 | /** |
||
| 374 | * @return TCallbackClientSide client side request options. |
||
| 375 | */ |
||
| 376 | public function getClientSide() |
||
| 377 | { |
||
| 378 | return $this->getAdapter()->getBaseActiveControl()->getClientSide(); |
||
| 379 | } |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Adds ID attribute, and renders the javascript for active component. |
||
| 383 | * @param THtmlWriter $writer the writer used for the rendering purpose |
||
| 384 | */ |
||
| 385 | public function addAttributesToRender($writer) |
||
| 391 | } |
||
| 392 | |||
| 393 | /** |
||
| 394 | * @return string corresponding javascript class name for this control. |
||
| 395 | */ |
||
| 396 | protected function getClientClassName() |
||
| 397 | { |
||
| 398 | return 'Prado.WebUI.TActiveFileUpload'; |
||
| 399 | } |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Gets the client side options for this control. |
||
| 403 | * @return array ( inputID => input client ID, |
||
| 404 | * flagID => flag client ID, |
||
| 405 | * targetName => target unique ID, |
||
| 406 | * formID => form client ID, |
||
| 407 | * indicatorID => upload indicator client ID, |
||
| 408 | * completeID => complete client ID, |
||
| 409 | * errorID => error client ID) |
||
| 410 | */ |
||
| 411 | protected function getClientOptions() |
||
| 425 | } |
||
| 426 | |||
| 427 | /** |
||
| 428 | * @return TImage the image displayed when an upload |
||
| 429 | * completes successfully. |
||
| 430 | */ |
||
| 431 | public function getSuccessImage() |
||
| 432 | { |
||
| 433 | $this->ensureChildControls(); |
||
| 435 | } |
||
| 436 | |||
| 437 | /** |
||
| 438 | * @return TImage the image displayed when an upload |
||
| 439 | * does not complete successfully. |
||
| 440 | */ |
||
| 441 | public function getErrorImage() |
||
| 445 | } |
||
| 446 | |||
| 447 | /** |
||
| 448 | * @return TImage the image displayed when an upload |
||
| 449 | * is in progress. |
||
| 450 | */ |
||
| 451 | public function getBusyImage() |
||
| 455 | } |
||
| 456 | } |
||
| 457 |
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