| Total Complexity | 6 |
| Total Lines | 75 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 12 | class Provider |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * For TSC Duration Seconds |
||
| 16 | */ |
||
| 17 | const DURATION_SECONDS = 3600; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var array |
||
| 21 | */ |
||
| 22 | protected static $credentialsCache = []; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Expiration time slot for temporary security credentials. |
||
| 26 | * |
||
| 27 | * @var int |
||
| 28 | */ |
||
| 29 | protected $expirationSlot = 180; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var Client |
||
| 33 | */ |
||
| 34 | protected $client; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | protected $error = 'Result contains no credentials'; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * CredentialTrait constructor. |
||
| 43 | * |
||
| 44 | * @param Client $client |
||
| 45 | */ |
||
| 46 | 47 | public function __construct(Client $client) |
|
| 47 | { |
||
| 48 | 47 | $this->client = $client; |
|
| 49 | 47 | } |
|
| 50 | |||
| 51 | /** |
||
| 52 | * Get the credentials from the cache in the validity period. |
||
| 53 | * |
||
| 54 | * @return array|null |
||
| 55 | */ |
||
| 56 | 33 | public function getCredentialsInCache() |
|
| 57 | { |
||
| 58 | 33 | if (isset(self::$credentialsCache[$this->key()])) { |
|
| 59 | 13 | $result = self::$credentialsCache[$this->key()]; |
|
| 60 | 13 | if (\strtotime($result['Expiration']) - \time() >= $this->expirationSlot) { |
|
| 61 | 6 | return $result; |
|
| 62 | } |
||
| 63 | 7 | unset(self::$credentialsCache[$this->key()]); |
|
| 64 | 7 | } |
|
| 65 | |||
| 66 | 27 | return null; |
|
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Get the toString of the credentials as the key. |
||
| 71 | * |
||
| 72 | * @return string |
||
| 73 | */ |
||
| 74 | 47 | protected function key() |
|
| 75 | { |
||
| 76 | 47 | return (string)$this->client->getCredential(); |
|
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Cache credentials. |
||
| 81 | * |
||
| 82 | * @param array $credential |
||
| 83 | */ |
||
| 84 | 22 | protected function cache(array $credential) |
|
| 87 | 22 | } |
|
| 88 | } |
||
| 89 |