Skip to content

Commit 4ff14b4

Browse files
committed
Improve code quaity.
1 parent da39643 commit 4ff14b4

File tree

2 files changed

+202
-169
lines changed

2 files changed

+202
-169
lines changed

src/Firewall/Driver/SqlDriverProvider.php

Lines changed: 19 additions & 169 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,20 @@
1313
namespace Shieldon\Firewall\Driver;
1414

1515
use Shieldon\Firewall\Driver\DriverProvider;
16+
use Shieldon\Firewall\Driver\SqlTrait;
1617
use Exception;
1718
use PDO;
1819

19-
use function is_array;
2020
use function is_bool;
21+
use function is_null;
2122

2223
/**
2324
* SQL Driver provider.
2425
*/
2526
class SqlDriverProvider extends DriverProvider
2627
{
28+
use SqlTrait;
29+
2730
/**
2831
* Data engine will be used on.
2932
*
@@ -196,10 +199,16 @@ protected function doSave(string $ip, array $data, string $type = 'filter', $exp
196199
*/
197200
protected function doDelete(string $ip, string $type = 'filter'): bool
198201
{
202+
199203
switch ($type) {
200-
case 'rule' : return $this->remove($this->tableRuleList, ['log_ip' => $ip]);
201-
case 'filter': return $this->remove($this->tableFilterLogs, ['log_ip' => $ip]);
202-
case 'session' : return $this->remove($this->tableSessions, ['id' => $ip]);
204+
case 'rule':
205+
return $this->remove($this->tableRuleList, ['log_ip' => $ip]);
206+
207+
case 'filter':
208+
return $this->remove($this->tableFilterLogs, ['log_ip' => $ip]);
209+
210+
case 'session':
211+
return $this->remove($this->tableSessions, ['id' => $ip]);
203212
}
204213

205214
return false;
@@ -222,7 +231,7 @@ protected function doRebuild(): bool
222231
*
223232
* @return bool
224233
*/
225-
private function update(string $table, array $data, array $where)
234+
protected function update(string $table, array $data, array $where)
226235
{
227236
$placeholder = [];
228237
foreach($data as $k => $v) {
@@ -288,7 +297,7 @@ private function update(string $table, array $data, array $where)
288297
*
289298
* @return bool
290299
*/
291-
private function insert(string $table, array $data)
300+
protected function insert(string $table, array $data)
292301
{
293302
$placeholderField = [];
294303
$placeholderValue = [];
@@ -347,7 +356,7 @@ private function insert(string $table, array $data)
347356
*
348357
* @return bool
349358
*/
350-
private function remove(string $table, array $where): bool
359+
protected function remove(string $table, array $where): bool
351360
{
352361

353362
$placeholder = [];
@@ -469,7 +478,7 @@ protected function rebuildSql(): bool
469478

470479
$this->installSql();
471480

472-
return true;
481+
473482

474483
// @codeCoverageIgnoreStart
475484

@@ -478,6 +487,8 @@ protected function rebuildSql(): bool
478487
}
479488

480489
// @codeCoverageIgnoreEnd
490+
491+
return true;
481492
}
482493

483494
/**
@@ -497,165 +508,4 @@ protected function checkTableExists(): bool
497508

498509
return false;
499510
}
500-
501-
/**
502-
* Fetch data from filter table.
503-
*
504-
* @param string $ip An IP address.
505-
*
506-
* @return array
507-
*/
508-
private function doFetchFromFilterTable(string $ip): array
509-
{
510-
$results = [];
511-
512-
$sql = 'SELECT log_ip, log_data FROM ' . $this->tableFilterLogs . '
513-
WHERE log_ip = :log_ip
514-
LIMIT 1';
515-
516-
$query = $this->db->prepare($sql);
517-
$query->bindValue(':log_ip', $ip, $this->db::PARAM_STR);
518-
$query->execute();
519-
$resultData = $query->fetch($this->db::FETCH_ASSOC);
520-
521-
// No data found.
522-
if (is_bool($resultData) && !$resultData) {
523-
$resultData = [];
524-
}
525-
526-
if (!empty($resultData['log_data'])) {
527-
$results = json_decode($resultData['log_data'], true);
528-
}
529-
530-
return $results;
531-
}
532-
533-
/**
534-
* Fetch data from rule table.
535-
*
536-
* @param string $ip An IP address.
537-
*
538-
* @return array
539-
*/
540-
private function doFetchFromRuleTable(string $ip): array
541-
{
542-
$results = [];
543-
544-
$sql = 'SELECT * FROM ' . $this->tableRuleList . '
545-
WHERE log_ip = :log_ip
546-
LIMIT 1';
547-
548-
$query = $this->db->prepare($sql);
549-
$query->bindValue(':log_ip', $ip, $this->db::PARAM_STR);
550-
$query->execute();
551-
$resultData = $query->fetch($this->db::FETCH_ASSOC);
552-
553-
// No data found.
554-
if (is_bool($resultData) && !$resultData) {
555-
$resultData = [];
556-
}
557-
558-
if (is_array($resultData)) {
559-
$results = $resultData;
560-
}
561-
562-
return $results;
563-
}
564-
565-
/**
566-
* Fetch data from session table.
567-
*
568-
* @param string $ip An IP address.
569-
*
570-
* @return array
571-
*/
572-
private function doFetchFromSessionTable(string $ip): array
573-
{
574-
$results = [];
575-
576-
$sql = 'SELECT * FROM ' . $this->tableSessions . '
577-
WHERE id = :id
578-
LIMIT 1';
579-
580-
$query = $this->db->prepare($sql);
581-
$query->bindValue(':id', $ip, $this->db::PARAM_STR);
582-
$query->execute();
583-
$resultData = $query->fetch($this->db::FETCH_ASSOC);
584-
585-
// No data found.
586-
if (is_bool($resultData) && !$resultData) {
587-
$resultData = [];
588-
}
589-
590-
if (is_array($resultData)) {
591-
$results = $resultData;
592-
}
593-
594-
return $results;
595-
}
596-
597-
/**
598-
* Fetch all data from filter table.
599-
*
600-
* @return array
601-
*/
602-
private function doFetchAllFromFilterTable(): array
603-
{
604-
$results = [];
605-
606-
$sql = 'SELECT log_ip, log_data FROM ' . $this->tableFilterLogs;
607-
608-
$query = $this->db->prepare($sql);
609-
$query->execute();
610-
$resultData = $query->fetchAll($this->db::FETCH_ASSOC);
611-
612-
if (is_array($resultData)) {
613-
$results = $resultData;
614-
}
615-
616-
return $results;
617-
}
618-
619-
/**
620-
* Fetch all data from filter table.
621-
*
622-
* @return array
623-
*/
624-
private function doFetchAllFromRuleTable(): array
625-
{
626-
$results = [];
627-
628-
$sql = 'SELECT * FROM ' . $this->tableRuleList;
629-
630-
$query = $this->db->prepare($sql);
631-
$query->execute();
632-
$resultData = $query->fetchAll($this->db::FETCH_ASSOC);
633-
634-
if (is_array($resultData)) {
635-
$results = $resultData;
636-
}
637-
638-
return $results;
639-
}
640-
641-
/**
642-
* Fetch all data from session table.
643-
* @return array
644-
*/
645-
private function doFetchAllFromSessionTable(): array
646-
{
647-
$results = [];
648-
649-
$sql = 'SELECT * FROM ' . $this->tableSessions . ' ORDER BY microtimesamp ASC';
650-
651-
$query = $this->db->prepare($sql);
652-
$query->execute();
653-
$resultData = $query->fetchAll($this->db::FETCH_ASSOC);
654-
655-
if (is_array($resultData)) {
656-
$results = $resultData;
657-
}
658-
659-
return $results;
660-
}
661511
}

0 commit comments

Comments
 (0)