BackupMetadata.php
2 years ago
FileBeingExtracted.php
2 years ago
ListableBackup.php
2 years ago
MultipartMetadata.php
2 years ago
FileBeingExtracted.php
139 lines
| 1 | <?php |
| 2 | |
| 3 | // TODO PHP7.x; declare(strict_type=1); |
| 4 | // TODO PHP7.x; type hints & return types |
| 5 | |
| 6 | namespace WPStaging\Backup\Entity; |
| 7 | |
| 8 | use WPStaging\Backup\BackupFileIndex; |
| 9 | use WPStaging\Framework\Filesystem\PathIdentifier; |
| 10 | use WPStaging\Framework\Traits\ResourceTrait; |
| 11 | |
| 12 | /** |
| 13 | * Class ExtractingFileEntity |
| 14 | * |
| 15 | * This is a OOP representation of a file being extracted. |
| 16 | * |
| 17 | * @see \WPStaging\Backup\Service\Extractor |
| 18 | * |
| 19 | * @package WPStaging\Backup\Entity\Service |
| 20 | */ |
| 21 | class FileBeingExtracted |
| 22 | { |
| 23 | use ResourceTrait; |
| 24 | |
| 25 | /** @var string */ |
| 26 | private $identifiablePath; |
| 27 | |
| 28 | /** @var string */ |
| 29 | private $relativePath; |
| 30 | |
| 31 | /** @var int */ |
| 32 | private $start; |
| 33 | |
| 34 | /** @var int */ |
| 35 | private $totalBytes; |
| 36 | |
| 37 | /** @var int */ |
| 38 | private $writtenBytes = 0; |
| 39 | |
| 40 | protected $extractFolder; |
| 41 | |
| 42 | /** @var PathIdentifier */ |
| 43 | protected $pathIdentifier; |
| 44 | |
| 45 | /** @var int 1 if yes, 0 if no. */ |
| 46 | protected $isCompressed; |
| 47 | |
| 48 | public function __construct($identifiablePath, $extractFolder, PathIdentifier $pathIdentifier, BackupFileIndex $backupFileIndex) |
| 49 | { |
| 50 | $this->identifiablePath = $identifiablePath; |
| 51 | $this->extractFolder = trailingslashit($extractFolder); |
| 52 | $this->start = $backupFileIndex->bytesStart; |
| 53 | $this->totalBytes = $backupFileIndex->bytesEnd; |
| 54 | $this->pathIdentifier = $pathIdentifier; |
| 55 | $this->isCompressed = $backupFileIndex->isCompressed; |
| 56 | $this->relativePath = $this->pathIdentifier->getPathWithoutIdentifier($this->identifiablePath); |
| 57 | } |
| 58 | |
| 59 | public function getBackupPath() |
| 60 | { |
| 61 | return $this->extractFolder . $this->pathIdentifier->getPathWithoutIdentifier($this->identifiablePath); |
| 62 | } |
| 63 | |
| 64 | public function findReadTo() |
| 65 | { |
| 66 | $maxLengthToWrite = 512 * KB_IN_BYTES; |
| 67 | |
| 68 | $remainingBytesToWrite = $this->totalBytes - $this->writtenBytes; |
| 69 | |
| 70 | return max(0, min($remainingBytesToWrite, $maxLengthToWrite)); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * @return string |
| 75 | */ |
| 76 | public function getPath() |
| 77 | { |
| 78 | return $this->identifiablePath; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * @return string |
| 83 | */ |
| 84 | public function getRelativePath() |
| 85 | { |
| 86 | return $this->relativePath; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * @return int |
| 91 | */ |
| 92 | public function getStart() |
| 93 | { |
| 94 | return $this->start; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * @return int |
| 99 | */ |
| 100 | public function getTotalBytes() |
| 101 | { |
| 102 | return $this->totalBytes; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * @return int |
| 107 | */ |
| 108 | public function getWrittenBytes() |
| 109 | { |
| 110 | return $this->writtenBytes; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * @param int $writtenBytes |
| 115 | */ |
| 116 | public function setWrittenBytes($writtenBytes) |
| 117 | { |
| 118 | $this->writtenBytes = $writtenBytes; |
| 119 | } |
| 120 | |
| 121 | public function addWrittenBytes($writtenBytes) |
| 122 | { |
| 123 | $this->writtenBytes += $writtenBytes; |
| 124 | } |
| 125 | |
| 126 | public function isFinished() |
| 127 | { |
| 128 | return $this->writtenBytes >= $this->totalBytes; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * @return bool|int |
| 133 | */ |
| 134 | public function getIsCompressed() |
| 135 | { |
| 136 | return $this->isCompressed; |
| 137 | } |
| 138 | } |
| 139 |