/lib/vendor/google/auth/src/Cache/Item.php

https://gitlab.com/efabian/maya · PHP · 190 lines · 95 code · 29 blank · 66 comment · 9 complexity · 2862106512ea1760c819f1d8c983b6e8 MD5 · raw file

  1. <?php
  2. /*
  3. * Copyright 2016 Google Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. namespace Google\Auth\Cache;
  18. use Psr\Cache\CacheItemInterface;
  19. /**
  20. * A cache item.
  21. */
  22. final class Item implements CacheItemInterface
  23. {
  24. /**
  25. * @var string
  26. */
  27. private $key;
  28. /**
  29. * @var mixed
  30. */
  31. private $value;
  32. /**
  33. * @var \DateTime|null
  34. */
  35. private $expiration;
  36. /**
  37. * @var bool
  38. */
  39. private $isHit = false;
  40. /**
  41. * @param string $key
  42. */
  43. public function __construct($key)
  44. {
  45. $this->key = $key;
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function getKey()
  51. {
  52. return $this->key;
  53. }
  54. /**
  55. * {@inheritdoc}
  56. */
  57. public function get()
  58. {
  59. return $this->isHit() ? $this->value : null;
  60. }
  61. /**
  62. * {@inheritdoc}
  63. */
  64. public function isHit()
  65. {
  66. if (!$this->isHit) {
  67. return false;
  68. }
  69. if ($this->expiration === null) {
  70. return true;
  71. }
  72. return $this->currentTime()->getTimestamp() < $this->expiration->getTimestamp();
  73. }
  74. /**
  75. * {@inheritdoc}
  76. */
  77. public function set($value)
  78. {
  79. $this->isHit = true;
  80. $this->value = $value;
  81. return $this;
  82. }
  83. /**
  84. * {@inheritdoc}
  85. */
  86. public function expiresAt($expiration)
  87. {
  88. if ($this->isValidExpiration($expiration)) {
  89. $this->expiration = $expiration;
  90. return $this;
  91. }
  92. $implementationMessage = interface_exists('DateTimeInterface')
  93. ? 'implement interface DateTimeInterface'
  94. : 'be an instance of DateTime';
  95. $error = sprintf(
  96. 'Argument 1 passed to %s::expiresAt() must %s, %s given',
  97. get_class($this),
  98. $implementationMessage,
  99. gettype($expiration)
  100. );
  101. $this->handleError($error);
  102. }
  103. /**
  104. * {@inheritdoc}
  105. */
  106. public function expiresAfter($time)
  107. {
  108. if (is_int($time)) {
  109. $this->expiration = $this->currentTime()->add(new \DateInterval("PT{$time}S"));
  110. } elseif ($time instanceof \DateInterval) {
  111. $this->expiration = $this->currentTime()->add($time);
  112. } elseif ($time === null) {
  113. $this->expiration = $time;
  114. } else {
  115. $message = 'Argument 1 passed to %s::expiresAfter() must be an ' .
  116. 'instance of DateInterval or of the type integer, %s given';
  117. $error = sprintf($message, get_class($this), gettype($time));
  118. $this->handleError($error);
  119. }
  120. return $this;
  121. }
  122. /**
  123. * Handles an error.
  124. *
  125. * @param string $error
  126. * @throws \TypeError
  127. */
  128. private function handleError($error)
  129. {
  130. if (class_exists('TypeError')) {
  131. throw new \TypeError($error);
  132. }
  133. trigger_error($error, E_USER_ERROR);
  134. }
  135. /**
  136. * Determines if an expiration is valid based on the rules defined by PSR6.
  137. *
  138. * @param mixed $expiration
  139. * @return bool
  140. */
  141. private function isValidExpiration($expiration)
  142. {
  143. if ($expiration === null) {
  144. return true;
  145. }
  146. // We test for two types here due to the fact the DateTimeInterface
  147. // was not introduced until PHP 5.5. Checking for the DateTime type as
  148. // well allows us to support 5.4.
  149. if ($expiration instanceof \DateTimeInterface) {
  150. return true;
  151. }
  152. if ($expiration instanceof \DateTime) {
  153. return true;
  154. }
  155. return false;
  156. }
  157. protected function currentTime()
  158. {
  159. return new \DateTime('now', new \DateTimeZone('UTC'));
  160. }
  161. }