| Total Complexity | 7 |
| Total Lines | 56 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 9 | class CacheManager |
||
| 10 | { |
||
| 11 | protected const CACHE_KEY = 'casbin_auth_collection'; |
||
| 12 | |||
| 13 | protected ICacheBridge $cacheBridge; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * Cache constructor. |
||
| 17 | * |
||
| 18 | * @param ICacheBridge $cacheBridge |
||
| 19 | */ |
||
| 20 | public function __construct(ICacheBridge $cacheBridge) |
||
| 21 | { |
||
| 22 | $this->cacheBridge = $cacheBridge; |
||
| 23 | } |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @param array<string,string[][]> $data |
||
| 27 | * |
||
| 28 | * @return bool |
||
| 29 | */ |
||
| 30 | public function storeAll(array $data): bool |
||
| 31 | { |
||
| 32 | $payload = json_encode($data); |
||
| 33 | |||
| 34 | $this->cacheBridge->set(static::CACHE_KEY, $payload, PHP_INT_MAX); |
||
| 35 | |||
| 36 | return $this->cacheBridge->has(static::CACHE_KEY); |
||
| 37 | } |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @return array|null |
||
| 41 | */ |
||
| 42 | public function getAll(): ?array |
||
| 43 | { |
||
| 44 | try { |
||
| 45 | $payload = $this->cacheBridge->get(static::CACHE_KEY); |
||
| 46 | } catch (\Exception $e) { |
||
| 47 | return null; |
||
| 48 | } |
||
| 49 | |||
| 50 | if (!is_string($payload) || $payload === '') { |
||
| 51 | return null; |
||
| 52 | } |
||
| 53 | |||
| 54 | return (array)json_decode($payload, true); |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @return int number of keys deleted |
||
| 59 | */ |
||
| 60 | public function clearAll(): int |
||
| 65 | } |
||
| 66 | } |
||
| 67 |