| @@ -12,8 +12,10 @@ | ||
| 12 | 12 | { |
| 13 | 13 | const CREATED = 0; |
| 14 | 14 | const INITIATED = 1; |
| 15 | 15 | const COMPLETED = 2; |
| 16 | + const PROGRESS_THRESHOLD_SIZE = 8; | |
| 17 | + private $progressBar = ["Transfer initiated...\n| | 0.0%\n", "|== | 12.5%\n", "|===== | 25.0%\n", "|======= | 37.5%\n", "|========== | 50.0%\n", "|============ | 62.5%\n", "|=============== | 75.0%\n", "|================= | 87.5%\n", "|====================| 100.0%\nTransfer complete!\n"]; | |
| 16 | 18 | /** @var array Params used to identity the upload. */ |
| 17 | 19 | private $id; |
| 18 | 20 | /** @var int Part size being used by the upload. */ |
| 19 | 21 | private $partSize; |
| @@ -20,14 +22,21 @@ | ||
| 20 | 22 | /** @var array Parts that have been uploaded. */ |
| 21 | 23 | private $uploadedParts = []; |
| 22 | 24 | /** @var int Identifies the status the upload. */ |
| 23 | 25 | private $status = self::CREATED; |
| 26 | + /** @var array Thresholds for progress of the upload. */ | |
| 27 | + private $progressThresholds = []; | |
| 28 | + /** @var boolean Determines status for tracking the upload */ | |
| 29 | + private $displayProgress = \false; | |
| 24 | 30 | /** |
| 25 | 31 | * @param array $id Params used to identity the upload. |
| 26 | 32 | */ |
| 27 | - public function __construct(array $id) | |
| 33 | + public function __construct(array $id, array $config = []) | |
| 28 | 34 | { |
| 29 | 35 | $this->id = $id; |
| 36 | + if (isset($config['display_progress']) && \is_bool($config['display_progress'])) { | |
| 37 | + $this->displayProgress = $config['display_progress']; | |
| 38 | + } | |
| 30 | 39 | } |
| 31 | 40 | /** |
| 32 | 41 | * Get the upload's ID, which is a tuple of parameters that can uniquely |
| 33 | 42 | * identify the upload. |
| @@ -38,9 +47,9 @@ | ||
| 38 | 47 | { |
| 39 | 48 | return $this->id; |
| 40 | 49 | } |
| 41 | 50 | /** |
| 42 | - * Set's the "upload_id", or 3rd part of the upload's ID. This typically | |
| 51 | + * Sets the "upload_id", or 3rd part of the upload's ID. This typically | |
| 43 | 52 | * only needs to be done after initiating an upload. |
| 44 | 53 | * |
| 45 | 54 | * @param string $key The param key of the upload_id. |
| 46 | 55 | * @param string $value The param value of the upload_id. |
| @@ -65,8 +74,44 @@ | ||
| 65 | 74 | */ |
| 66 | 75 | public function setPartSize($partSize) |
| 67 | 76 | { |
| 68 | 77 | $this->partSize = $partSize; |
| 78 | + } | |
| 79 | + /** | |
| 80 | + * Sets the 1/8th thresholds array. $totalSize is only sent if | |
| 81 | + * 'track_upload' is true. | |
| 82 | + * | |
| 83 | + * @param $totalSize numeric Size of object to upload. | |
| 84 | + * | |
| 85 | + * @return array | |
| 86 | + */ | |
| 87 | + public function setProgressThresholds($totalSize) : array | |
| 88 | + { | |
| 89 | + if (!\is_numeric($totalSize)) { | |
| 90 | + throw new \InvalidArgumentException('The total size of the upload must be a number.'); | |
| 91 | + } | |
| 92 | + $this->progressThresholds[0] = 0; | |
| 93 | + for ($i = 1; $i <= self::PROGRESS_THRESHOLD_SIZE; $i++) { | |
| 94 | + $this->progressThresholds[] = \round($totalSize * ($i / self::PROGRESS_THRESHOLD_SIZE)); | |
| 95 | + } | |
| 96 | + return $this->progressThresholds; | |
| 97 | + } | |
| 98 | + /** | |
| 99 | + * Prints progress of upload. | |
| 100 | + * | |
| 101 | + * @param $totalUploaded numeric Size of upload so far. | |
| 102 | + */ | |
| 103 | + public function getDisplayProgress($totalUploaded) : void | |
| 104 | + { | |
| 105 | + if (!\is_numeric($totalUploaded)) { | |
| 106 | + throw new \InvalidArgumentException('The size of the bytes being uploaded must be a number.'); | |
| 107 | + } | |
| 108 | + if ($this->displayProgress) { | |
| 109 | + while (!empty($this->progressBar) && $totalUploaded >= $this->progressThresholds[0]) { | |
| 110 | + echo \array_shift($this->progressBar); | |
| 111 | + \array_shift($this->progressThresholds); | |
| 112 | + } | |
| 113 | + } | |
| 69 | 114 | } |
| 70 | 115 | /** |
| 71 | 116 | * Marks a part as being uploaded. |
| 72 | 117 | * |