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