Passed
Push — 2.x ( da3964...4ff14b )
by Terry
01:53
created

SqlTrait::doFetchAllFromSessionTable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 8
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 15
rs 10
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
11
declare(strict_types=1);
12
13
namespace Shieldon\Firewall\Driver;
14
15
use function is_array;
16
use function is_bool;
17
18
/**
19
 * SQL trait
20
 */
21
trait SqlTrait
22
{
23
/**
24
     * Fetch data from filter table.
25
     *
26
     * @param string $ip An IP address.
27
     *
28
     * @return array
29
     */
30
    protected function doFetchFromFilterTable(string $ip): array
31
    {
32
        $results = [];
33
34
        $sql = 'SELECT log_ip, log_data FROM ' . $this->tableFilterLogs . '
35
            WHERE log_ip = :log_ip
36
            LIMIT 1';
37
38
        $query = $this->db->prepare($sql);
39
        $query->bindValue(':log_ip', $ip, $this->db::PARAM_STR);
40
        $query->execute();
41
        $resultData = $query->fetch($this->db::FETCH_ASSOC);
42
43
        // No data found.
44
        if (is_bool($resultData) && !$resultData) {
45
            $resultData = [];
46
        }
47
48
        if (!empty($resultData['log_data'])) {
49
            $results = json_decode($resultData['log_data'], true); 
50
        }
51
52
        return $results;
53
    }
54
55
    /**
56
     * Fetch data from rule table.
57
     *
58
     * @param string $ip An IP address.
59
     *
60
     * @return array
61
     */
62
    protected function doFetchFromRuleTable(string $ip): array
63
    {
64
        $results = [];
65
66
        $sql = 'SELECT * FROM ' . $this->tableRuleList . '
67
            WHERE log_ip = :log_ip
68
            LIMIT 1';
69
70
        $query = $this->db->prepare($sql);
71
        $query->bindValue(':log_ip', $ip, $this->db::PARAM_STR);
72
        $query->execute();
73
        $resultData = $query->fetch($this->db::FETCH_ASSOC);
74
75
        // No data found.
76
        if (is_bool($resultData) && !$resultData) {
77
            $resultData = [];
78
        }
79
80
        if (is_array($resultData)) {
81
            $results = $resultData;
82
        }
83
84
        return $results;
85
    }
86
87
    /**
88
     * Fetch data from session table.
89
     *
90
     * @param string $ip An IP address.
91
     *
92
     * @return array
93
     */
94
    protected function doFetchFromSessionTable(string $ip): array
95
    {
96
        $results = [];
97
98
        $sql = 'SELECT * FROM ' . $this->tableSessions . '
99
            WHERE id = :id
100
            LIMIT 1';
101
102
        $query = $this->db->prepare($sql);
103
        $query->bindValue(':id', $ip, $this->db::PARAM_STR);
104
        $query->execute();
105
        $resultData = $query->fetch($this->db::FETCH_ASSOC);
106
107
        // No data found.
108
        if (is_bool($resultData) && !$resultData) {
109
            $resultData = [];
110
        }
111
112
        if (is_array($resultData)) {
113
            $results = $resultData;
114
        }
115
116
        return $results;
117
    }
118
119
    /**
120
     * Fetch all data from filter table.
121
     *
122
     * @return array
123
     */
124
    protected function doFetchAllFromFilterTable(): array
125
    {
126
        $results = [];
127
128
        $sql = 'SELECT log_ip, log_data FROM ' . $this->tableFilterLogs;
129
130
        $query = $this->db->prepare($sql);
131
        $query->execute();
132
        $resultData = $query->fetchAll($this->db::FETCH_ASSOC);
133
134
        if (is_array($resultData)) {
135
            $results = $resultData;
136
        }
137
138
        return $results;
139
    }
140
141
    /**
142
     * Fetch all data from filter table.
143
     *
144
     * @return array
145
     */
146
    protected function doFetchAllFromRuleTable(): array
147
    {
148
        $results = [];
149
150
        $sql = 'SELECT * FROM ' . $this->tableRuleList;
151
152
        $query = $this->db->prepare($sql);
153
        $query->execute();
154
        $resultData = $query->fetchAll($this->db::FETCH_ASSOC);
155
156
        if (is_array($resultData)) {
157
            $results = $resultData;
158
        }
159
160
        return $results;
161
    }
162
163
    /**
164
     * Fetch all data from session table.
165
     * @return array
166
     */
167
    protected function doFetchAllFromSessionTable(): array
168
    {
169
        $results = [];
170
171
        $sql = 'SELECT * FROM ' . $this->tableSessions . ' ORDER BY microtimesamp ASC';
172
173
        $query = $this->db->prepare($sql);
174
        $query->execute();
175
        $resultData = $query->fetchAll($this->db::FETCH_ASSOC);
176
177
        if (is_array($resultData)) {
178
            $results = $resultData;
179
        }
180
181
        return $results;
182
    }
183
}