PluginProbe
Media Cloud Sync / 1.2.8
Media Cloud Sync v1.2.8
1.4.2 1.4.1 1.4.0 1.3.12 1.3.11 1.3.10 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.2.0 1.2.10 1.2.11 1.2.12 1.2.13 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 All 36 releases
media-cloud-sync / includes / sdk / s3 / Aws / Handler / GuzzleV6 / GuzzleHandler.php

GuzzleHandler.php in Media Cloud Sync 1.2.8, at includes/sdk/s3/Aws/Handler/GuzzleV6/GuzzleHandler.php

62 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Dudlewebs\WPMCS\s3\Aws\Handler\GuzzleV6;
4
5 use Exception;
6 use Dudlewebs\WPMCS\s3\GuzzleHttp\Exception\ConnectException;
7 use Dudlewebs\WPMCS\s3\GuzzleHttp\Exception\RequestException;
8 use Dudlewebs\WPMCS\s3\GuzzleHttp\Promise;
9 use Dudlewebs\WPMCS\s3\GuzzleHttp\Client;
10 use Dudlewebs\WPMCS\s3\GuzzleHttp\ClientInterface;
11 use Dudlewebs\WPMCS\s3\GuzzleHttp\TransferStats;
12 use Dudlewebs\WPMCS\s3\Psr\Http\Message\RequestInterface as Psr7Request;
13 /**
14 * A request handler that sends PSR-7-compatible requests with Guzzle 6.
15 */
16 class GuzzleHandler
17 {
18 /** @var ClientInterface */
19 private $client;
20 /**
21 * @param ClientInterface $client
22 */
23 public function __construct(ClientInterface $client = null)
24 {
25 $this->client = $client ?: new Client();
26 }
27 /**
28 * @param Psr7Request $request
29 * @param array $options
30 *
31 * @return Promise\Promise
32 */
33 public function __invoke(Psr7Request $request, array $options = [])
34 {
35 $request = $request->withHeader('User-Agent', $request->getHeaderLine('User-Agent') . ' ' . \Dudlewebs\WPMCS\s3\GuzzleHttp\default_user_agent());
36 return $this->client->sendAsync($request, $this->parseOptions($options))->otherwise(static function ($e) {
37 $error = ['exception' => $e, 'connection_error' => $e instanceof ConnectException, 'response' => null];
38 if ($e instanceof RequestException && $e->getResponse()) {
39 $error['response'] = $e->getResponse();
40 }
41 return new Promise\RejectedPromise($error);
42 });
43 }
44 private function parseOptions(array $options)
45 {
46 if (isset($options['http_stats_receiver'])) {
47 $fn = $options['http_stats_receiver'];
48 unset($options['http_stats_receiver']);
49 $prev = isset($options['on_stats']) ? $options['on_stats'] : null;
50 $options['on_stats'] = static function (TransferStats $stats) use($fn, $prev) {
51 if (\is_callable($prev)) {
52 $prev($stats);
53 }
54 $transferStats = ['total_time' => $stats->getTransferTime()];
55 $transferStats += $stats->getHandlerStats();
56 $fn($transferStats);
57 };
58 }
59 return $options;
60 }
61 }
62