Skip to content

Commit 1482fdc

Browse files
Refactor db usage (#21)
* refactors database use by implementing specific classes for use cases * sanitize before saving in local variable * phpcs * phpcbf --------- Co-authored-by: PT-ATA No One <ata-no-one@gdata.de>
1 parent 8b26260 commit 1482fdc

25 files changed

+455
-430
lines changed

GdataAntivirusPlugin.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
namespace Gdatacyberdefenseag\GdataAntivirus;
44

5+
use Gdatacyberdefenseag\GdataAntivirus\Infrastructure\Database\FindingsQuery;
6+
use Gdatacyberdefenseag\GdataAntivirus\Infrastructure\Database\IFindingsQuery;
57
use Gdatacyberdefenseag\GdataAntivirus\Infrastructure\Database\IGdataAntivirusDatabase;
8+
use Gdatacyberdefenseag\GdataAntivirus\Infrastructure\Database\IScansQuery;
9+
use Gdatacyberdefenseag\GdataAntivirus\Infrastructure\Database\ScansQuery;
610
use Gdatacyberdefenseag\GdataAntivirus\Infrastructure\Database\WordPressDatabase;
711
use Gdatacyberdefenseag\GdataAntivirus\Infrastructure\FileSystem\IGdataAntivirusFileSystem;
812
use Gdatacyberdefenseag\GdataAntivirus\Infrastructure\FileSystem\WordPressFileSystem;
@@ -24,7 +28,8 @@ public function __construct( LoggerInterface $logger = new NullLogger() ) {
2428
$this->singleton(FullScanMenuPage::class, FullScanMenuPage::class);
2529
$this->singleton(OnDemandScan::class, OnDemandScan::class);
2630
$this->singleton(IGdataAntivirusFileSystem::class, WordPressFileSystem::class);
27-
$this->singleton(IGdataAntivirusDatabase::class, WordPressDatabase::class);
31+
$this->singleton(IFindingsQuery::class, FindingsQuery::class);
32+
$this->singleton(IScansQuery::class, ScansQuery::class);
2833
$this->singleton(GdataAntivirusMenuPage::class, GdataAntivirusMenuPage::class);
2934
$this->singleton(ScanClient::class, ScanClient::class);
3035
$this->singleton(AdminNotices::class, AdminNotices::class);
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
<?php
2+
3+
namespace Gdatacyberdefenseag\GdataAntivirus\Infrastructure\Database;
4+
5+
use Psr\Log\LoggerInterface;
6+
use wpdb;
7+
8+
class FindingsQuery implements IFindingsQuery {
9+
private wpdb $wpdb;
10+
private LoggerInterface $logger;
11+
12+
public function __construct(
13+
LoggerInterface $logger,
14+
) {
15+
global $wpdb;
16+
$this->wpdb = $wpdb;
17+
$this->logger = $logger;
18+
}
19+
20+
private function get_table_name(): string {
21+
return $this->wpdb->prefix.GDATACYBERDEFENCEAG_ANTIVIRUS_MENU_FINDINGS_TABLE_NAME;
22+
}
23+
24+
public function create(): void {
25+
$charset_collate = $this->wpdb->get_charset_collate();
26+
$sql = 'CREATE TABLE ' . $this->get_table_name() . ' (
27+
file_path VARCHAR(512) NOT NULL,
28+
UNIQUE KEY file_path (file_path)
29+
)' . $charset_collate . ';';
30+
31+
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
32+
\dbDelta($sql);
33+
\wp_cache_set($this->get_table_name(), 'true', 'GdataAntivirus');
34+
}
35+
36+
public function remove(): void {
37+
if (! $this->table_exists()) {
38+
return;
39+
}
40+
$this->wpdb->query(
41+
$this->wpdb->prepare('DROP TABLE IF EXISTS %i', $this->get_table_name())
42+
);
43+
\wp_cache_set($this->get_table_name(), 'false', 'GdataAntivirus');
44+
}
45+
46+
public function table_exists(): bool {
47+
$tables_exists = \wp_cache_get($this->get_table_name(), 'GdataAntivirus');
48+
$this->logger->debug('Exists in cache: ' . ($tables_exists ? 'true' : 'false'));
49+
if (false === $tables_exists) {
50+
$exists = $this->wpdb->get_var(
51+
$this->wpdb->prepare('SHOW TABLES LIKE %s', $this->get_table_name())
52+
) === $this->get_table_name();
53+
$this->logger->debug('Exists in database: ' . ($exists ? 'true' : 'false'));
54+
\wp_cache_set($this->get_table_name(), \wp_json_encode($exists), 'GdataAntivirus');
55+
return $exists;
56+
}
57+
if ('true' === $tables_exists) {
58+
return true;
59+
}
60+
return false;
61+
}
62+
63+
public function add( string $file ): void {
64+
if (! $this->table_exists()) {
65+
return;
66+
}
67+
68+
try {
69+
$this->wpdb->insert(
70+
$this->get_table_name(),
71+
array( 'file_path' => $file )
72+
);
73+
} catch (\Exception $e) {
74+
$this->logger->debug($e->getMessage());
75+
}
76+
}
77+
78+
public function delete( string $file ): void {
79+
if (! $this->table_exists()) {
80+
return;
81+
}
82+
$this->wpdb->delete(
83+
$this->get_table_name(),
84+
array( 'file_path' => $file )
85+
);
86+
}
87+
88+
public function get_all(): array {
89+
if (! $this->table_exists()) {
90+
return array();
91+
}
92+
return $this->wpdb->get_results(
93+
$this->wpdb->prepare('SELECT file_path FROM %i', $this->get_table_name()),
94+
ARRAY_A
95+
);
96+
}
97+
98+
public function count(): int {
99+
$this->logger->debug('FindingsMenuPage::get_findings_count');
100+
if (! $this->table_exists()) {
101+
return 0;
102+
}
103+
return (int) $this->wpdb->get_var(
104+
$this->wpdb->prepare('SELECT COUNT(*) FROM %i', $this->get_table_name())
105+
);
106+
}
107+
108+
public function validate(): void {
109+
if (! $this->table_exists()) {
110+
return;
111+
}
112+
$findings = $this->get_all();
113+
114+
foreach ($findings as $finding) {
115+
if (! file_exists($finding['file_path'])) {
116+
$this->delete($finding['file_path']);
117+
}
118+
}
119+
}
120+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Gdatacyberdefenseag\GdataAntivirus\Infrastructure\Database;
4+
5+
interface IDatabase {
6+
public function create(): void;
7+
public function remove(): void;
8+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Gdatacyberdefenseag\GdataAntivirus\Infrastructure\Database;
4+
5+
interface IFindingsQuery extends IDatabase {
6+
public function add( string $file ): void;
7+
public function delete( string $file ): void;
8+
public function get_all(): array;
9+
public function table_exists(): bool;
10+
public function count(): int;
11+
public function validate(): void;
12+
}

Infrastructure/Database/IGdataAntivirusDatabase.php

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Gdatacyberdefenseag\GdataAntivirus\Infrastructure\Database;
4+
5+
interface IScansQuery extends IDatabase {
6+
public function write_lock(): void;
7+
public function write_unlock(): void;
8+
public function scheduled_count(): int;
9+
public function increase_scheduled(): void;
10+
public function finished_count(): int;
11+
public function increase_finished(): void;
12+
public function reset(): void;
13+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
namespace Gdatacyberdefenseag\GdataAntivirus\Infrastructure\Database;
4+
5+
use Psr\Log\LoggerInterface;
6+
use wpdb;
7+
8+
class ScansQuery implements IScansQuery {
9+
private wpdb $wpdb;
10+
private LoggerInterface $logger;
11+
12+
public function __construct(
13+
LoggerInterface $logger,
14+
) {
15+
global $wpdb;
16+
$this->wpdb = $wpdb;
17+
$this->logger = $logger;
18+
}
19+
20+
private function get_table_name(): string {
21+
return $this->wpdb->prefix.GDATACYBERDEFENCEAG_ANTIVIRUS_MENU_FULL_SCAN_OPERATIONS_TABLE_NAME;
22+
}
23+
24+
public function create(): void {
25+
$charset_collate = $this->wpdb->get_charset_collate();
26+
$sql = 'CREATE TABLE ' . $this->get_table_name() . ' (
27+
scheduled_scans TINYINT NOT NULL DEFAULT 0,
28+
finished_scans TINYINT NOT NULL DEFAULT 0
29+
)' . $charset_collate . ';';
30+
31+
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
32+
dbDelta($sql);
33+
34+
$this->wpdb->query(
35+
$this->wpdb->prepare('INSERT INTO %i (scheduled_scans, finished_scans) VALUES (0, 0)', $this->get_table_name())
36+
);
37+
}
38+
39+
public function remove(): void {
40+
$this->wpdb->query(
41+
$this->wpdb->prepare('DROP TABLE IF EXISTS %i', $this->get_table_name())
42+
);
43+
}
44+
45+
public function write_lock(): void {
46+
$this->wpdb->query(
47+
$this->wpdb->prepare('LOCK TABLES %i WRITE', $this->get_table_name())
48+
);
49+
}
50+
51+
public function write_unlock(): void {
52+
$this->wpdb->query(
53+
$this->wpdb->prepare('UNLOCK TABLES %i WRITE', $this->get_table_name())
54+
);
55+
}
56+
57+
public function scheduled_count(): int {
58+
return $this->wpdb->get_var(
59+
$this->wpdb->prepare('SELECT scheduled_scans FROM %i', $this->get_table_name())
60+
);
61+
}
62+
63+
public function increase_scheduled(): void {
64+
$this->wpdb->query(
65+
$this->wpdb->prepare('UPDATE %i SET scheduled_scans = scheduled_scans + 1', $this->get_table_name())
66+
);
67+
}
68+
69+
public function finished_count(): int {
70+
return $this->wpdb->get_var(
71+
$this->wpdb->prepare('SELECT finished_scans FROM %i', $this->get_table_name())
72+
);
73+
}
74+
75+
public function increase_finished(): void {
76+
$this->wpdb->query(
77+
$this->wpdb->prepare('UPDATE %i SET finished_scans = finished_scans + 1', $this->get_table_name())
78+
);
79+
}
80+
81+
public function reset(): void {
82+
$this->wpdb->query(
83+
$this->wpdb->prepare('UPDATE %i SET scheduled_scans = 0, finished_scans = 0', $this->get_table_name())
84+
);
85+
}
86+
}

Infrastructure/Database/WordPressDatabase.php

Lines changed: 0 additions & 58 deletions
This file was deleted.

Infrastructure/FileSystem/FileSystemBase.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@
66
use function Amp\ByteStream\Internal\tryToCreateReadableStreamFromResource;
77

88
/**
9-
* this implements the functions that some filesystems have in common
9+
* This implements the functions that some filesystems have in common
1010
* for example, there is no proper replacement for fopen (where you get a stream from)
1111
* in the WP_Filesystem_Base class, so we need to implement it here
1212
*/
1313
trait FileSystemBase {
14-
public function get_resource_stream_from_string(string $content): ReadableResourceStream {
14+
public function get_resource_stream_from_string( string $content ): ReadableResourceStream {
1515
$stream = fopen('php://temp', 'r+');
1616
fwrite($stream, $content);
1717
rewind($stream);
1818
return tryToCreateReadableStreamFromResource($stream);
1919
}
2020

21-
public function open(string $path): ReadableResourceStream {
21+
public function open( string $path ): ReadableResourceStream {
2222
return tryToCreateReadableStreamFromResource(fopen($path, 'r'));
2323
}
2424
}

Infrastructure/FileSystem/IGdataAntivirusFileSystem.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public function open( string $path ): ReadableResourceStream;
99
public function read( string $path ): string;
1010
public function write( string $path, string $content ): bool;
1111
public function delete( string $path ): bool;
12-
public function is_writable( string $path ): bool;
12+
public function is_writable( string $path ): bool;
1313

1414
public function get_resource_stream_from_string( string $content ): ReadableResourceStream;
1515
}

0 commit comments

Comments
 (0)