Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
89.06% |
57 / 64 |
|
70.59% |
12 / 17 |
CRAP | |
0.00% |
0 / 1 |
| GcpTokenCacheItemPool | |
89.06% |
57 / 64 |
|
70.59% |
12 / 17 |
26.88 | |
0.00% |
0 / 1 |
| getItem | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| getItems | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 | |||
| hasItem | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| clear | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| deleteItem | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| deleteItems | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| save | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
2.03 | |||
| saveDeferred | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
2.03 | |||
| commit | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
2.06 | |||
| log | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
1 | |||
| isWritingDisabled | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| getCache | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| createCache | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| unSerializeIfNecessary | |
40.00% |
2 / 5 |
|
0.00% |
0 / 1 |
4.94 | |||
| serializeToPersist | |
50.00% |
1 / 2 |
|
0.00% |
0 / 1 |
2.50 | |||
| getCacheKey | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getCacheFactory | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2020 (original work) Open Assessment Technologies SA |
| 19 | */ |
| 20 | |
| 21 | declare(strict_types=1); |
| 22 | |
| 23 | namespace oat\oatbox\cache; |
| 24 | |
| 25 | use oat\oatbox\cache\factory\CacheItemPoolFactory; |
| 26 | use oat\oatbox\service\ConfigurableService; |
| 27 | use Psr\Cache\CacheItemInterface; |
| 28 | use Psr\Cache\CacheItemPoolInterface; |
| 29 | |
| 30 | class GcpTokenCacheItemPool extends ConfigurableService implements CacheItemPoolInterface |
| 31 | { |
| 32 | public const SERVICE_ID = 'generis/GcpTokenCacheItemPool'; |
| 33 | |
| 34 | public const OPTION_PERSISTENCE = 'persistence'; |
| 35 | public const OPTION_DISABLE_WRITE = 'disableWrite'; |
| 36 | public const OPTION_TOKEN_CACHE_KEY = 'tokenCacheKey'; |
| 37 | |
| 38 | /** @var CacheItemPoolInterface */ |
| 39 | private $cache; |
| 40 | |
| 41 | /** @var bool */ |
| 42 | private $isWritingDisabled; |
| 43 | |
| 44 | /** |
| 45 | * {@inheritdoc} |
| 46 | */ |
| 47 | public function getItem($key) |
| 48 | { |
| 49 | $key = $this->getCacheKey(); |
| 50 | |
| 51 | $this->log(__METHOD__, [$key]); |
| 52 | |
| 53 | $item = $this->getCache()->getItem($key); |
| 54 | |
| 55 | $this->unSerializeIfNecessary($item); |
| 56 | |
| 57 | return $item; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * {@inheritdoc} |
| 62 | */ |
| 63 | public function getItems(array $keys = []) |
| 64 | { |
| 65 | $keys = [$this->getCacheKey()]; |
| 66 | |
| 67 | $this->log(__METHOD__, $keys); |
| 68 | |
| 69 | $items = $this->getCache()->getItems($keys); |
| 70 | |
| 71 | foreach ($items as $item) { |
| 72 | $this->unSerializeIfNecessary($item); |
| 73 | } |
| 74 | |
| 75 | return $items; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * {@inheritdoc} |
| 80 | */ |
| 81 | public function hasItem($key) |
| 82 | { |
| 83 | $key = $this->getCacheKey(); |
| 84 | |
| 85 | $this->log(__METHOD__, [$key]); |
| 86 | |
| 87 | return $this->getCache()->hasItem($key); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * {@inheritdoc} |
| 92 | */ |
| 93 | public function clear() |
| 94 | { |
| 95 | $this->log(__METHOD__); |
| 96 | |
| 97 | return $this->getCache()->clear(); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * {@inheritdoc} |
| 102 | */ |
| 103 | public function deleteItem($key) |
| 104 | { |
| 105 | $key = $this->getCacheKey(); |
| 106 | |
| 107 | $this->log(__METHOD__, [$key]); |
| 108 | |
| 109 | return $this->getCache()->deleteItem($key); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * {@inheritdoc} |
| 114 | */ |
| 115 | public function deleteItems(array $keys) |
| 116 | { |
| 117 | $keys = [$this->getCacheKey()]; |
| 118 | |
| 119 | $this->log(__METHOD__, $keys); |
| 120 | |
| 121 | return $this->getCache()->deleteItems($keys); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * {@inheritdoc} |
| 126 | */ |
| 127 | public function save(CacheItemInterface $item) |
| 128 | { |
| 129 | $this->log(__METHOD__); |
| 130 | |
| 131 | if ($this->isWritingDisabled()) { |
| 132 | return true; |
| 133 | } |
| 134 | |
| 135 | $this->serializeToPersist($item); |
| 136 | |
| 137 | return $this->getCache()->save($item); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * {@inheritdoc} |
| 142 | */ |
| 143 | public function saveDeferred(CacheItemInterface $item) |
| 144 | { |
| 145 | $this->log(__METHOD__); |
| 146 | |
| 147 | if ($this->isWritingDisabled()) { |
| 148 | return true; |
| 149 | } |
| 150 | |
| 151 | $this->serializeToPersist($item); |
| 152 | |
| 153 | return $this->getCache()->saveDeferred($item); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * {@inheritdoc} |
| 158 | */ |
| 159 | public function commit() |
| 160 | { |
| 161 | $this->log(__METHOD__); |
| 162 | |
| 163 | if ($this->isWritingDisabled()) { |
| 164 | return true; |
| 165 | } |
| 166 | |
| 167 | return $this->getCache()->commit(); |
| 168 | } |
| 169 | |
| 170 | private function log(string $method, array $keys = []): void |
| 171 | { |
| 172 | $this->getLogger()->debug( |
| 173 | sprintf( |
| 174 | '[%s] Called method %s with key(s) %s', |
| 175 | __CLASS__, |
| 176 | $method, |
| 177 | implode(',', $keys) |
| 178 | ) |
| 179 | ); |
| 180 | } |
| 181 | |
| 182 | private function isWritingDisabled(): bool |
| 183 | { |
| 184 | if ($this->isWritingDisabled === null) { |
| 185 | $this->isWritingDisabled = (bool)$this->getOption(self::OPTION_DISABLE_WRITE, false); |
| 186 | } |
| 187 | |
| 188 | return $this->isWritingDisabled; |
| 189 | } |
| 190 | |
| 191 | private function getCache(): CacheItemPoolInterface |
| 192 | { |
| 193 | if ($this->cache === null) { |
| 194 | $this->cache = $this->createCache(); |
| 195 | } |
| 196 | |
| 197 | return $this->cache; |
| 198 | } |
| 199 | |
| 200 | private function createCache(): CacheItemPoolInterface |
| 201 | { |
| 202 | return $this->getCacheFactory()->create( |
| 203 | [ |
| 204 | CacheItemPoolFactory::CONFIG_PERSISTENCE => $this->getOption(self::OPTION_PERSISTENCE, 'default_kv'), |
| 205 | ] |
| 206 | ); |
| 207 | } |
| 208 | |
| 209 | private function unSerializeIfNecessary(CacheItemInterface $item): void |
| 210 | { |
| 211 | if (empty($item->get())) { |
| 212 | return; |
| 213 | } |
| 214 | |
| 215 | $unSerialized = @unserialize($item->get()); |
| 216 | |
| 217 | if ($unSerialized !== false) { |
| 218 | $item->set($unSerialized); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | private function serializeToPersist(CacheItemInterface $item): void |
| 223 | { |
| 224 | if (is_array($item->get())) { |
| 225 | $item->set(serialize($item->get())); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | private function getCacheKey(): string |
| 230 | { |
| 231 | return (string)$this->getOption(self::OPTION_TOKEN_CACHE_KEY, 'GCP-TOKEN-SANCTUARY:GOOGLE_AUTH_PHP_GCE'); |
| 232 | } |
| 233 | |
| 234 | private function getCacheFactory(): CacheItemPoolFactory |
| 235 | { |
| 236 | return $this->getServiceLocator()->get(CacheItemPoolFactory::class); |
| 237 | } |
| 238 | } |