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