Exception
11 months ago
Configuration.php
11 months ago
ConfigurationInterface.php
11 months ago
ConfigurationProvider.php
11 months ago
EndpointDiscoveryMiddleware.php
11 months ago
EndpointList.php
11 months ago
EndpointList.php
86 lines
| 1 | <?php |
| 2 | namespace Aws\EndpointDiscovery; |
| 3 | |
| 4 | class EndpointList |
| 5 | { |
| 6 | private $active; |
| 7 | private $expired = []; |
| 8 | |
| 9 | public function __construct(array $endpoints) |
| 10 | { |
| 11 | $this->active = $endpoints; |
| 12 | reset($this->active); |
| 13 | } |
| 14 | |
| 15 | /** |
| 16 | * Gets an active (unexpired) endpoint. Returns null if none found. |
| 17 | * |
| 18 | * @return null|string |
| 19 | */ |
| 20 | public function getActive() |
| 21 | { |
| 22 | if (count($this->active) < 1) { |
| 23 | return null; |
| 24 | } |
| 25 | while (time() > current($this->active)) { |
| 26 | $key = key($this->active); |
| 27 | $this->expired[$key] = current($this->active); |
| 28 | $this->increment($this->active); |
| 29 | unset($this->active[$key]); |
| 30 | if (count($this->active) < 1) { |
| 31 | return null; |
| 32 | } |
| 33 | } |
| 34 | $active = key($this->active); |
| 35 | $this->increment($this->active); |
| 36 | return $active; |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Gets an active endpoint if possible, then an expired endpoint if possible. |
| 41 | * Returns null if no endpoints found. |
| 42 | * |
| 43 | * @return null|string |
| 44 | */ |
| 45 | public function getEndpoint() |
| 46 | { |
| 47 | if (!empty($active = $this->getActive())) { |
| 48 | return $active; |
| 49 | } |
| 50 | return $this->getExpired(); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Removes an endpoint from both lists. |
| 55 | * |
| 56 | * @param string $key |
| 57 | */ |
| 58 | public function remove($key) |
| 59 | { |
| 60 | unset($this->active[$key]); |
| 61 | unset($this->expired[$key]); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Get an expired endpoint. Returns null if none found. |
| 66 | * |
| 67 | * @return null|string |
| 68 | */ |
| 69 | private function getExpired() |
| 70 | { |
| 71 | if (count($this->expired) < 1) { |
| 72 | return null; |
| 73 | } |
| 74 | $expired = key($this->expired); |
| 75 | $this->increment($this->expired); |
| 76 | return $expired; |
| 77 | } |
| 78 | |
| 79 | private function increment(&$array) |
| 80 | { |
| 81 | if (next($array) === false) { |
| 82 | reset($array); |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 |