| 1 |
<?php |
| 2 |
|
| 3 |
namespace Dudlewebs\WPMCS\s3\Aws; |
| 4 |
|
| 5 |
use Dudlewebs\WPMCS\s3\Aws\Api\Service; |
| 6 |
/** |
| 7 |
* A trait providing generic functionality for interacting with Amazon Web |
| 8 |
* Services. This is meant to be used in classes implementing |
| 9 |
* \Aws\AwsClientInterface |
| 10 |
*/ |
| 11 |
trait AwsClientTrait |
| 12 |
{ |
| 13 |
public function getPaginator($name, array $args = []) |
| 14 |
{ |
| 15 |
$config = $this->getApi()->getPaginatorConfig($name); |
| 16 |
return new ResultPaginator($this, $name, $args, $config); |
| 17 |
} |
| 18 |
public function getIterator($name, array $args = []) |
| 19 |
{ |
| 20 |
$config = $this->getApi()->getPaginatorConfig($name); |
| 21 |
if (!$config['result_key']) { |
| 22 |
throw new \UnexpectedValueException(\sprintf('There are no resources to iterate for the %s operation of %s', $name, $this->getApi()['serviceFullName'])); |
| 23 |
} |
| 24 |
$key = \is_array($config['result_key']) ? $config['result_key'][0] : $config['result_key']; |
| 25 |
if ($config['output_token'] && $config['input_token']) { |
| 26 |
return $this->getPaginator($name, $args)->search($key); |
| 27 |
} |
| 28 |
$result = $this->execute($this->getCommand($name, $args))->search($key); |
| 29 |
return new \ArrayIterator((array) $result); |
| 30 |
} |
| 31 |
public function waitUntil($name, array $args = []) |
| 32 |
{ |
| 33 |
return $this->getWaiter($name, $args)->promise()->wait(); |
| 34 |
} |
| 35 |
public function getWaiter($name, array $args = []) |
| 36 |
{ |
| 37 |
$config = isset($args['@waiter']) ? $args['@waiter'] : []; |
| 38 |
$config += $this->getApi()->getWaiterConfig($name); |
| 39 |
return new Waiter($this, $name, $args, $config); |
| 40 |
} |
| 41 |
public function execute(CommandInterface $command) |
| 42 |
{ |
| 43 |
return $this->executeAsync($command)->wait(); |
| 44 |
} |
| 45 |
public function executeAsync(CommandInterface $command) |
| 46 |
{ |
| 47 |
$handler = $command->getHandlerList()->resolve(); |
| 48 |
return $handler($command); |
| 49 |
} |
| 50 |
public function __call($name, array $args) |
| 51 |
{ |
| 52 |
if (\substr($name, -5) === 'Async') { |
| 53 |
$name = \substr($name, 0, -5); |
| 54 |
$isAsync = \true; |
| 55 |
} |
| 56 |
if (!empty($this->aliases[\ucfirst($name)])) { |
| 57 |
$name = $this->aliases[\ucfirst($name)]; |
| 58 |
} |
| 59 |
$params = isset($args[0]) ? $args[0] : []; |
| 60 |
if (!empty($isAsync)) { |
| 61 |
return $this->executeAsync($this->getCommand($name, $params)); |
| 62 |
} |
| 63 |
return $this->execute($this->getCommand($name, $params)); |
| 64 |
} |
| 65 |
/** |
| 66 |
* @param string $name |
| 67 |
* @param array $args |
| 68 |
* |
| 69 |
* @return CommandInterface |
| 70 |
*/ |
| 71 |
public abstract function getCommand($name, array $args = []); |
| 72 |
/** |
| 73 |
* @return Service |
| 74 |
*/ |
| 75 |
public abstract function getApi(); |
| 76 |
} |
| 77 |
|