| 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 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
final class DirectoryTransferProgressAggregator extends AbstractTransferListener |
| 15 |
{ |
| 16 |
|
| 17 |
private string $identifier; |
| 18 |
|
| 19 |
|
| 20 |
private int $totalBytes; |
| 21 |
|
| 22 |
|
| 23 |
private int $totalFiles; |
| 24 |
|
| 25 |
|
| 26 |
private int $transferredBytes = 0; |
| 27 |
|
| 28 |
|
| 29 |
private int $transferredFiles = 0; |
| 30 |
|
| 31 |
|
| 32 |
private array $objectBytes = []; |
| 33 |
|
| 34 |
|
| 35 |
private array $objectTerminal = []; |
| 36 |
|
| 37 |
|
| 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 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 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 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 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 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 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 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
public function incrementTotals(int $bytes, int $files = 1): void |
| 118 |
{ |
| 119 |
$this->totalBytes += $bytes; |
| 120 |
$this->totalFiles += $files; |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
public function bytesTransferred(array $context): bool |
| 127 |
{ |
| 128 |
|
| 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 |
|
| 141 |
|
| 142 |
public function transferComplete(array $context): void |
| 143 |
{ |
| 144 |
|
| 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 |
|
| 155 |
|
| 156 |
public function transferFail(array $context): void |
| 157 |
{ |
| 158 |
|
| 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 |
|
| 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 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
private function updateObjectProgress(TransferProgressSnapshot $snapshot): void |
| 188 |
{ |
| 189 |
$identifier = $snapshot->getIdentifier(); |
| 190 |
$previous = $this->objectBytes[$identifier] ?? 0; |
| 191 |
$current = $snapshot->getTransferredBytes(); |
| 192 |
|
| 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 |
|
| 204 |
|
| 205 |
|
| 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 |
} |