Completed
Push — master ( 127f02...ecc5eb )
by Lee
06:27 queued 02:34
created

InternalApi::ShouldPersist()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 2
nc 2
nop 0
crap 2
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;
6
7
use Casbin\Exceptions\NotImplementedException;
8
use Casbin\Persist\BatchAdapter;
9
10
/**
11
 * Trait InternalApi.
12
 *
13
 * @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...
14
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
15
trait InternalApi
16
{
17
    /**
18
     * adds a rule to the current policy.
0 ignored issues
show
Coding Style introduced by
Doc comment short description must start with a capital letter
Loading history...
19
     *
20
     * @param string $sec
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
21
     * @param string $ptype
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
22
     * @param array  $rule
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
23
     *
24
     * @return bool
25
     */
26 27
    protected function addPolicyInternal(string $sec, string $ptype, array $rule): bool
27
    {
28 27
        if ($this->model->hasPolicy($sec, $ptype, $rule)) {
29 3
            return false;
30
        }
31
32 27
        $this->model->addPolicy($sec, $ptype, $rule);
33
34 27
        if ($this->ShouldPersist()) {
35
            try {
36 24
                $this->adapter->addPolicy($sec, $ptype, $rule);
37 24
            } catch (NotImplementedException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
38
            }
39
        }
40 27
        $this->checkWatcher();
41
42 27
        return true;
43
    }
44
45
    /**
46
     * adds a rules to the current policy.
0 ignored issues
show
Coding Style introduced by
Doc comment short description must start with a capital letter
Loading history...
47
     *
48
     * @param string $sec
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
49
     * @param string $ptype
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
50
     * @param array  $rules
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
51
     *
52
     * @return bool
53
     */
54 6
    protected function addPoliciesInternal(string $sec, string $ptype, array $rules): bool
55
    {
56 6
        if ($this->model->hasPolicies($sec, $ptype, $rules)) {
57 6
            return false;
58
        }
59
60 6
        $this->model->addPolicies($sec, $ptype, $rules);
61
62 6
        if ($this->ShouldPersist() && $this->adapter instanceof BatchAdapter) {
63
            try {
64 6
                $this->adapter->addPolicies($sec, $ptype, $rules);
65 6
            } catch (NotImplementedException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
66
            }
67
        }
68 6
        $this->checkWatcher();
69
70 6
        return true;
71
    }
72
73
    /**
74
     * removes a rule from the current policy.
0 ignored issues
show
Coding Style introduced by
Doc comment short description must start with a capital letter
Loading history...
75
     *
76
     * @param string $sec
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
77
     * @param string $ptype
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
78
     * @param array  $rule
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
79
     *
80
     * @return bool
81
     */
82 27
    protected function removePolicyInternal(string $sec, string $ptype, array $rule): bool
83
    {
84 27
        $ruleRemoved = $this->model->removePolicy($sec, $ptype, $rule);
85 27
        if (!$ruleRemoved) {
86 3
            return $ruleRemoved;
87
        }
88
89 27
        if ($this->ShouldPersist()) {
90
            try {
91 27
                $this->adapter->removePolicy($sec, $ptype, $rule);
92 27
            } catch (NotImplementedException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
93
            }
94
        }
95 27
        $this->checkWatcher();
96
97 27
        return $ruleRemoved;
98
    }
99
100
    /**
101
     * removes a rules from the current policy.
0 ignored issues
show
Coding Style introduced by
Doc comment short description must start with a capital letter
Loading history...
102
     *
103
     * @param string $sec
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
104
     * @param string $ptype
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
105
     * @param array  $rules
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
106
     *
107
     * @return bool
108
     */
109 6
    protected function removePoliciesInternal(string $sec, string $ptype, array $rules): bool
110
    {
111 6
        $ruleRemoved = $this->model->removePolicies($sec, $ptype, $rules);
112 6
        if (!$ruleRemoved) {
113 6
            return $ruleRemoved;
114
        }
115
116 6
        if ($this->ShouldPersist() && $this->adapter instanceof BatchAdapter) {
117
            try {
118 6
                $this->adapter->removePolicies($sec, $ptype, $rules);
119 6
            } catch (NotImplementedException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
120
            }
121
        }
122 6
        $this->checkWatcher();
123
124 6
        return $ruleRemoved;
125
    }
126
127
    /**
128
     * removes rules based on field filters from the current policy.
0 ignored issues
show
Coding Style introduced by
Doc comment short description must start with a capital letter
Loading history...
129
     *
130
     * @param string $sec
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
131
     * @param string $ptype
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
132
     * @param int    $fieldIndex
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
133
     * @param string ...$fieldValues
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
134
     *
135
     * @return bool
136
     */
137 24
    protected function removeFilteredPolicyInternal(string $sec, string $ptype, int $fieldIndex, string ...$fieldValues): bool
138
    {
139 24
        $ruleRemoved = $this->model->removeFilteredPolicy($sec, $ptype, $fieldIndex, ...$fieldValues);
140 24
        if (!$ruleRemoved) {
141
            return $ruleRemoved;
142
        }
143
144 24
        if ($this->ShouldPersist()) {
145
            try {
146 24
                $this->adapter->removeFilteredPolicy($sec, $ptype, $fieldIndex, ...$fieldValues);
147 24
            } catch (NotImplementedException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
148
            }
149
        }
150 24
        $this->checkWatcher();
151
152 24
        return $ruleRemoved;
153
    }
154
155
    /**
156
     * check $this->watcher ans $this->autoNotifyWatcher.
0 ignored issues
show
Coding Style introduced by
Doc comment short description must start with a capital letter
Loading history...
157
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
158 51
    private function checkWatcher(): void
0 ignored issues
show
Coding Style introduced by
Private method name "InternalApi::checkWatcher" must be prefixed with an underscore
Loading history...
159
    {
160 51
        if (!is_null($this->watcher) && $this->autoNotifyWatcher) {
161
            // error intentionally ignored
162
            $this->watcher->update();
163
        }
164 51
    }
165
166 51
    private function ShouldPersist(): bool
0 ignored issues
show
Coding Style introduced by
Private method name "InternalApi::ShouldPersist" must be prefixed with an underscore
Loading history...
Coding Style introduced by
Private method name "InternalApi::ShouldPersist" is not in camel caps format
Loading history...
Coding Style introduced by
Missing doc comment for function ShouldPersist()
Loading history...
167
    {
168 51
        return !is_null($this->adapter) && $this->autoNotifyWatcher;
169
    }
170
}
171