ArchiverDto.php
204 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 $fileHeaderBytes = 0; |
| 17 | |
| 18 | /** @var int */ |
| 19 | private $writtenBytesTotal = 0; |
| 20 | |
| 21 | /** @var int */ |
| 22 | private $fileSize; |
| 23 | |
| 24 | /** @var array */ |
| 25 | private $indexPositionCreated = []; |
| 26 | |
| 27 | /** @var BackupMetadata */ |
| 28 | private $backupMetadata; |
| 29 | |
| 30 | /** |
| 31 | * @param int $bytes |
| 32 | * @return void |
| 33 | */ |
| 34 | public function appendWrittenBytes(int $bytes) |
| 35 | { |
| 36 | $this->writtenBytesTotal += (int) $bytes; |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * @return bool |
| 41 | */ |
| 42 | public function isFinished(): bool |
| 43 | { |
| 44 | return $this->fileSize <= $this->writtenBytesTotal; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * @return void |
| 49 | */ |
| 50 | public function resetIfFinished() |
| 51 | { |
| 52 | if ($this->isFinished()) { |
| 53 | $this->reset(); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * @return void |
| 59 | */ |
| 60 | public function reset() |
| 61 | { |
| 62 | $this->setFileSize(-1); |
| 63 | $this->setFilePath(''); |
| 64 | $this->setWrittenBytesTotal(0); |
| 65 | $this->setIndexPositionCreated(false); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * @return string |
| 70 | */ |
| 71 | public function getFilePath(): string |
| 72 | { |
| 73 | return (string)$this->filePath; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * @param string $filePath |
| 78 | * @return void |
| 79 | */ |
| 80 | public function setFilePath(string $filePath) |
| 81 | { |
| 82 | $this->filePath = wp_normalize_path((string)$filePath); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * @return string |
| 87 | */ |
| 88 | public function getIndexPath(): string |
| 89 | { |
| 90 | return $this->indexPath; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * @param string $indexPath |
| 95 | * @return void |
| 96 | */ |
| 97 | public function setIndexPath(string $indexPath) |
| 98 | { |
| 99 | $this->indexPath = wp_normalize_path((string)$indexPath); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * @return int |
| 104 | */ |
| 105 | public function getWrittenBytesTotal(): int |
| 106 | { |
| 107 | /** @noinspection UnnecessaryCastingInspection */ |
| 108 | return (int) $this->writtenBytesTotal; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * @param int $writtenBytesTotal |
| 113 | * @return void |
| 114 | */ |
| 115 | public function setWrittenBytesTotal(int $writtenBytesTotal) |
| 116 | { |
| 117 | $this->writtenBytesTotal = $writtenBytesTotal; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * @return int |
| 122 | */ |
| 123 | public function getFileSize(): int |
| 124 | { |
| 125 | return (int)$this->fileSize; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * @param int $fileSize |
| 130 | * @return void |
| 131 | */ |
| 132 | public function setFileSize(int $fileSize) |
| 133 | { |
| 134 | $this->fileSize = $fileSize; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * @return int |
| 139 | */ |
| 140 | public function getFileHeaderBytes(): int |
| 141 | { |
| 142 | return $this->fileHeaderBytes; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * @param int $fileHeaderBytes |
| 147 | * @return void |
| 148 | */ |
| 149 | public function setFileHeaderBytes(int $fileHeaderBytes) |
| 150 | { |
| 151 | $this->fileHeaderBytes = $fileHeaderBytes; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * @param string $category |
| 156 | * @param int $categoryIndex |
| 157 | * @return bool |
| 158 | */ |
| 159 | public function isIndexPositionCreated(string $category = '', int $categoryIndex = 0): bool |
| 160 | { |
| 161 | if (!isset($this->indexPositionCreated[$category])) { |
| 162 | return false; |
| 163 | } |
| 164 | |
| 165 | return (bool)$this->indexPositionCreated[$category][$categoryIndex]; |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * @param bool $indexPositionCreated |
| 170 | * @param string $category |
| 171 | * @param int $categoryIndex |
| 172 | * @return void |
| 173 | */ |
| 174 | public function setIndexPositionCreated(bool $indexPositionCreated, string $category = '', int $categoryIndex = 0) |
| 175 | { |
| 176 | if (!isset($this->indexPositionCreated[$category])) { |
| 177 | $this->indexPositionCreated[$category] = []; |
| 178 | } |
| 179 | |
| 180 | $this->indexPositionCreated[$category][$categoryIndex] = (bool)$indexPositionCreated; |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * @return BackupMetadata |
| 185 | */ |
| 186 | public function getBackupMetadata(): BackupMetadata |
| 187 | { |
| 188 | if (!$this->backupMetadata) { |
| 189 | $this->backupMetadata = new BackupMetadata(); |
| 190 | } |
| 191 | |
| 192 | return $this->backupMetadata; |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * @param BackupMetadata $backupMetadata |
| 197 | * @return void |
| 198 | */ |
| 199 | public function setBackupMetadata(BackupMetadata $backupMetadata) |
| 200 | { |
| 201 | $this->backupMetadata = $backupMetadata; |
| 202 | } |
| 203 | } |
| 204 |