| 1 |
<?php |
| 2 |
namespace Aws\Multipart; |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
class UploadState |
| 11 |
{ |
| 12 |
const CREATED = 0; |
| 13 |
const INITIATED = 1; |
| 14 |
const COMPLETED = 2; |
| 15 |
const PROGRESS_THRESHOLD_SIZE = 8; |
| 16 |
|
| 17 |
private $progressBar = [ |
| 18 |
"Transfer initiated...\n| | 0.0%\n", |
| 19 |
"|== | 12.5%\n", |
| 20 |
"|===== | 25.0%\n", |
| 21 |
"|======= | 37.5%\n", |
| 22 |
"|========== | 50.0%\n", |
| 23 |
"|============ | 62.5%\n", |
| 24 |
"|=============== | 75.0%\n", |
| 25 |
"|================= | 87.5%\n", |
| 26 |
"|====================| 100.0%\nTransfer complete!\n" |
| 27 |
]; |
| 28 |
|
| 29 |
|
| 30 |
private $id; |
| 31 |
|
| 32 |
|
| 33 |
private $partSize; |
| 34 |
|
| 35 |
|
| 36 |
private $uploadedParts = []; |
| 37 |
|
| 38 |
|
| 39 |
private $status = self::CREATED; |
| 40 |
|
| 41 |
|
| 42 |
private $progressThresholds = []; |
| 43 |
|
| 44 |
|
| 45 |
private $displayProgress = false; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public function __construct(array $id, array $config = []) |
| 51 |
{ |
| 52 |
$this->id = $id; |
| 53 |
|
| 54 |
if (isset($config['display_progress']) |
| 55 |
&& is_bool($config['display_progress']) |
| 56 |
) { |
| 57 |
$this->displayProgress = $config['display_progress']; |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
public function getId() |
| 68 |
{ |
| 69 |
return $this->id; |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
public function setUploadId($key, $value) |
| 80 |
{ |
| 81 |
$this->id[$key] = $value; |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
public function getPartSize() |
| 90 |
{ |
| 91 |
return $this->partSize; |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
public function setPartSize($partSize) |
| 100 |
{ |
| 101 |
$this->partSize = $partSize; |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
public function setProgressThresholds($totalSize): array |
| 113 |
{ |
| 114 |
if(!is_numeric($totalSize)) { |
| 115 |
throw new \InvalidArgumentException( |
| 116 |
'The total size of the upload must be a number.' |
| 117 |
); |
| 118 |
} |
| 119 |
|
| 120 |
$this->progressThresholds[0] = 0; |
| 121 |
for ($i = 1; $i <= self::PROGRESS_THRESHOLD_SIZE; $i++) { |
| 122 |
$this->progressThresholds[] = round( |
| 123 |
$totalSize * ($i / self::PROGRESS_THRESHOLD_SIZE) |
| 124 |
); |
| 125 |
} |
| 126 |
|
| 127 |
return $this->progressThresholds; |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
public function getDisplayProgress($totalUploaded): void |
| 136 |
{ |
| 137 |
if (!is_numeric($totalUploaded)) { |
| 138 |
throw new \InvalidArgumentException( |
| 139 |
'The size of the bytes being uploaded must be a number.' |
| 140 |
); |
| 141 |
} |
| 142 |
|
| 143 |
if ($this->displayProgress) { |
| 144 |
while (!empty($this->progressBar) |
| 145 |
&& $totalUploaded >= $this->progressThresholds[0] |
| 146 |
) { |
| 147 |
echo array_shift($this->progressBar); |
| 148 |
array_shift($this->progressThresholds); |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
public function markPartAsUploaded($partNumber, array $partData = []) |
| 161 |
{ |
| 162 |
$this->uploadedParts[$partNumber] = $partData; |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
public function hasPartBeenUploaded($partNumber) |
| 173 |
{ |
| 174 |
return isset($this->uploadedParts[$partNumber]); |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
public function getUploadedParts() |
| 183 |
{ |
| 184 |
ksort($this->uploadedParts); |
| 185 |
return $this->uploadedParts; |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
public function setStatus($status) |
| 195 |
{ |
| 196 |
$this->status = $status; |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
public function isInitiated() |
| 205 |
{ |
| 206 |
return $this->status === self::INITIATED; |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
public function isCompleted() |
| 215 |
{ |
| 216 |
return $this->status === self::COMPLETED; |
| 217 |
} |
| 218 |
} |