InvalidArgumentException.php
6 months ago
Item.php
6 months ago
MemoryCacheItemPool.php
6 months ago
SysVCacheItemPool.php
6 months ago
TypedItem.php
6 months ago
Item.php
175 lines
| 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 | |
| 18 | namespace AmeliaVendor\Google\Auth\Cache; |
| 19 | |
| 20 | use DateTime; |
| 21 | use DateTimeInterface; |
| 22 | use DateTimeZone; |
| 23 | use AmeliaVendor\Psr\Cache\CacheItemInterface; |
| 24 | use TypeError; |
| 25 | |
| 26 | /** |
| 27 | * A cache item. |
| 28 | * |
| 29 | * This class will be used by MemoryCacheItemPool and SysVCacheItemPool |
| 30 | * on PHP 7.4 and below. It is compatible with psr/cache 1.0 and 2.0 (PSR-6). |
| 31 | * @see TypedItem for compatiblity with psr/cache 3.0. |
| 32 | */ |
| 33 | final class Item implements CacheItemInterface |
| 34 | { |
| 35 | /** |
| 36 | * @var string |
| 37 | */ |
| 38 | private $key; |
| 39 | |
| 40 | /** |
| 41 | * @var mixed |
| 42 | */ |
| 43 | private $value; |
| 44 | |
| 45 | /** |
| 46 | * @var DateTimeInterface|null |
| 47 | */ |
| 48 | private $expiration; |
| 49 | |
| 50 | /** |
| 51 | * @var bool |
| 52 | */ |
| 53 | private $isHit = false; |
| 54 | |
| 55 | /** |
| 56 | * @param string $key |
| 57 | */ |
| 58 | public function __construct($key) |
| 59 | { |
| 60 | $this->key = $key; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * {@inheritdoc} |
| 65 | */ |
| 66 | public function getKey() |
| 67 | { |
| 68 | return $this->key; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * {@inheritdoc} |
| 73 | */ |
| 74 | public function get() |
| 75 | { |
| 76 | return $this->isHit() ? $this->value : null; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * {@inheritdoc} |
| 81 | */ |
| 82 | public function isHit() |
| 83 | { |
| 84 | if (!$this->isHit) { |
| 85 | return false; |
| 86 | } |
| 87 | |
| 88 | if ($this->expiration === null) { |
| 89 | return true; |
| 90 | } |
| 91 | |
| 92 | return $this->currentTime()->getTimestamp() < $this->expiration->getTimestamp(); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * {@inheritdoc} |
| 97 | */ |
| 98 | public function set($value) |
| 99 | { |
| 100 | $this->isHit = true; |
| 101 | $this->value = $value; |
| 102 | |
| 103 | return $this; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * {@inheritdoc} |
| 108 | */ |
| 109 | public function expiresAt($expiration) |
| 110 | { |
| 111 | if ($this->isValidExpiration($expiration)) { |
| 112 | $this->expiration = $expiration; |
| 113 | |
| 114 | return $this; |
| 115 | } |
| 116 | |
| 117 | $error = sprintf( |
| 118 | 'Argument 1 passed to %s::expiresAt() must implement interface DateTimeInterface, %s given', |
| 119 | get_class($this), |
| 120 | gettype($expiration) |
| 121 | ); |
| 122 | |
| 123 | throw new TypeError($error); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * {@inheritdoc} |
| 128 | */ |
| 129 | public function expiresAfter($time) |
| 130 | { |
| 131 | if (is_int($time)) { |
| 132 | $this->expiration = $this->currentTime()->add(new \DateInterval("PT{$time}S")); |
| 133 | } elseif ($time instanceof \DateInterval) { |
| 134 | $this->expiration = $this->currentTime()->add($time); |
| 135 | } elseif ($time === null) { |
| 136 | $this->expiration = $time; |
| 137 | } else { |
| 138 | $message = 'Argument 1 passed to %s::expiresAfter() must be an ' . |
| 139 | 'instance of DateInterval or of the type integer, %s given'; |
| 140 | $error = sprintf($message, get_class($this), gettype($time)); |
| 141 | |
| 142 | throw new TypeError($error); |
| 143 | } |
| 144 | |
| 145 | return $this; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Determines if an expiration is valid based on the rules defined by PSR6. |
| 150 | * |
| 151 | * @param mixed $expiration |
| 152 | * @return bool |
| 153 | */ |
| 154 | private function isValidExpiration($expiration) |
| 155 | { |
| 156 | if ($expiration === null) { |
| 157 | return true; |
| 158 | } |
| 159 | |
| 160 | if ($expiration instanceof DateTimeInterface) { |
| 161 | return true; |
| 162 | } |
| 163 | |
| 164 | return false; |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * @return DateTime |
| 169 | */ |
| 170 | protected function currentTime() |
| 171 | { |
| 172 | return new DateTime('now', new DateTimeZone('UTC')); |
| 173 | } |
| 174 | } |
| 175 |