1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace League\Flysystem\AwsS3V3; |
6
|
|
|
|
7
|
|
|
use Aws\Command; |
8
|
|
|
use Aws\CommandInterface; |
9
|
|
|
use Aws\ResultInterface; |
10
|
|
|
use Aws\S3\Exception\S3Exception; |
11
|
|
|
use Aws\S3\S3ClientInterface; |
12
|
|
|
use Aws\S3\S3ClientTrait; |
13
|
|
|
use GuzzleHttp\Psr7\Response; |
14
|
|
|
|
15
|
|
|
use Throwable; |
16
|
|
|
|
17
|
|
|
use function GuzzleHttp\Promise\promise_for; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @codeCoverageIgnore |
21
|
|
|
*/ |
22
|
|
|
class S3ClientStub implements S3ClientInterface |
23
|
|
|
{ |
24
|
|
|
use S3ClientTrait; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var S3ClientInterface |
28
|
|
|
*/ |
29
|
|
|
private $actualClient; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var S3Exception[] |
33
|
|
|
*/ |
34
|
|
|
private $stagedExceptions = []; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var ResultInterface[] |
38
|
|
|
*/ |
39
|
|
|
private $stagedResult = []; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var Throwable|null |
43
|
|
|
*/ |
44
|
|
|
private $exceptionForUpload = null; |
45
|
|
|
|
46
|
|
|
public function __construct(S3ClientInterface $client) |
47
|
|
|
{ |
48
|
|
|
return $this->actualClient = $client; |
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function throwDuringUpload(Throwable $throwable): void |
52
|
|
|
{ |
53
|
|
|
$this->exceptionForUpload = $throwable; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function upload($bucket, $key, $body, $acl = 'private', array $options = []) |
57
|
|
|
{ |
58
|
|
|
if ($this->exceptionForUpload instanceof Throwable) { |
59
|
|
|
$throwable = $this->exceptionForUpload; |
60
|
|
|
$this->exceptionForUpload = null; |
61
|
|
|
throw $throwable; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function failOnNextCopy(): void |
66
|
|
|
{ |
67
|
|
|
$this->throwExceptionWhenExecutingCommand('CopyObject'); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function throwExceptionWhenExecutingCommand(string $commandName, S3Exception $exception = null): void |
71
|
|
|
{ |
72
|
|
|
$this->stagedExceptions[$commandName] = $exception ?: new S3Exception($commandName, new Command($commandName)); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function throw500ExceptionWhenExecutingCommand(string $commandName): void |
76
|
|
|
{ |
77
|
|
|
$response = new Response(500); |
78
|
|
|
$exception = new S3Exception($commandName, new Command($commandName), compact('response')); |
79
|
|
|
|
80
|
|
|
$this->throwExceptionWhenExecutingCommand($commandName, $exception); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function stageResultForCommand(string $commandName, ResultInterface $result): void |
84
|
|
|
{ |
85
|
|
|
$this->stagedResult[$commandName] = $result; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function execute(CommandInterface $command) |
89
|
|
|
{ |
90
|
|
|
return $this->executeAsync($command)->wait(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function getCommand($name, array $args = []) |
94
|
|
|
{ |
95
|
|
|
return $this->actualClient->getCommand($name, $args); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function getHandlerList() |
99
|
|
|
{ |
100
|
|
|
return $this->actualClient->getHandlerList(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function getIterator($name, array $args = []) |
104
|
|
|
{ |
105
|
|
|
return $this->actualClient->getIterator($name, $args); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function __call($name, array $arguments) |
109
|
|
|
{ |
110
|
|
|
return $this->actualClient->__call($name, $arguments); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function executeAsync(CommandInterface $command) |
114
|
|
|
{ |
115
|
|
|
$name = $command->getName(); |
116
|
|
|
|
117
|
|
|
if (array_key_exists($name, $this->stagedExceptions)) { |
118
|
|
|
$exception = $this->stagedExceptions[$name]; |
119
|
|
|
unset($this->stagedExceptions[$name]); |
120
|
|
|
throw $exception; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
if (array_key_exists($name, $this->stagedResult)) { |
124
|
|
|
$result = $this->stagedResult[$name]; |
125
|
|
|
unset($this->stagedResult[$name]); |
126
|
|
|
|
127
|
|
|
return promise_for($result); |
|
|
|
|
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return $this->actualClient->executeAsync($command); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function getCredentials() |
134
|
|
|
{ |
135
|
|
|
return $this->actualClient->getCredentials(); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function getRegion() |
139
|
|
|
{ |
140
|
|
|
return $this->actualClient->getRegion(); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function getEndpoint() |
144
|
|
|
{ |
145
|
|
|
return $this->actualClient->getEndpoint(); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function getApi() |
149
|
|
|
{ |
150
|
|
|
return $this->actualClient->getApi(); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function getConfig($option = null) |
154
|
|
|
{ |
155
|
|
|
return $this->actualClient->getConfig($option); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
public function getPaginator($name, array $args = []) |
159
|
|
|
{ |
160
|
|
|
return $this->actualClient->getPaginator($name, $args); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
public function waitUntil($name, array $args = []) |
164
|
|
|
{ |
165
|
|
|
$this->actualClient->waitUntil($name, $args); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
public function getWaiter($name, array $args = []) |
169
|
|
|
{ |
170
|
|
|
return $this->actualClient->getWaiter($name, $args); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
public function createPresignedRequest(CommandInterface $command, $expires, array $options = []) |
174
|
|
|
{ |
175
|
|
|
return $this->actualClient->createPresignedRequest($command, $expires, $options); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
public function getObjectUrl($bucket, $key) |
179
|
|
|
{ |
180
|
|
|
return $this->actualClient->getObjectUrl($bucket, $key); |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|