ArchiverDto.php
267 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Backup\Dto\Service; |
| 4 | |
| 5 | use WPStaging\Backup\Entity\BackupMetadata; |
| 6 | |
| 7 | class ArchiverDto |
| 8 | { |
| 9 | /** @var string */ |
| 10 | private $filePath; |
| 11 | |
| 12 | /** @var string */ |
| 13 | private $indexPath; |
| 14 | |
| 15 | /** @var int */ |
| 16 | private $fileHeaderSizeInBytes = 0; |
| 17 | |
| 18 | /** @var int */ |
| 19 | private $writtenBytesTotal = 0; |
| 20 | |
| 21 | /** @var int */ |
| 22 | private $startOffset = 0; |
| 23 | |
| 24 | /** @var int */ |
| 25 | private $fileSize; |
| 26 | |
| 27 | /** @var array */ |
| 28 | private $indexPositionCreated = []; |
| 29 | |
| 30 | /** @var BackupMetadata */ |
| 31 | private $backupMetadata; |
| 32 | |
| 33 | /** |
| 34 | * True when the current file is being continued from a previous multipart part. |
| 35 | * The next segment's FileHeader emits REQUIRE_PREVIOUS_PART when this is set. |
| 36 | * @var bool |
| 37 | */ |
| 38 | private $isContinuation = false; |
| 39 | |
| 40 | /** |
| 41 | * Total bytes of the source file written across every segment of a multipart-split file. |
| 42 | * When no multipart split is in progress this stays at 0. |
| 43 | * @var int |
| 44 | */ |
| 45 | private $sourceBytesWrittenTotal = 0; |
| 46 | |
| 47 | /** |
| 48 | * @param int $bytes |
| 49 | * @return void |
| 50 | */ |
| 51 | public function appendWrittenBytes(int $bytes) |
| 52 | { |
| 53 | $this->writtenBytesTotal += (int) $bytes; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * @return bool |
| 58 | */ |
| 59 | public function isFinished(): bool |
| 60 | { |
| 61 | return $this->fileSize <= $this->writtenBytesTotal; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * @return void |
| 66 | */ |
| 67 | public function resetIfFinished() |
| 68 | { |
| 69 | if ($this->isFinished()) { |
| 70 | $this->reset(); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * @return void |
| 76 | */ |
| 77 | public function reset() |
| 78 | { |
| 79 | $this->setFileSize(-1); |
| 80 | $this->setFilePath(''); |
| 81 | $this->setWrittenBytesTotal(0); |
| 82 | $this->setIndexPositionCreated(false); |
| 83 | $this->setFileHeaderSizeInBytes(0); |
| 84 | $this->setStartOffset(0); |
| 85 | $this->setIsContinuation(false); |
| 86 | $this->setSourceBytesWrittenTotal(0); |
| 87 | } |
| 88 | |
| 89 | public function isContinuation(): bool |
| 90 | { |
| 91 | return $this->isContinuation; |
| 92 | } |
| 93 | |
| 94 | public function setIsContinuation(bool $isContinuation) |
| 95 | { |
| 96 | $this->isContinuation = $isContinuation; |
| 97 | } |
| 98 | |
| 99 | public function getSourceBytesWrittenTotal(): int |
| 100 | { |
| 101 | return $this->sourceBytesWrittenTotal; |
| 102 | } |
| 103 | |
| 104 | public function setSourceBytesWrittenTotal(int $sourceBytesWrittenTotal) |
| 105 | { |
| 106 | $this->sourceBytesWrittenTotal = $sourceBytesWrittenTotal; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * @return string |
| 111 | */ |
| 112 | public function getFilePath(): string |
| 113 | { |
| 114 | return (string)$this->filePath; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * @param string $filePath |
| 119 | * @return void |
| 120 | */ |
| 121 | public function setFilePath(string $filePath) |
| 122 | { |
| 123 | $this->filePath = wp_normalize_path((string)$filePath); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * @return string |
| 128 | */ |
| 129 | public function getIndexPath(): string |
| 130 | { |
| 131 | return $this->indexPath; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * @param string $indexPath |
| 136 | * @return void |
| 137 | */ |
| 138 | public function setIndexPath(string $indexPath) |
| 139 | { |
| 140 | $this->indexPath = wp_normalize_path((string)$indexPath); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * @return int |
| 145 | */ |
| 146 | public function getWrittenBytesTotal(): int |
| 147 | { |
| 148 | /** @noinspection UnnecessaryCastingInspection */ |
| 149 | return (int) $this->writtenBytesTotal; |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * @param int $writtenBytesTotal |
| 154 | * @return void |
| 155 | */ |
| 156 | public function setWrittenBytesTotal(int $writtenBytesTotal) |
| 157 | { |
| 158 | $this->writtenBytesTotal = $writtenBytesTotal; |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * @return int |
| 163 | */ |
| 164 | public function getFileSize(): int |
| 165 | { |
| 166 | return (int)$this->fileSize; |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * @param int $fileSize |
| 171 | * @return void |
| 172 | */ |
| 173 | public function setFileSize(int $fileSize) |
| 174 | { |
| 175 | $this->fileSize = $fileSize; |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * @return int |
| 180 | */ |
| 181 | public function getFileHeaderSizeInBytes(): int |
| 182 | { |
| 183 | return $this->fileHeaderSizeInBytes; |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * @param int $fileHeaderSizeInBytes |
| 188 | * @return void |
| 189 | */ |
| 190 | public function setFileHeaderSizeInBytes(int $fileHeaderSizeInBytes) |
| 191 | { |
| 192 | $this->fileHeaderSizeInBytes = $fileHeaderSizeInBytes; |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * @param string $category |
| 197 | * @param int $categoryIndex |
| 198 | * @return bool |
| 199 | */ |
| 200 | public function isIndexPositionCreated(string $category = '', int $categoryIndex = 0): bool |
| 201 | { |
| 202 | if (!isset($this->indexPositionCreated[$category])) { |
| 203 | return false; |
| 204 | } |
| 205 | |
| 206 | return (bool)$this->indexPositionCreated[$category][$categoryIndex]; |
| 207 | } |
| 208 | |
| 209 | public function isFileHeaderWritten(): bool |
| 210 | { |
| 211 | return $this->fileHeaderSizeInBytes > 0; |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * @param bool $indexPositionCreated |
| 216 | * @param string $category |
| 217 | * @param int $categoryIndex |
| 218 | * @return void |
| 219 | */ |
| 220 | public function setIndexPositionCreated(bool $indexPositionCreated, string $category = '', int $categoryIndex = 0) |
| 221 | { |
| 222 | if (!isset($this->indexPositionCreated[$category])) { |
| 223 | $this->indexPositionCreated[$category] = []; |
| 224 | } |
| 225 | |
| 226 | $this->indexPositionCreated[$category][$categoryIndex] = (bool)$indexPositionCreated; |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * @param int $startOffset |
| 231 | * @return void |
| 232 | */ |
| 233 | public function setStartOffset(int $startOffset) |
| 234 | { |
| 235 | $this->startOffset = $startOffset; |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * @return int |
| 240 | */ |
| 241 | public function getStartOffset(): int |
| 242 | { |
| 243 | return $this->startOffset; |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * @return BackupMetadata |
| 248 | */ |
| 249 | public function getBackupMetadata(): BackupMetadata |
| 250 | { |
| 251 | if (!$this->backupMetadata) { |
| 252 | $this->backupMetadata = new BackupMetadata(); |
| 253 | } |
| 254 | |
| 255 | return $this->backupMetadata; |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * @param BackupMetadata $backupMetadata |
| 260 | * @return void |
| 261 | */ |
| 262 | public function setBackupMetadata(BackupMetadata $backupMetadata) |
| 263 | { |
| 264 | $this->backupMetadata = $backupMetadata; |
| 265 | } |
| 266 | } |
| 267 |