CacheManager::storeAll()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 7
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Framework\Authorization;
6
7
use Opulence\Cache\ICacheBridge;
8
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
61
    {
62
        $this->cacheBridge->delete(static::CACHE_KEY);
63
64
        return 1;
65
    }
66
}
67