Completed
Push — 2.x ( 9685d0...843cc1 )
by Frank
01:13
created

S3ClientStub.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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;
0 ignored issues
show
Constructors do not have meaningful return values, anything that is returned from here is discarded. Are you sure this is correct?
Loading history...
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
        return $this->actualClient->upload($bucket, $key, $body, $acl, $options);
65
    }
66
67
    public function failOnNextCopy(): void
68
    {
69
        $this->throwExceptionWhenExecutingCommand('CopyObject');
70
    }
71
72
    public function throwExceptionWhenExecutingCommand(string $commandName, S3Exception $exception = null): void
73
    {
74
        $this->stagedExceptions[$commandName] = $exception ?: new S3Exception($commandName, new Command($commandName));
75
    }
76
77
    public function throw500ExceptionWhenExecutingCommand(string $commandName): void
78
    {
79
        $response = new Response(500);
80
        $exception = new S3Exception($commandName, new Command($commandName), compact('response'));
81
82
        $this->throwExceptionWhenExecutingCommand($commandName, $exception);
83
    }
84
85
    public function stageResultForCommand(string $commandName, ResultInterface $result): void
86
    {
87
        $this->stagedResult[$commandName] = $result;
88
    }
89
90
    public function execute(CommandInterface $command)
91
    {
92
        return $this->executeAsync($command)->wait();
93
    }
94
95
    public function getCommand($name, array $args = [])
96
    {
97
        return $this->actualClient->getCommand($name, $args);
98
    }
99
100
    public function getHandlerList()
101
    {
102
        return $this->actualClient->getHandlerList();
103
    }
104
105
    public function getIterator($name, array $args = [])
106
    {
107
        return $this->actualClient->getIterator($name, $args);
108
    }
109
110
    public function __call($name, array $arguments)
111
    {
112
        return $this->actualClient->__call($name, $arguments);
113
    }
114
115
    public function executeAsync(CommandInterface $command)
116
    {
117
        $name = $command->getName();
118
119
        if (array_key_exists($name, $this->stagedExceptions)) {
120
            $exception = $this->stagedExceptions[$name];
121
            unset($this->stagedExceptions[$name]);
122
            throw $exception;
123
        }
124
125
        if (array_key_exists($name, $this->stagedResult)) {
126
            $result = $this->stagedResult[$name];
127
            unset($this->stagedResult[$name]);
128
129
            return promise_for($result);
0 ignored issues
show
Deprecated Code introduced by
The function GuzzleHttp\Promise\promise_for() has been deprecated with message: promise_for will be removed in guzzlehttp/promises:2.0. Use Create::promiseFor instead.

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
130
        }
131
132
        return $this->actualClient->executeAsync($command);
133
    }
134
135
    public function getCredentials()
136
    {
137
        return $this->actualClient->getCredentials();
138
    }
139
140
    public function getRegion()
141
    {
142
        return $this->actualClient->getRegion();
143
    }
144
145
    public function getEndpoint()
146
    {
147
        return $this->actualClient->getEndpoint();
148
    }
149
150
    public function getApi()
151
    {
152
        return $this->actualClient->getApi();
153
    }
154
155
    public function getConfig($option = null)
156
    {
157
        return $this->actualClient->getConfig($option);
158
    }
159
160
    public function getPaginator($name, array $args = [])
161
    {
162
        return $this->actualClient->getPaginator($name, $args);
163
    }
164
165
    public function waitUntil($name, array $args = [])
166
    {
167
        $this->actualClient->waitUntil($name, $args);
168
    }
169
170
    public function getWaiter($name, array $args = [])
171
    {
172
        return $this->actualClient->getWaiter($name, $args);
173
    }
174
175
    public function createPresignedRequest(CommandInterface $command, $expires, array $options = [])
176
    {
177
        return $this->actualClient->createPresignedRequest($command, $expires, $options);
178
    }
179
180
    public function getObjectUrl($bucket, $key)
181
    {
182
        return $this->actualClient->getObjectUrl($bucket, $key);
183
    }
184
}
185