Debian Codegraph @

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

raw symbols SCIP
1 <?php
2
3 namespace Aws\S3\S3Transfer\Progress;
4
5 use Aws\S3\S3Transfer\Progress\TransferProgressSnapshot;
6 use Aws\S3\S3Transfer\Progress\TransferListenerNotifier;
7 use Throwable;
8
9 /**
10 * Aggregates per-object progress snapshots into a directory-level snapshot.
11 * Acts as an object-level listener and emits directory-level events through an
12 * internal notifier to the provided directory listeners.
13 */
14 final class DirectoryTransferProgressAggregator extends AbstractTransferListener
15 {
16 /** @var string */
17 private string $identifier;
18
19 /** @var int */
20 private int $totalBytes;
21
22 /** @var int */
23 private int $totalFiles;
24
25 /** @var int */
26 private int $transferredBytes = 0;
27
28 /** @var int */
29 private int $transferredFiles = 0;
30
31 /** @var array<string, int> */
32 private array $objectBytes = [];
33
34 /** @var array<string, bool> */
35 private array $objectTerminal = [];
36
37 /** @var TransferListenerNotifier */
38 private TransferListenerNotifier $directoryNotifier;
39
40 public function __construct(
41 string $identifier,
42 int $totalBytes,
43 int $totalFiles,
44 array $directoryListeners = [],
45 ?AbstractTransferListener $directoryProgressTracker = null
46 ) {
47 if ($directoryProgressTracker !== null) {
48 $directoryListeners[] = $directoryProgressTracker;
49 }
50
51 $this->identifier = $identifier;
52 $this->totalBytes = $totalBytes;
53 $this->totalFiles = $totalFiles;
54 $this->directoryNotifier = new TransferListenerNotifier($directoryListeners);
55 }
56
57 /**
58 * Notify directory listeners that the directory transfer has been initiated.
59 *
60 * @param array $requestArgs
61 *
62 * @return void
63 */
64 public function notifyDirectoryInitiated(array $requestArgs): void
65 {
66 $this->directoryNotifier->transferInitiated([
67 self::REQUEST_ARGS_KEY => $requestArgs,
68 self::PROGRESS_SNAPSHOT_KEY => $this->getSnapshot(),
69 ]);
70 }
71
72 /**
73 * Notify directory listeners that the directory transfer completed.
74 *
75 * @param array|null $response
76 *
77 * @return void
78 */
79 public function notifyDirectoryComplete(?array $response = null): void
80 {
81 $snapshot = $this->getSnapshot();
82 if ($response !== null) {
83 $snapshot = $snapshot->withResponse($response);
84 }
85
86 $this->directoryNotifier->transferComplete([
87 self::REQUEST_ARGS_KEY => [],
88 self::PROGRESS_SNAPSHOT_KEY => $snapshot,
89 ]);
90 }
91
92 /**
93 * Notify directory listeners that the directory transfer failed.
94 *
95 * @param Throwable|string $reason
96 *
97 * @return void
98 */
99 public function notifyDirectoryFail(Throwable|string $reason): void
100 {
101 $snapshot = $this->getSnapshot();
102 $this->directoryNotifier->transferFail([
103 self::REQUEST_ARGS_KEY => [],
104 self::PROGRESS_SNAPSHOT_KEY => $snapshot,
105 self::REASON_KEY => $reason,
106 ]);
107 }
108
109 /**
110 * Update totals, useful when object list is streamed.
111 *
112 * @param int $bytes
113 * @param int $files
114 *
115 * @return void
116 */
117 public function incrementTotals(int $bytes, int $files = 1): void
118 {
119 $this->totalBytes += $bytes;
120 $this->totalFiles += $files;
121 }
122
123 /**
124 * @inheritDoc
125 */
126 public function bytesTransferred(array $context): bool
127 {
128 /** @var TransferProgressSnapshot $snapshot */
129 $snapshot = $context[self::PROGRESS_SNAPSHOT_KEY];
130 $this->updateObjectProgress($snapshot);
131 $this->directoryNotifier->bytesTransferred([
132 self::REQUEST_ARGS_KEY => $context[self::REQUEST_ARGS_KEY] ?? [],
133 self::PROGRESS_SNAPSHOT_KEY => $this->getSnapshot(),
134 ]);
135
136 return true;
137 }
138
139 /**
140 * @inheritDoc
141 */
142 public function transferComplete(array $context): void
143 {
144 /** @var TransferProgressSnapshot $snapshot */
145 $snapshot = $context[self::PROGRESS_SNAPSHOT_KEY];
146 $this->markObjectTerminal($snapshot);
147 $this->directoryNotifier->bytesTransferred([
148 self::REQUEST_ARGS_KEY => $context[self::REQUEST_ARGS_KEY] ?? [],
149 self::PROGRESS_SNAPSHOT_KEY => $this->getSnapshot(),
150 ]);
151 }
152
153 /**
154 * @inheritDoc
155 */
156 public function transferFail(array $context): void
157 {
158 /** @var TransferProgressSnapshot $snapshot */
159 $snapshot = $context[self::PROGRESS_SNAPSHOT_KEY];
160 $this->markObjectTerminal($snapshot);
161 $this->directoryNotifier->bytesTransferred([
162 self::REQUEST_ARGS_KEY => $context[self::REQUEST_ARGS_KEY] ?? [],
163 self::PROGRESS_SNAPSHOT_KEY => $this->getSnapshot(),
164 self::REASON_KEY => $context[self::REASON_KEY] ?? null,
165 ]);
166 }
167
168 /**
169 * @return DirectoryTransferProgressSnapshot
170 */
171 public function getSnapshot(): DirectoryTransferProgressSnapshot
172 {
173 return new DirectoryTransferProgressSnapshot(
174 $this->identifier,
175 $this->transferredBytes,
176 $this->totalBytes,
177 $this->transferredFiles,
178 $this->totalFiles,
179 );
180 }
181
182 /**
183 * @param TransferProgressSnapshot $snapshot
184 *
185 * @return void
186 */
187 private function updateObjectProgress(TransferProgressSnapshot $snapshot): void
188 {
189 $identifier = $snapshot->getIdentifier();
190 $previous = $this->objectBytes[$identifier] ?? 0;
191 $current = $snapshot->getTransferredBytes();
192 // Avoid double counting when updates decrease (should not happen, but guard)
193 $delta = $current - $previous;
194 if ($delta < 0) {
195 $delta = 0;
196 }
197
198 $this->objectBytes[$identifier] = $current;
199 $this->transferredBytes += $delta;
200 }
201
202 /**
203 * @param TransferProgressSnapshot $snapshot
204 *
205 * @return void
206 */
207 private function markObjectTerminal(TransferProgressSnapshot $snapshot): void
208 {
209 $this->updateObjectProgress($snapshot);
210 $identifier = $snapshot->getIdentifier();
211 if (!($this->objectTerminal[$identifier] ?? false)) {
212 $this->objectTerminal[$identifier] = true;
213 $this->transferredFiles++;
214 }
215 }
216 }