| 1 |
<?php |
| 2 |
|
| 3 |
namespace Dudlewebs\WPMCS\s3\Aws\EndpointV2; |
| 4 |
|
| 5 |
use Dudlewebs\WPMCS\s3\Aws\EndpointV2\Ruleset\Ruleset; |
| 6 |
use Dudlewebs\WPMCS\s3\Aws\EndpointV2\Ruleset\RulesetEndpoint; |
| 7 |
use Dudlewebs\WPMCS\s3\Aws\Exception\UnresolvedEndpointException; |
| 8 |
use Dudlewebs\WPMCS\s3\Aws\LruArrayCache; |
| 9 |
/** |
| 10 |
* Given a service's Ruleset and client-provided input parameters, provides |
| 11 |
* either an object reflecting the properties of a resolved endpoint, |
| 12 |
* or throws an error. |
| 13 |
*/ |
| 14 |
class EndpointProviderV2 |
| 15 |
{ |
| 16 |
/** @var Ruleset */ |
| 17 |
private $ruleset; |
| 18 |
/** @var LruArrayCache */ |
| 19 |
private $cache; |
| 20 |
public function __construct(array $ruleset, array $partitions) |
| 21 |
{ |
| 22 |
$this->ruleset = new Ruleset($ruleset, $partitions); |
| 23 |
$this->cache = new LruArrayCache(100); |
| 24 |
} |
| 25 |
/** |
| 26 |
* @return Ruleset |
| 27 |
*/ |
| 28 |
public function getRuleset() |
| 29 |
{ |
| 30 |
return $this->ruleset; |
| 31 |
} |
| 32 |
/** |
| 33 |
* Given a Ruleset and input parameters, determines the correct endpoint |
| 34 |
* or an error to be thrown for a given request. |
| 35 |
* |
| 36 |
* @return RulesetEndpoint |
| 37 |
* @throws UnresolvedEndpointException |
| 38 |
*/ |
| 39 |
public function resolveEndpoint(array $inputParameters) |
| 40 |
{ |
| 41 |
$hashedParams = $this->hashInputParameters($inputParameters); |
| 42 |
$match = $this->cache->get($hashedParams); |
| 43 |
if (!\is_null($match)) { |
| 44 |
return $match; |
| 45 |
} |
| 46 |
$endpoint = $this->ruleset->evaluate($inputParameters); |
| 47 |
if ($endpoint === \false) { |
| 48 |
throw new UnresolvedEndpointException('Unable to resolve an endpoint using the provider arguments: ' . \json_encode($inputParameters)); |
| 49 |
} |
| 50 |
$this->cache->set($hashedParams, $endpoint); |
| 51 |
return $endpoint; |
| 52 |
} |
| 53 |
private function hashInputParameters($inputParameters) |
| 54 |
{ |
| 55 |
return \md5(\serialize($inputParameters)); |
| 56 |
} |
| 57 |
} |
| 58 |
|