| 1 |
<?php |
| 2 |
|
| 3 |
namespace Aws\EndpointV2\Bdd; |
| 4 |
|
| 5 |
use Aws\EndpointV2\Ruleset\RulesetEndpoint; |
| 6 |
use Aws\EndpointV2\Ruleset\RulesetStandardLibrary; |
| 7 |
use Aws\Exception\UnresolvedEndpointException; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
final class BddResultResolver |
| 22 |
{ |
| 23 |
public function __construct( |
| 24 |
private readonly BddRuleset $ruleset |
| 25 |
) { |
| 26 |
} |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
public function resolve(int $resultIndex, array $inputParameters): RulesetEndpoint |
| 32 |
{ |
| 33 |
if ($resultIndex === 0) { |
| 34 |
$this->resolveNoMatch($inputParameters); |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
$results = $this->ruleset->getResults(); |
| 40 |
$result = $results[$resultIndex - 1] ?? null; |
| 41 |
|
| 42 |
if ($result === null) { |
| 43 |
throw new UnresolvedEndpointException(sprintf( |
| 44 |
'Endpoint BDD referenced unknown result index %d.', |
| 45 |
$resultIndex |
| 46 |
)); |
| 47 |
} |
| 48 |
|
| 49 |
if (isset($result['error'])) { |
| 50 |
$this->throwError($result, $inputParameters); |
| 51 |
} |
| 52 |
|
| 53 |
if (!isset($result['endpoint'])) { |
| 54 |
throw new UnresolvedEndpointException( |
| 55 |
'Endpoint BDD result is missing an `endpoint` or `error` block.' |
| 56 |
); |
| 57 |
} |
| 58 |
|
| 59 |
return $this->buildEndpoint($result['endpoint'], $inputParameters); |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
public function resolveNoMatch(array $inputParameters): never |
| 66 |
{ |
| 67 |
throw new UnresolvedEndpointException( |
| 68 |
'Unable to resolve an endpoint using the provider arguments: ' |
| 69 |
. json_encode($inputParameters) |
| 70 |
); |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
private function throwError(array $result, array $inputParameters): never |
| 77 |
{ |
| 78 |
$message = $this->ruleset->standardLibrary->resolveValue( |
| 79 |
$result['error'], |
| 80 |
$inputParameters |
| 81 |
); |
| 82 |
throw new UnresolvedEndpointException((string) $message); |
| 83 |
} |
| 84 |
|
| 85 |
private function buildEndpoint(array $endpoint, array $inputParameters): RulesetEndpoint |
| 86 |
{ |
| 87 |
$library = $this->ruleset->standardLibrary; |
| 88 |
|
| 89 |
$url = $library->resolveValue($endpoint['url'], $inputParameters); |
| 90 |
$properties = isset($endpoint['properties']) |
| 91 |
? $this->resolveProperties($endpoint['properties'], $inputParameters, $library) |
| 92 |
: null; |
| 93 |
$headers = isset($endpoint['headers']) |
| 94 |
? $this->resolveHeaders($endpoint['headers'], $inputParameters, $library) |
| 95 |
: null; |
| 96 |
|
| 97 |
return new RulesetEndpoint($url, $properties, $headers); |
| 98 |
} |
| 99 |
|
| 100 |
private function resolveProperties( |
| 101 |
$properties, |
| 102 |
array $inputParameters, |
| 103 |
RulesetStandardLibrary $library |
| 104 |
) { |
| 105 |
if (is_array($properties)) { |
| 106 |
$resolved = []; |
| 107 |
foreach ($properties as $key => $value) { |
| 108 |
$resolved[$key] = $this->resolveProperties( |
| 109 |
$value, |
| 110 |
$inputParameters, |
| 111 |
$library |
| 112 |
); |
| 113 |
} |
| 114 |
return $resolved; |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
if (is_string($properties) && str_contains($properties, '{') && $library->isTemplate($properties)) { |
| 119 |
return $library->resolveTemplateString($properties, $inputParameters); |
| 120 |
} |
| 121 |
|
| 122 |
return $properties; |
| 123 |
} |
| 124 |
|
| 125 |
private function resolveHeaders( |
| 126 |
array $headers, |
| 127 |
array $inputParameters, |
| 128 |
RulesetStandardLibrary $library |
| 129 |
): array { |
| 130 |
$resolved = []; |
| 131 |
foreach ($headers as $name => $values) { |
| 132 |
$resolvedValues = []; |
| 133 |
foreach ($values as $value) { |
| 134 |
$resolvedValues[] = $library->resolveValue($value, $inputParameters); |
| 135 |
} |
| 136 |
$resolved[$name] = $resolvedValues; |
| 137 |
} |
| 138 |
return $resolved; |
| 139 |
} |
| 140 |
} |