Passed
Pull Request — master (#1)
by Peter
07:07
created

CombinedAdapter::loadCachedPolicy()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 10
nc 6
nop 1
dl 0
loc 21
rs 8.8333
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Framework\Authorization;
6
7
use Casbin\Model\Model;
8
use Casbin\Persist\Adapter as CasbinAdapter;
9
10
class CombinedAdapter implements CasbinAdapter
11
{
12
    /** @var CasbinAdapter */
13
    protected $defaultAdapter;
14
15
    /** @var CacheManager|null */
16
    protected $cacheManager;
17
18
    /** @var CasbinAdapter[] */
19
    protected $registeredAdapters = [];
20
21
    /**
22
     * PolicyAdapter constructor.
23
     *
24
     * @param CasbinAdapter     $defaultAdapter
25
     * @param CacheManager|null $cacheManager
26
     */
27
    public function __construct(CasbinAdapter $defaultAdapter, ?CacheManager $cacheManager = null)
28
    {
29
        $this->defaultAdapter = $defaultAdapter;
30
        $this->cacheManager   = $cacheManager;
31
    }
32
33
    /**
34
     * @param CasbinAdapter $adapter
35
     *
36
     * @return $this
37
     */
38
    public function registerAdapter(CasbinAdapter $adapter)
39
    {
40
        $this->registeredAdapters[] = $adapter;
41
42
        return $this;
43
    }
44
45
    /**
46
     * @param Model $model
47
     */
48
    public function loadPolicy($model)
49
    {
50
        if ($this->loadCachedPolicy($model)) {
51
            return;
52
        }
53
54
        if ($this->loadAdapterPolicies($model)) {
55
            $this->storeLoadedPolicies($model);
56
        }
57
    }
58
59
    /**
60
     * @param Model $model
61
     *
62
     * @return bool
63
     */
64
    protected function loadCachedPolicy($model): bool
65
    {
66
        if (!$this->cacheManager) {
67
            return false;
68
        }
69
70
        $cachedData = $this->cacheManager->getAll();
71
72
        if (!$cachedData || !array_key_exists('g', $cachedData) || !array_key_exists('p', $cachedData)) {
73
            return false;
74
        }
75
76
        foreach ($cachedData['g'] as $policy) {
77
            $model->model['g']['g']->policy[] = $policy;
78
        }
79
80
        foreach ($cachedData['p'] as $policy) {
81
            $model->model['p']['p']->policy[] = $policy;
82
        }
83
84
        return true;
85
    }
86
87
    /**
88
     * @param Model $model
89
     *
90
     * @return bool
91
     */
92
    protected function storeLoadedPolicies($model): bool
93
    {
94
        if (!$this->cacheManager) {
95
            return false;
96
        }
97
98
        $data = [
99
            'g' => $model->model['g']['g']->policy,
100
            'p' => $model->model['p']['p']->policy,
101
        ];
102
103
        return $this->cacheManager->storeAll($data);
104
    }
105
106
    /**
107
     * @param Model $model
108
     *
109
     * @return bool
110
     */
111
    protected function loadAdapterPolicies($model): bool
112
    {
113
        $this->defaultAdapter->loadPolicy($model);
114
115
        foreach ($this->registeredAdapters as $adapter) {
116
            $adapter->loadPolicy($model);
117
        }
118
119
        return true;
120
    }
121
122
    /**
123
     * @param Model $model
124
     *
125
     * @return mixed
126
     */
127
    public function savePolicy($model)
128
    {
129
        return $this->defaultAdapter->savePolicy($model);
130
    }
131
132
    /**
133
     * @param string $sec
134
     * @param string $ptype
135
     * @param string $rule
136
     *
137
     * @return mixed
138
     */
139
    public function addPolicy($sec, $ptype, $rule)
140
    {
141
        return $this->defaultAdapter->addPolicy($sec, $ptype, $rule);
142
    }
143
144
    /**
145
     * @param string $sec
146
     * @param string $ptype
147
     * @param string $rule
148
     *
149
     * @return mixed
150
     */
151
    public function removePolicy($sec, $ptype, $rule)
152
    {
153
        return $this->defaultAdapter->removePolicy($sec, $ptype, $rule);
154
    }
155
156
    /**
157
     * @param string $sec
158
     * @param string $ptype
159
     * @param string $fieldIndex
160
     * @param string ...$fieldValues
161
     *
162
     * @return mixed
163
     */
164
    public function removeFilteredPolicy($sec, $ptype, $fieldIndex, ...$fieldValues)
165
    {
166
        $args = array_merge([$sec, $ptype, $fieldIndex], $fieldValues);
167
168
        return call_user_func_array([$this->defaultAdapter, 'removeFilteredPolicy'], $args);
169
    }
170
}
171