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