Debian Codegraph @

lib/3rdParty/composer/aws/aws-sdk-php/src/S3/S3Transfer/Progress/SingleProgressTracker.php

raw symbols SCIP
1 <?php
2
3 namespace Aws\S3\S3Transfer\Progress;
4
5 use Aws\S3\S3Transfer\Exception\ProgressTrackerException;
6
7 /**
8 * To track single object transfers.
9 */
10 final class SingleProgressTracker extends AbstractTransferListener
11 implements ProgressTrackerInterface
12 {
13 /** @var ProgressBarInterface */
14 private ProgressBarInterface $progressBar;
15
16 /** @var resource */
17 private mixed $output;
18
19 /** @var bool */
20 private bool $clear;
21
22 /** @var TransferProgressSnapshot|null */
23 private ?TransferProgressSnapshot $currentSnapshot;
24
25 /** @var bool */
26 private bool $showProgressOnUpdate;
27
28 /**
29 * @param ProgressBarInterface $progressBar
30 * @param mixed|false|resource $output
31 * @param bool $clear
32 * @param TransferProgressSnapshot|null $currentSnapshot
33 * @param bool $showProgressOnUpdate
34 */
35 public function __construct(
36 ProgressBarInterface $progressBar = new ConsoleProgressBar(),
37 mixed $output = STDOUT,
38 bool $clear = true,
39 ?TransferProgressSnapshot $currentSnapshot = null,
40 bool $showProgressOnUpdate = true
41 ) {
42 $this->progressBar = $progressBar;
43 if (get_resource_type($output) !== 'stream') {
44 throw new \InvalidArgumentException("The type for $output must be a stream");
45 }
46 $this->output = $output;
47 $this->clear = $clear;
48 $this->currentSnapshot = $currentSnapshot;
49 $this->showProgressOnUpdate = $showProgressOnUpdate;
50 }
51
52 /**
53 * @return ProgressBarInterface
54 */
55 public function getProgressBar(): ProgressBarInterface
56 {
57 return $this->progressBar;
58 }
59
60 /**
61 * @return mixed
62 */
63 public function getOutput(): mixed
64 {
65 return $this->output;
66 }
67
68 /**
69 * @return bool
70 */
71 public function isClear(): bool
72 {
73 return $this->clear;
74 }
75
76 /**
77 * @return TransferProgressSnapshot|null
78 */
79 public function getCurrentSnapshot(): ?TransferProgressSnapshot
80 {
81 return $this->currentSnapshot;
82 }
83
84 /**
85 * @return bool
86 */
87 public function isShowProgressOnUpdate(): bool
88 {
89 return $this->showProgressOnUpdate;
90 }
91
92 /**
93 * @inheritDoc
94 *
95 * @return void
96 */
97 public function transferInitiated(array $context): void
98 {
99 $this->currentSnapshot = $context[AbstractTransferListener::PROGRESS_SNAPSHOT_KEY];
100 $progressFormat = $this->progressBar->getProgressBarFormat();
101 // Probably a common argument
102 $progressFormat->setArg(
103 'object_name',
104 $this->currentSnapshot->getIdentifier()
105 );
106
107 $this->updateProgressBar();
108 }
109
110 /**
111 * @inheritDoc
112 */
113 public function bytesTransferred(array $context): bool
114 {
115 $this->currentSnapshot = $context[AbstractTransferListener::PROGRESS_SNAPSHOT_KEY];
116 $progressFormat = $this->progressBar->getProgressBarFormat();
117 if ($progressFormat instanceof ColoredTransferProgressBarFormat) {
118 $progressFormat->setArg(
119 'color_code',
120 ColoredTransferProgressBarFormat::BLUE_COLOR_CODE
121 );
122 }
123
124 $this->updateProgressBar();
125
126 return true;
127 }
128
129 /**
130 * @inheritDoc
131 *
132 * @return void
133 */
134 public function transferComplete(array $context): void
135 {
136 $this->currentSnapshot = $context[AbstractTransferListener::PROGRESS_SNAPSHOT_KEY];
137 $progressFormat = $this->progressBar->getProgressBarFormat();
138 if ($progressFormat instanceof ColoredTransferProgressBarFormat) {
139 $progressFormat->setArg(
140 'color_code',
141 ColoredTransferProgressBarFormat::GREEN_COLOR_CODE
142 );
143 }
144
145 $this->updateProgressBar(
146 $this->currentSnapshot->getTotalBytes() === 0
147 );
148 }
149
150 /**
151 * @inheritDoc
152 *
153 * @return void
154 */
155 public function transferFail(array $context): void
156 {
157 $this->currentSnapshot = $context[AbstractTransferListener::PROGRESS_SNAPSHOT_KEY];
158 $progressFormat = $this->progressBar->getProgressBarFormat();
159 if ($progressFormat instanceof ColoredTransferProgressBarFormat) {
160 $progressFormat->setArg(
161 'color_code',
162 ColoredTransferProgressBarFormat::RED_COLOR_CODE
163 );
164 $progressFormat->setArg(
165 'message',
166 $context[AbstractTransferListener::REASON_KEY]
167 );
168 }
169
170 $this->updateProgressBar();
171 }
172
173 /**
174 * Updates the progress bar with the transfer snapshot
175 * and also call showProgress.
176 *
177 * @param bool $forceCompletion To force the progress bar to be
178 * completed. This is useful for files where its size is zero,
179 * for which a ratio will return zero, and hence the percent
180 * will be zero.
181 *
182 * @return void
183 */
184 private function updateProgressBar(
185 bool $forceCompletion = false
186 ): void
187 {
188 if (!$forceCompletion) {
189 $this->progressBar->setPercentCompleted(
190 ((int)floor($this->currentSnapshot->ratioTransferred() * 100))
191 );
192 } else {
193 $this->progressBar->setPercentCompleted(100);
194 }
195
196 $this->progressBar->getProgressBarFormat()->setArgs([
197 'transferred' => min(
198 $this->currentSnapshot->getTransferredBytes(),
199 $this->currentSnapshot->getTotalBytes()
200 ),
201 'to_be_transferred' => $this->currentSnapshot->getTotalBytes(),
202 'unit' => 'B',
203 ]);
204 // Display progress
205 if ($this->showProgressOnUpdate) {
206 $this->showProgress();
207 }
208 }
209
210 /**
211 * @inheritDoc
212 *
213 * @return void
214 */
215 public function showProgress(): void
216 {
217 if ($this->currentSnapshot === null) {
218 throw new ProgressTrackerException(
219 "There is not snapshot to show progress for."
220 );
221 }
222
223 if ($this->clear) {
224 fwrite($this->output, "\033[2J\033[H");
225 }
226
227 fwrite($this->output, sprintf(
228 "\r\n%s",
229 $this->progressBar->render()
230 ));
231 fflush($this->output);
232 }
233 }