| Total Complexity | 68 |
| Total Lines | 451 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Zip 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 Zip, and based on these observations, apply Extract Interface, too.
| 1 | <?php namespace Comodojo\Zip; |
||
| 36 | class Zip implements ZipInterface, Countable { |
||
| 37 | |||
| 38 | use SkipTrait; |
||
| 39 | use MaskTrait; |
||
| 40 | use PasswordTrait; |
||
| 41 | use PathTrait; |
||
| 42 | use ArchiveTrait; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * zip file name |
||
| 46 | * |
||
| 47 | * @var string |
||
| 48 | */ |
||
| 49 | private $zip_file; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Class constructor |
||
| 53 | * |
||
| 54 | * @param string $zip_file ZIP file name |
||
| 55 | * |
||
| 56 | * @throws ZipException |
||
| 57 | */ |
||
| 58 | public function __construct(string $zip_file) { |
||
| 59 | |||
| 60 | if ( empty($zip_file) ) { |
||
| 61 | throw new ZipException(StatusCodes::get(ZipArchive::ER_NOENT)); |
||
| 62 | } |
||
| 63 | |||
| 64 | $this->zip_file = $zip_file; |
||
| 65 | |||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * {@inheritdoc} |
||
| 70 | */ |
||
| 71 | public static function open(string $zip_file): ZipInterface { |
||
| 72 | |||
| 73 | try { |
||
| 74 | |||
| 75 | $zip = new Zip($zip_file); |
||
| 76 | $zip->setArchive(self::openZipFile($zip_file)); |
||
| 77 | |||
| 78 | } catch (ZipException $ze) { |
||
| 79 | throw $ze; |
||
| 80 | } |
||
| 81 | |||
| 82 | return $zip; |
||
| 83 | |||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * {@inheritdoc} |
||
| 88 | */ |
||
| 89 | public static function check(string $zip_file): bool { |
||
| 90 | |||
| 91 | try { |
||
| 92 | |||
| 93 | $zip = self::openZipFile($zip_file, ZipArchive::CHECKCONS); |
||
| 94 | $zip->close(); |
||
| 95 | |||
| 96 | } catch (ZipException $ze) { |
||
| 97 | throw $ze; |
||
| 98 | } |
||
| 99 | |||
| 100 | return true; |
||
| 101 | |||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * {@inheritdoc} |
||
| 106 | */ |
||
| 107 | public static function create(string $zip_file, bool $overwrite = false): ZipInterface { |
||
| 108 | |||
| 109 | $overwrite = DataFilter::filterBoolean($overwrite); |
||
|
|
|||
| 110 | |||
| 111 | try { |
||
| 112 | |||
| 113 | $zip = new Zip($zip_file); |
||
| 114 | |||
| 115 | if ( $overwrite ) { |
||
| 116 | $zip->setArchive( |
||
| 117 | self::openZipFile( |
||
| 118 | $zip_file, |
||
| 119 | ZipArchive::CREATE | ZipArchive::OVERWRITE |
||
| 120 | ) |
||
| 121 | ); |
||
| 122 | } else { |
||
| 123 | $zip->setArchive( |
||
| 124 | self::openZipFile($zip_file, ZipArchive::CREATE) |
||
| 125 | ); |
||
| 126 | } |
||
| 127 | |||
| 128 | } catch (ZipException $ze) { |
||
| 129 | throw $ze; |
||
| 130 | } |
||
| 131 | |||
| 132 | return $zip; |
||
| 133 | |||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Count the number of files in the archive |
||
| 138 | * |
||
| 139 | * @return int |
||
| 140 | */ |
||
| 141 | public function count(): int { |
||
| 142 | return count($this->getArchive()); |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Get current zip file |
||
| 147 | * |
||
| 148 | * @return string |
||
| 149 | */ |
||
| 150 | public function getZipFile(): string { |
||
| 151 | |||
| 152 | return $this->zip_file; |
||
| 153 | |||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Get the list of files in the archive as an array |
||
| 158 | * |
||
| 159 | * @return array |
||
| 160 | * @throws ZipException |
||
| 161 | */ |
||
| 162 | public function listFiles(): array { |
||
| 163 | |||
| 164 | $list = []; |
||
| 165 | |||
| 166 | for ( $i = 0; $i < $this->getArchive()->numFiles; $i++ ) { |
||
| 167 | |||
| 168 | $name = $this->getArchive()->getNameIndex($i); |
||
| 169 | if ( $name === false ) { |
||
| 170 | throw new ZipException(StatusCodes::get($this->getArchive()->status)); |
||
| 171 | } |
||
| 172 | $list[] = $name; |
||
| 173 | |||
| 174 | } |
||
| 175 | |||
| 176 | return $list; |
||
| 177 | |||
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Extract files from zip archive |
||
| 182 | * |
||
| 183 | * @param string $destination Destination path |
||
| 184 | * @param mixed $files (optional) a filename or an array of filenames |
||
| 185 | * |
||
| 186 | * @return bool |
||
| 187 | * @throws ZipException |
||
| 188 | */ |
||
| 189 | public function extract(string $destination, $files = null): bool { |
||
| 190 | |||
| 191 | if ( empty($destination) ) { |
||
| 192 | throw new ZipException("Invalid destination path: $destination"); |
||
| 193 | } |
||
| 194 | |||
| 195 | if ( !file_exists($destination) ) { |
||
| 196 | |||
| 197 | $omask = umask(0); |
||
| 198 | $action = mkdir($destination, $this->getMask(), true); |
||
| 199 | umask($omask); |
||
| 200 | |||
| 201 | if ( $action === false ) { |
||
| 202 | throw new ZipException("Error creating folder: $destination"); |
||
| 203 | } |
||
| 204 | |||
| 205 | } |
||
| 206 | |||
| 207 | if ( !is_writable($destination) ) { |
||
| 208 | throw new ZipException("Destination path $destination not writable"); |
||
| 209 | } |
||
| 210 | |||
| 211 | if ( is_array($files) && @sizeof($files) != 0 ) { |
||
| 212 | $file_matrix = $files; |
||
| 213 | } else { |
||
| 214 | $file_matrix = $this->getArchiveFiles(); |
||
| 215 | } |
||
| 216 | |||
| 217 | if ( !empty($this->getPassword()) ) { |
||
| 218 | $this->getArchive()->setPassword($this->getPassword()); |
||
| 219 | } |
||
| 220 | |||
| 221 | $extract = $this->getArchive()->extractTo($destination, $file_matrix); |
||
| 222 | |||
| 223 | if ( $extract === false ) { |
||
| 224 | throw new ZipException(StatusCodes::get($this->getArchive()->status)); |
||
| 225 | } |
||
| 226 | |||
| 227 | return true; |
||
| 228 | |||
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Add files to zip archive |
||
| 233 | * |
||
| 234 | * @param mixed $file_name_or_array Filename to add or an array of filenames |
||
| 235 | * @param bool $flatten_root_folder In case of directory, specify if root folder should be flatten or not |
||
| 236 | * |
||
| 237 | * @return Zip |
||
| 238 | * @throws ZipException |
||
| 239 | */ |
||
| 240 | public function add($file_name_or_array, bool $flatten_root_folder = false): Zip { |
||
| 241 | |||
| 242 | if ( empty($file_name_or_array) ) { |
||
| 243 | throw new ZipException(StatusCodes::get(ZipArchive::ER_NOENT)); |
||
| 244 | } |
||
| 245 | |||
| 246 | $flatten_root_folder = DataFilter::filterBoolean($flatten_root_folder); |
||
| 247 | |||
| 248 | try { |
||
| 249 | |||
| 250 | if ( is_array($file_name_or_array) ) { |
||
| 251 | foreach ( $file_name_or_array as $file_name ) { |
||
| 252 | $this->addItem($file_name, $flatten_root_folder); |
||
| 253 | } |
||
| 254 | } else { |
||
| 255 | $this->addItem($file_name_or_array, $flatten_root_folder); |
||
| 256 | } |
||
| 257 | |||
| 258 | } catch (ZipException $ze) { |
||
| 259 | throw $ze; |
||
| 260 | } |
||
| 261 | |||
| 262 | return $this; |
||
| 263 | |||
| 264 | } |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Delete files from zip archive |
||
| 268 | * |
||
| 269 | * @param mixed $file_name_or_array Filename to delete or an array of filenames |
||
| 270 | * |
||
| 271 | * @return Zip |
||
| 272 | * @throws ZipException |
||
| 273 | */ |
||
| 274 | public function delete($file_name_or_array): Zip { |
||
| 275 | |||
| 276 | if ( empty($file_name_or_array) ) { |
||
| 277 | throw new ZipException(StatusCodes::get(ZipArchive::ER_NOENT)); |
||
| 278 | } |
||
| 279 | |||
| 280 | try { |
||
| 281 | |||
| 282 | if ( is_array($file_name_or_array) ) { |
||
| 283 | foreach ( $file_name_or_array as $file_name ) { |
||
| 284 | $this->deleteItem($file_name); |
||
| 285 | } |
||
| 286 | } else { |
||
| 287 | $this->deleteItem($file_name_or_array); |
||
| 288 | } |
||
| 289 | |||
| 290 | } catch (ZipException $ze) { |
||
| 291 | throw $ze; |
||
| 292 | } |
||
| 293 | |||
| 294 | return $this; |
||
| 295 | |||
| 296 | } |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Close the zip archive |
||
| 300 | * |
||
| 301 | * @return bool |
||
| 302 | * @throws ZipException |
||
| 303 | */ |
||
| 304 | public function close(): bool { |
||
| 305 | |||
| 306 | if ( $this->getArchive()->close() === false ) { |
||
| 307 | throw new ZipException(StatusCodes::get($this->getArchive()->status)); |
||
| 308 | } |
||
| 309 | |||
| 310 | return true; |
||
| 311 | |||
| 312 | } |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Get a list of file contained in zip archive before extraction |
||
| 316 | * |
||
| 317 | * @return array |
||
| 318 | */ |
||
| 319 | private function getArchiveFiles(): array { |
||
| 350 | |||
| 351 | } |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Add item to zip archive |
||
| 355 | * |
||
| 356 | * @param string $file File to add (realpath) |
||
| 357 | * @param bool $flatroot (optional) If true, source directory will be not included |
||
| 358 | * @param string $base (optional) Base to record in zip file |
||
| 359 | * @return void |
||
| 360 | * @throws ZipException |
||
| 361 | */ |
||
| 362 | private function addItem( |
||
| 363 | string $file, |
||
| 364 | bool $flatroot = false, |
||
| 365 | ?string $base = null |
||
| 366 | ): void { |
||
| 367 | |||
| 368 | $file = is_null($this->getPath()) ? $file : $this->getPath()."/$file"; |
||
| 369 | $real_file = str_replace('\\', '/', realpath($file)); |
||
| 370 | $real_name = basename($real_file); |
||
| 371 | |||
| 372 | if ( !is_null($base) ) { |
||
| 373 | |||
| 374 | if ( |
||
| 375 | ( |
||
| 376 | $real_name[0] == "." && |
||
| 377 | in_array($this->getSkipMode(), ["HIDDEN", "ALL"]) |
||
| 378 | ) || |
||
| 379 | ( |
||
| 380 | $real_name[0] == "." && |
||
| 381 | @$real_name[1] == "_" && |
||
| 382 | in_array($this->getSkipMode(), ["COMODOJO", "ALL"]) |
||
| 383 | ) |
||
| 384 | ) { |
||
| 385 | return; |
||
| 386 | } |
||
| 387 | |||
| 388 | } |
||
| 389 | |||
| 390 | if ( is_dir($real_file) ) { |
||
| 391 | $this->addDirectoryItem($real_file, $real_name, $base, $flatroot); |
||
| 392 | } else if ( is_file($real_file) ) { |
||
| 393 | $this->addFileItem($real_file, $real_name, $base); |
||
| 394 | } else { |
||
| 395 | return; |
||
| 396 | } |
||
| 397 | |||
| 398 | } |
||
| 399 | |||
| 400 | private function addFileItem( |
||
| 401 | string $real_file, |
||
| 402 | string $real_name, |
||
| 403 | ?string $base = null |
||
| 404 | ): void { |
||
| 405 | |||
| 406 | $file_target = is_null($base) ? $real_name : $base.$real_name; |
||
| 407 | $add_file = $this->getArchive()->addFile($real_file, $file_target); |
||
| 408 | if ( $add_file === false ) { |
||
| 409 | throw new ZipException(StatusCodes::get($this->getArchive()->status)); |
||
| 410 | } |
||
| 411 | |||
| 412 | } |
||
| 413 | |||
| 414 | private function addDirectoryItem( |
||
| 415 | string $real_file, |
||
| 416 | string $real_name, |
||
| 417 | ?string $base = null, |
||
| 418 | bool $flatroot |
||
| 419 | ): void { |
||
| 420 | |||
| 421 | if ( !$flatroot ) { |
||
| 422 | |||
| 423 | $folder_target = is_null($base) ? $real_name : $base.$real_name; |
||
| 424 | $new_folder = $this->getArchive()->addEmptyDir($folder_target); |
||
| 425 | if ( $new_folder === false ) { |
||
| 426 | throw new ZipException(StatusCodes::get($this->getArchive()->status)); |
||
| 427 | } |
||
| 428 | |||
| 429 | } else { |
||
| 430 | $folder_target = null; |
||
| 431 | } |
||
| 432 | |||
| 433 | foreach ( new DirectoryIterator($real_file) as $path ) { |
||
| 434 | |||
| 435 | if ( $path->isDot() ) { |
||
| 436 | continue; |
||
| 437 | } |
||
| 438 | |||
| 439 | $file_real = $path->getPathname(); |
||
| 440 | $base = is_null($folder_target) ? null : ($folder_target."/"); |
||
| 441 | |||
| 442 | try { |
||
| 443 | $this->addItem($file_real, false, $base); |
||
| 444 | } catch (ZipException $ze) { |
||
| 445 | throw $ze; |
||
| 446 | } |
||
| 447 | |||
| 448 | } |
||
| 449 | |||
| 450 | } |
||
| 451 | |||
| 452 | /** |
||
| 453 | * Delete item from zip archive |
||
| 454 | * |
||
| 455 | * @param string $file File to delete (zippath) |
||
| 456 | * @return void |
||
| 457 | * @throws ZipException |
||
| 458 | */ |
||
| 459 | private function deleteItem(string $file): void { |
||
| 464 | } |
||
| 465 | |||
| 466 | } |
||
| 467 | |||
| 468 | /** |
||
| 469 | * Open a zip file |
||
| 470 | * |
||
| 471 | * @param string $zip_file ZIP file name |
||
| 472 | * @param int $flags ZipArchive::open flags |
||
| 473 | * |
||
| 474 | * @return ZipArchive |
||
| 475 | * @throws ZipException |
||
| 476 | */ |
||
| 477 | private static function openZipFile(string $zip_file, int $flags = null): ZipArchive { |
||
| 487 | |||
| 488 | } |
||
| 489 | |||
| 490 | } |
||
| 491 |