| 1 |
<?php |
| 2 |
|
| 3 |
namespace Dudlewebs\WPMCS\s3\Aws\EndpointV2; |
| 4 |
|
| 5 |
use Dudlewebs\WPMCS\s3\Aws\Api\Serializer\RestSerializer; |
| 6 |
use Dudlewebs\WPMCS\s3\Aws\EndpointV2\Ruleset\RulesetEndpoint; |
| 7 |
use Dudlewebs\WPMCS\s3\GuzzleHttp\Psr7\Uri; |
| 8 |
/** |
| 9 |
* Set of helper functions used to set endpoints and endpoint |
| 10 |
* properties derived from dynamic endpoint resolution. |
| 11 |
* |
| 12 |
* @internal |
| 13 |
*/ |
| 14 |
trait EndpointV2SerializerTrait |
| 15 |
{ |
| 16 |
/** |
| 17 |
* Applies a resolved endpoint, headers and any custom HTTP schemes provided |
| 18 |
* in client configuration to options which are applied to the serialized request. |
| 19 |
* |
| 20 |
* @param $endpoint |
| 21 |
* @param $headers |
| 22 |
* |
| 23 |
* @return void |
| 24 |
*/ |
| 25 |
private function setEndpointV2RequestOptions(RulesetEndpoint $endpoint, array &$headers) : void |
| 26 |
{ |
| 27 |
$this->applyHeaders($endpoint, $headers); |
| 28 |
$resolvedUrl = $endpoint->getUrl(); |
| 29 |
$this->applyScheme($resolvedUrl); |
| 30 |
$this->endpoint = $this instanceof RestSerializer ? new Uri($resolvedUrl) : $resolvedUrl; |
| 31 |
} |
| 32 |
/** |
| 33 |
* Combines modeled headers and headers resolved from an endpoint object. |
| 34 |
* |
| 35 |
* @param $endpoint |
| 36 |
* @param $headers |
| 37 |
* @return void |
| 38 |
*/ |
| 39 |
private function applyHeaders(RulesetEndpoint $endpoint, array &$headers) : void |
| 40 |
{ |
| 41 |
if (!\is_null($endpoint->getHeaders())) { |
| 42 |
$headers = \array_merge($headers, $endpoint->getHeaders()); |
| 43 |
} |
| 44 |
} |
| 45 |
/** |
| 46 |
* Applies custom HTTP schemes provided in client configuration. |
| 47 |
* |
| 48 |
* @param $resolvedUrl |
| 49 |
* @return void |
| 50 |
*/ |
| 51 |
private function applyScheme(&$resolvedUrl) : void |
| 52 |
{ |
| 53 |
$resolvedEndpointScheme = \parse_url($resolvedUrl, \PHP_URL_SCHEME); |
| 54 |
$scheme = $this->endpoint instanceof Uri ? $this->endpoint->getScheme() : \parse_url($this->endpoint, \PHP_URL_SCHEME); |
| 55 |
if (!empty($scheme) && $scheme !== $resolvedEndpointScheme) { |
| 56 |
$resolvedUrl = \str_replace($resolvedEndpointScheme, $scheme, $resolvedUrl); |
| 57 |
} |
| 58 |
} |
| 59 |
} |
| 60 |
|