| @@ -1,8 +1,9 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace Dudlewebs\WPMCS\s3\GuzzleHttp; |
| 4 | 4 | |
| 5 | +use Dudlewebs\WPMCS\s3\GuzzleHttp\Promise as P; | |
| 5 | 6 | use Dudlewebs\WPMCS\s3\GuzzleHttp\Promise\EachPromise; |
| 6 | 7 | use Dudlewebs\WPMCS\s3\GuzzleHttp\Promise\PromiseInterface; |
| 7 | 8 | use Dudlewebs\WPMCS\s3\GuzzleHttp\Promise\PromisorInterface; |
| 8 | 9 | use Dudlewebs\WPMCS\s3\Psr\Http\Message\RequestInterface; |
| @@ -15,12 +16,16 @@ | ||
| 15 | 16 | * |
| 16 | 17 | * When a function is yielded by the iterator, the function is provided the |
| 17 | 18 | * "request_options" array that should be merged on top of any existing |
| 18 | 19 | * options, and the function MUST then return a wait-able promise. |
| 20 | + * | |
| 21 | + * @final | |
| 19 | 22 | */ |
| 20 | 23 | class Pool implements PromisorInterface |
| 21 | 24 | { |
| 22 | - /** @var EachPromise */ | |
| 25 | + /** | |
| 26 | + * @var EachPromise | |
| 27 | + */ | |
| 23 | 28 | private $each; |
| 24 | 29 | /** |
| 25 | 30 | * @param ClientInterface $client Client used to send the requests. |
| 26 | 31 | * @param array|\Iterator $requests Requests or functions that return |
| @@ -25,19 +30,16 @@ | ||
| 25 | 30 | * @param ClientInterface $client Client used to send the requests. |
| 26 | 31 | * @param array|\Iterator $requests Requests or functions that return |
| 27 | 32 | * requests to send concurrently. |
| 28 | 33 | * @param array $config Associative array of options |
| 29 | - * - concurrency: (int) Maximum number of requests to send concurrently | |
| 30 | - * - options: Array of request options to apply to each request. | |
| 31 | - * - fulfilled: (callable) Function to invoke when a request completes. | |
| 32 | - * - rejected: (callable) Function to invoke when a request is rejected. | |
| 34 | + * - concurrency: (int) Maximum number of requests to send concurrently | |
| 35 | + * - options: Array of request options to apply to each request. | |
| 36 | + * - fulfilled: (callable) Function to invoke when a request completes. | |
| 37 | + * - rejected: (callable) Function to invoke when a request is rejected. | |
| 33 | 38 | */ |
| 34 | 39 | public function __construct(ClientInterface $client, $requests, array $config = []) |
| 35 | 40 | { |
| 36 | - // Backwards compatibility. | |
| 37 | - if (isset($config['pool_size'])) { | |
| 38 | - $config['concurrency'] = $config['pool_size']; | |
| 39 | - } elseif (!isset($config['concurrency'])) { | |
| 41 | + if (!isset($config['concurrency'])) { | |
| 40 | 42 | $config['concurrency'] = 25; |
| 41 | 43 | } |
| 42 | 44 | if (isset($config['options'])) { |
| 43 | 45 | $opts = $config['options']; |
| @@ -44,10 +46,10 @@ | ||
| 44 | 46 | unset($config['options']); |
| 45 | 47 | } else { |
| 46 | 48 | $opts = []; |
| 47 | 49 | } |
| 48 | - $iterable = \Dudlewebs\WPMCS\s3\GuzzleHttp\Promise\iter_for($requests); | |
| 49 | - $requests = function () use($iterable, $client, $opts) { | |
| 50 | + $iterable = P\Create::iterFor($requests); | |
| 51 | + $requests = static function () use($iterable, $client, $opts) { | |
| 50 | 52 | foreach ($iterable as $key => $rfn) { |
| 51 | 53 | if ($rfn instanceof RequestInterface) { |
| 52 | 54 | (yield $key => $client->sendAsync($rfn, $opts)); |
| 53 | 55 | } elseif (\is_callable($rfn)) { |
| @@ -52,9 +54,9 @@ | ||
| 52 | 54 | (yield $key => $client->sendAsync($rfn, $opts)); |
| 53 | 55 | } elseif (\is_callable($rfn)) { |
| 54 | 56 | (yield $key => $rfn($opts)); |
| 55 | 57 | } else { |
| 56 | - throw new \InvalidArgumentException('Each value yielded by ' . 'the iterator must be a Psr7\\Http\\Message\\RequestInterface ' . 'or a callable that returns a promise that fulfills ' . 'with a Psr7\\Message\\Http\\ResponseInterface object.'); | |
| 58 | + throw new \InvalidArgumentException('Each value yielded by the iterator must be a Psr7\\Http\\Message\\RequestInterface or a callable that returns a promise that fulfills with a Psr7\\Message\\Http\\ResponseInterface object.'); | |
| 57 | 59 | } |
| 58 | 60 | } |
| 59 | 61 | }; |
| 60 | 62 | $this->each = new EachPromise($requests(), $config); |
| @@ -60,12 +62,10 @@ | ||
| 60 | 62 | $this->each = new EachPromise($requests(), $config); |
| 61 | 63 | } |
| 62 | 64 | /** |
| 63 | 65 | * Get promise |
| 64 | - * | |
| 65 | - * @return PromiseInterface | |
| 66 | 66 | */ |
| 67 | - public function promise() | |
| 67 | + public function promise() : PromiseInterface | |
| 68 | 68 | { |
| 69 | 69 | return $this->each->promise(); |
| 70 | 70 | } |
| 71 | 71 | /** |
| @@ -78,15 +78,16 @@ | ||
| 78 | 78 | * |
| 79 | 79 | * @param ClientInterface $client Client used to send the requests |
| 80 | 80 | * @param array|\Iterator $requests Requests to send concurrently. |
| 81 | 81 | * @param array $options Passes through the options available in |
| 82 | - * {@see GuzzleHttp\Pool::__construct} | |
| 82 | + * {@see Pool::__construct} | |
| 83 | 83 | * |
| 84 | 84 | * @return array Returns an array containing the response or an exception |
| 85 | 85 | * in the same order that the requests were sent. |
| 86 | + * | |
| 86 | 87 | * @throws \InvalidArgumentException if the event format is incorrect. |
| 87 | 88 | */ |
| 88 | - public static function batch(ClientInterface $client, $requests, array $options = []) | |
| 89 | + public static function batch(ClientInterface $client, $requests, array $options = []) : array | |
| 89 | 90 | { |
| 90 | 91 | $res = []; |
| 91 | 92 | self::cmpCallback($options, 'fulfilled', $res); |
| 92 | 93 | self::cmpCallback($options, 'rejected', $res); |
| @@ -96,20 +97,18 @@ | ||
| 96 | 97 | return $res; |
| 97 | 98 | } |
| 98 | 99 | /** |
| 99 | 100 | * Execute callback(s) |
| 100 | - * | |
| 101 | - * @return void | |
| 102 | 101 | */ |
| 103 | - private static function cmpCallback(array &$options, $name, array &$results) | |
| 102 | + private static function cmpCallback(array &$options, string $name, array &$results) : void | |
| 104 | 103 | { |
| 105 | 104 | if (!isset($options[$name])) { |
| 106 | - $options[$name] = function ($v, $k) use(&$results) { | |
| 105 | + $options[$name] = static function ($v, $k) use(&$results) { | |
| 107 | 106 | $results[$k] = $v; |
| 108 | 107 | }; |
| 109 | 108 | } else { |
| 110 | 109 | $currentFn = $options[$name]; |
| 111 | - $options[$name] = function ($v, $k) use(&$results, $currentFn) { | |
| 110 | + $options[$name] = static function ($v, $k) use(&$results, $currentFn) { | |
| 112 | 111 | $currentFn($v, $k); |
| 113 | 112 | $results[$k] = $v; |
| 114 | 113 | }; |
| 115 | 114 | } |