FileAdapter::updatePolicy()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 4
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
declare(strict_types=1);
4
5
namespace Casbin\Persist\Adapters;
6
7
use Casbin\Exceptions\CasbinException;
8
use Casbin\Exceptions\InvalidFilePathException;
9
use Casbin\Exceptions\NotImplementedException;
10
use Casbin\Model\Model;
11
use Casbin\Persist\Adapter;
12
use Casbin\Persist\AdapterHelper;
13
use Casbin\Persist\BatchAdapter;
14
use Casbin\Persist\UpdatableAdapter;
15
use Casbin\Util\Util;
16
17
/**
18
 * Class FileAdapter
19
 * The file adapter for Casbin.
20
 * It can load policy from file or save policy to file.
21
 *
22
 * @author [email protected]
0 ignored issues
show
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
23
 */
0 ignored issues
show
Coding Style introduced by
Missing @link tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
24
class FileAdapter implements Adapter, BatchAdapter, UpdatableAdapter
25
{
26
    use AdapterHelper;
27
28
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
29
     * @var string
30
     */
31
    protected $filePath;
32
33
    /**
34
     * FileAdapter constructor.
35
     *
36
     * @param string $filePath
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
37
     */
38 336
    public function __construct(string $filePath)
39
    {
40 336
        $this->filePath = $filePath;
41 224
    }
42
43
    /**
44
     * Loads all policy rules from the storage.
45
     *
46
     * @param Model $model
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
47
     *
48
     * @throws CasbinException
49
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
50 333
    public function loadPolicy(Model $model): void
51
    {
52 333
        if (!file_exists($this->filePath)) {
53 27
            throw new InvalidFilePathException('invalid file path, file path cannot be empty');
54
        }
55
56 312
        $this->loadPolicyFile($model);
57 208
    }
58
59
    /**
60
     * Saves all policy rules to the storage.
61
     *
62
     * @param Model $model
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
63
     *
64
     * @throws CasbinException
65
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
66 6
    public function savePolicy(Model $model): void
67
    {
68 6
        if ('' == $this->filePath) {
69
            throw new InvalidFilePathException('invalid file path, file path cannot be empty');
70
        }
71
72 6
        $writeString = '';
73
74 6
        if (isset($model['p'])) {
75 6
            foreach ($model['p'] as $ptype => $ast) {
76 6
                foreach ($ast->policy as $rule) {
77 6
                    $writeString .= $ptype . ', ';
78 6
                    $writeString .= Util::arrayToString($rule);
79 6
                    $writeString .= PHP_EOL;
80
                }
81
            }
82
        }
83
84 6
        if (isset($model['g'])) {
85 3
            foreach ($model['g'] as $ptype => $ast) {
86 3
                foreach ($ast->policy as $rule) {
87 3
                    $writeString .= $ptype . ', ';
88 3
                    $writeString .= Util::arrayToString($rule);
89 3
                    $writeString .= PHP_EOL;
90
                }
91
            }
92
        }
93
94 6
        $this->savePolicyFile(rtrim($writeString, PHP_EOL));
95 4
    }
96
97
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
98
     * @param Model $model
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
99
     * @throws InvalidFilePathException
0 ignored issues
show
Coding Style introduced by
Tag @throws cannot be grouped with parameter tags in a doc comment
Loading history...
100
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
101 312
    protected function loadPolicyFile(Model $model): void
102
    {
103 312
        $file = fopen($this->filePath, 'rb');
104
105 312
        if (false === $file) {
106
            throw new InvalidFilePathException(sprintf('Unable to access to the specified path "%s"', $this->filePath));
107
        }
108
109 312
        while ($line = fgets($file)) {
110 312
            $this->loadPolicyLine(trim($line), $model);
111
        }
112 312
        fclose($file);
113 208
    }
114
115
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
116
     * @param string $text
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
117
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
118 6
    protected function savePolicyFile(string $text): void
119
    {
120 6
        file_put_contents($this->filePath, $text, LOCK_EX);
121 4
    }
122
123
    /**
124
     * Adds a policy rule to the storage.
125
     *
126
     * @param string $sec
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
127
     * @param string $ptype
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
128
     * @param string[] $rule
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
129
     *
130
     * @throws NotImplementedException
131
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
132 36
    public function addPolicy(string $sec, string $ptype, array $rule): void
133
    {
134 36
        throw new NotImplementedException('not implemented');
135
    }
136
137
    /**
138
     * Adds a policy rule to the storage.
139
     *
140
     * @param string $sec
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
141
     * @param string $ptype
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
142
     * @param string[][] $rules
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
143
     *
144
     * @throws NotImplementedException
145
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
146 12
    public function addPolicies(string $sec, string $ptype, array $rules): void
147
    {
148 12
        throw new NotImplementedException('not implemented');
149
    }
150
151
    /**
152
     * Removes a policy rule from the storage.
153
     *
154
     * @param string $sec
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
155
     * @param string $ptype
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
156
     * @param string[] $rule
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
157
     *
158
     * @throws NotImplementedException
159
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
160 33
    public function removePolicy(string $sec, string $ptype, array $rule): void
161
    {
162 33
        throw new NotImplementedException('not implemented');
163
    }
164
165
    /**
166
     * Removes a policy rules from the storage.
167
     *
168
     * @param string $sec
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
169
     * @param string $ptype
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
170
     * @param string[][] $rules
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
171
     *
172
     * @throws NotImplementedException
173
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
174 15
    public function removePolicies(string $sec, string $ptype, array $rules): void
175
    {
176 15
        throw new NotImplementedException('not implemented');
177
    }
178
179
    /**
180
     * Removes policy rules that match the filter from the storage.
181
     *
182
     * @param string $sec
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
183
     * @param string $ptype
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
184
     * @param int $fieldIndex
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
185
     * @param string ...$fieldValues
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
186
     *
187
     * @throws NotImplementedException
188
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
189 24
    public function removeFilteredPolicy(string $sec, string $ptype, int $fieldIndex, string ...$fieldValues): void
190
    {
191 24
        throw new NotImplementedException('not implemented');
192
    }
193
194
    /**
195
     * Updates a policy rule from storage.
196
     * This is part of the Auto-Save feature.
197
     *
198
     * @param string $sec
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
199
     * @param string $ptype
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
200
     * @param string[] $oldRule
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
201
     * @param string[] $newPolicy
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
202
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
203 3
    public function updatePolicy(string $sec, string $ptype, array $oldRule, array $newPolicy): void
204
    {
205 3
        throw new NotImplementedException('not implemented');
206
    }
207
208
    /**
209
     * UpdatePolicies updates some policy rules to storage, like db, redis.
210
     *
211
     * @param string $sec
0 ignored issues
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
Coding Style introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
212
     * @param string $ptype
0 ignored issues
show
Coding Style introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
213
     * @param string[][] $oldRules
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
214
     * @param string[][] $newRules
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
215
     * @return void
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
216
     */
217 3
    public function updatePolicies(string $sec, string $ptype, array $oldRules, array $newRules): void
218
    {
219 3
        throw new NotImplementedException('not implemented');
220
    }
221
222
    /**
223
     * UpdateFilteredPolicies deletes old rules and adds new rules.
224
     *
225
     * @param string $sec
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
226
     * @param string $ptype
0 ignored issues
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
227
     * @param array $newPolicies
0 ignored issues
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
228
     * @param integer $fieldIndex
0 ignored issues
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
229
     * @param string ...$fieldValues
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
230
     * @return array
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
231
     */
232
    public function updateFilteredPolicies(string $sec, string $ptype, array $newPolicies, int $fieldIndex, string ...$fieldValues): array
233
    {
234
        throw new NotImplementedException('not implemented');
235
    }
236
}
237