SqlDriverTrait::assertPrepare()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
1
<?php
2
/**
3
 * This file is part of the Shieldon package.
4
 *
5
 * (c) Terry L. <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * php version 7.1.0
11
 *
12
 * @category  Web-security
13
 * @package   Shieldon
14
 * @author    Terry Lin <[email protected]>
15
 * @copyright 2019 terrylinooo
16
 * @license   https://github.com/terrylinooo/shieldon/blob/2.x/LICENSE MIT
17
 * @link      https://github.com/terrylinooo/shieldon
18
 * @see       https://shieldon.io
19
 */
20
21
declare(strict_types=1);
22
23
namespace Shieldon\Firewall\Driver;
24
25
use RuntimeException;
26
27
use function is_array;
28
use function is_bool;
29
use function json_decode;
30
31
/**
32
 * SQL Driver Trait
33
 */
34
trait SqlDriverTrait
35
{
36
    /**
37
     * Fetch data from filter table.
38
     *
39
     * @param string $ip An IP address.
40
     *
41
     * @return array
42
     */
43 24
    protected function doFetchFromFilterTable(string $ip): array
44
    {
45 24
        $results = [];
46
47 24
        $sql = 'SELECT log_ip, log_data FROM ' . $this->tableFilterLogs . '
48
            WHERE log_ip = :log_ip
49 24
            LIMIT 1';
50
51 24
        $query = $this->db->prepare($sql);
52
53 24
        $this->assertPrepare($query);
54
55 24
        $query->bindValue(':log_ip', $ip, $this->db::PARAM_STR);
56 24
        $query->execute();
57 24
        $resultData = $query->fetch($this->db::FETCH_ASSOC);
58
59
        // No data found.
60 24
        if (is_bool($resultData) && !$resultData) {
61 24
            $resultData = [];
62
        }
63
64 24
        if (!empty($resultData['log_data'])) {
65 15
            $results = json_decode($resultData['log_data'], true);
66
        }
67
68 24
        return $results;
69
    }
70
71
    /**
72
     * Fetch data from rule table.
73
     *
74
     * @param string $ip An IP address.
75
     *
76
     * @return array
77
     */
78 32
    protected function doFetchFromRuleTable(string $ip): array
79
    {
80 32
        $results = [];
81
82 32
        $sql = 'SELECT * FROM ' . $this->tableRuleList . '
83
            WHERE log_ip = :log_ip
84 32
            LIMIT 1';
85
86 32
        $query = $this->db->prepare($sql);
87
88 32
        $this->assertPrepare($query);
89
        
90 32
        $query->bindValue(':log_ip', $ip, $this->db::PARAM_STR);
91 32
        $query->execute();
92 32
        $resultData = $query->fetch($this->db::FETCH_ASSOC);
93
94
        // No data found.
95 32
        if (is_bool($resultData) && !$resultData) {
96 32
            $resultData = [];
97
        }
98
99 32
        if (is_array($resultData)) {
100 32
            $results = $resultData;
101
        }
102
103 32
        return $results;
104
    }
105
106
    /**
107
     * Fetch data from session table.
108
     *
109
     * @param string $id A session ID.
110
     *
111
     * @return array
112
     */
113 1
    protected function doFetchFromSessionTable(string $id): array
114
    {
115 1
        $results = [];
116
117 1
        $sql = 'SELECT * FROM ' . $this->tableSessions . '
118
            WHERE id = :id
119 1
            LIMIT 1';
120
121
      
122 1
        $query = $this->db->prepare($sql);
123
124 1
        $this->assertPrepare($query);
125
126 1
        $query->bindValue(':id', $id, $this->db::PARAM_STR);
127 1
        $query->execute();
128 1
        $resultData = $query->fetch($this->db::FETCH_ASSOC);
129
130
        // No data found.
131 1
        if (is_bool($resultData) && !$resultData) {
132 1
            $resultData = [];
133
        }
134
135 1
        if (is_array($resultData)) {
136 1
            $results = $resultData;
137
        }
138
139 1
        return $results;
140
    }
141
142
    /**
143
     * Fetch all data from filter table.
144
     *
145
     * @return array
146
     */
147 1
    protected function doFetchAllFromFilterTable(): array
148
    {
149 1
        $results = [];
150
151 1
        $sql = 'SELECT log_ip, log_data FROM ' . $this->tableFilterLogs;
152
153 1
        $query = $this->db->prepare($sql);
154
155 1
        $this->assertPrepare($query);
156
157 1
        $query->execute();
158 1
        $resultData = $query->fetchAll($this->db::FETCH_ASSOC);
159
160 1
        if (is_array($resultData)) {
161 1
            $results = $resultData;
162
        }
163
164 1
        return $results;
165
    }
166
167
    /**
168
     * Fetch all data from filter table.
169
     *
170
     * @return array
171
     */
172 1
    protected function doFetchAllFromRuleTable(): array
173
    {
174 1
        $results = [];
175
176 1
        $sql = 'SELECT * FROM ' . $this->tableRuleList;
177
178 1
        $query = $this->db->prepare($sql);
179
180 1
        $this->assertPrepare($query);
181
182 1
        $query->execute();
183 1
        $resultData = $query->fetchAll($this->db::FETCH_ASSOC);
184
185 1
        if (is_array($resultData)) {
186 1
            $results = $resultData;
187
        }
188
189 1
        return $results;
190
    }
191
192
    /**
193
     * Fetch all data from session table.
194
     *
195
     * @return array
196
     */
197 10
    protected function doFetchAllFromSessionTable(): array
198
    {
199 10
        $results = [];
200
201 10
        $sql = 'SELECT * FROM ' . $this->tableSessions . ' ORDER BY microtimestamp ASC';
202
203 10
        $query = $this->db->prepare($sql);
204
205 10
        $this->assertPrepare($query);
206
207 10
        $query->execute();
208 10
        $resultData = $query->fetchAll($this->db::FETCH_ASSOC);
209
210 10
        if (is_array($resultData)) {
211 10
            $results = $resultData;
212
        }
213
214 10
        return $results;
215
    }
216
217
    /**
218
     * Check the prepare statement status.
219
     *
220
     * @param object|bool $status Return false if failed.
221
     *
222
     * @return void
223
     */
224 35
    protected function assertPrepare($status): void
225
    {
226 35
        if (!$status) {
227 1
            throw new RuntimeException(
228 1
                json_encode($this->db->errorInfo())
229 1
            );
230
        }
231
    }
232
}
233