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