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