Skip to content

Commit

Permalink
Refactor db usage (#22)
Browse files Browse the repository at this point in the history
* refactors database use by implementing specific classes for use cases

* phpcs

* phpcbf

* use wpdb directly instead of saving it as member

---------

Co-authored-by: PT-ATA No One <[email protected]>
  • Loading branch information
unglaublicherdude and ata-no-one authored Sep 24, 2024
1 parent 1482fdc commit 45ffb83
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 50 deletions.
3 changes: 2 additions & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"vscode": {
"extensions": [
"recca0120.vscode-phpunit",
"github.vscode-github-actions"
"github.vscode-github-actions",
"slevesque.vscode-zipexplorer"
]
}
},
Expand Down
43 changes: 28 additions & 15 deletions Infrastructure/Database/FindingsQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,24 @@
use wpdb;

class FindingsQuery implements IFindingsQuery {
private wpdb $wpdb;
private LoggerInterface $logger;

public function __construct(
LoggerInterface $logger,
) {
global $wpdb;
$this->wpdb = $wpdb;
$this->logger = $logger;
}

private function get_table_name(): string {
return $this->wpdb->prefix.GDATACYBERDEFENCEAG_ANTIVIRUS_MENU_FINDINGS_TABLE_NAME;
global $wpdb;

return $wpdb->prefix.GDATACYBERDEFENCEAG_ANTIVIRUS_MENU_FINDINGS_TABLE_NAME;
}

public function create(): void {
$charset_collate = $this->wpdb->get_charset_collate();
global $wpdb;

$charset_collate = $wpdb->get_charset_collate();
$sql = 'CREATE TABLE ' . $this->get_table_name() . ' (
file_path VARCHAR(512) NOT NULL,
UNIQUE KEY file_path (file_path)
Expand All @@ -34,21 +35,25 @@ public function create(): void {
}

public function remove(): void {
global $wpdb;

if (! $this->table_exists()) {
return;
}
$this->wpdb->query(
$this->wpdb->prepare('DROP TABLE IF EXISTS %i', $this->get_table_name())
$wpdb->query(
$wpdb->prepare('DROP TABLE IF EXISTS %i', $this->get_table_name())
);
\wp_cache_set($this->get_table_name(), 'false', 'GdataAntivirus');
}

public function table_exists(): bool {
global $wpdb;

$tables_exists = \wp_cache_get($this->get_table_name(), 'GdataAntivirus');
$this->logger->debug('Exists in cache: ' . ($tables_exists ? 'true' : 'false'));
if (false === $tables_exists) {
$exists = $this->wpdb->get_var(
$this->wpdb->prepare('SHOW TABLES LIKE %s', $this->get_table_name())
$exists = $wpdb->get_var(
$wpdb->prepare('SHOW TABLES LIKE %s', $this->get_table_name())
) === $this->get_table_name();
$this->logger->debug('Exists in database: ' . ($exists ? 'true' : 'false'));
\wp_cache_set($this->get_table_name(), \wp_json_encode($exists), 'GdataAntivirus');
Expand All @@ -61,12 +66,14 @@ public function table_exists(): bool {
}

public function add( string $file ): void {
global $wpdb;

if (! $this->table_exists()) {
return;
}

try {
$this->wpdb->insert(
$wpdb->insert(
$this->get_table_name(),
array( 'file_path' => $file )
);
Expand All @@ -76,32 +83,38 @@ public function add( string $file ): void {
}

public function delete( string $file ): void {
global $wpdb;

if (! $this->table_exists()) {
return;
}
$this->wpdb->delete(
$wpdb->delete(
$this->get_table_name(),
array( 'file_path' => $file )
);
}

public function get_all(): array {
global $wpdb;

if (! $this->table_exists()) {
return array();
}
return $this->wpdb->get_results(
$this->wpdb->prepare('SELECT file_path FROM %i', $this->get_table_name()),
return $wpdb->get_results(
$wpdb->prepare('SELECT file_path FROM %i', $this->get_table_name()),
ARRAY_A
);
}

public function count(): int {
global $wpdb;

$this->logger->debug('FindingsMenuPage::get_findings_count');
if (! $this->table_exists()) {
return 0;
}
return (int) $this->wpdb->get_var(
$this->wpdb->prepare('SELECT COUNT(*) FROM %i', $this->get_table_name())
return (int) $wpdb->get_var(
$wpdb->prepare('SELECT COUNT(*) FROM %i', $this->get_table_name())
);
}

Expand Down
73 changes: 41 additions & 32 deletions Infrastructure/Database/ScansQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,20 @@

namespace Gdatacyberdefenseag\GdataAntivirus\Infrastructure\Database;

use Psr\Log\LoggerInterface;
use wpdb;

class ScansQuery implements IScansQuery {
private wpdb $wpdb;
private LoggerInterface $logger;

public function __construct(
LoggerInterface $logger,
) {
global $wpdb;
$this->wpdb = $wpdb;
$this->logger = $logger;
public function __construct() {
}

private function get_table_name(): string {
return $this->wpdb->prefix.GDATACYBERDEFENCEAG_ANTIVIRUS_MENU_FULL_SCAN_OPERATIONS_TABLE_NAME;
global $wpdb;

return $wpdb->prefix.GDATACYBERDEFENCEAG_ANTIVIRUS_MENU_FULL_SCAN_OPERATIONS_TABLE_NAME;
}

public function create(): void {
$charset_collate = $this->wpdb->get_charset_collate();
global $wpdb;

$charset_collate = $wpdb->get_charset_collate();
$sql = 'CREATE TABLE ' . $this->get_table_name() . ' (
scheduled_scans TINYINT NOT NULL DEFAULT 0,
finished_scans TINYINT NOT NULL DEFAULT 0
Expand All @@ -31,56 +24,72 @@ public function create(): void {
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
dbDelta($sql);

$this->wpdb->query(
$this->wpdb->prepare('INSERT INTO %i (scheduled_scans, finished_scans) VALUES (0, 0)', $this->get_table_name())
$wpdb->query(
$wpdb->prepare('INSERT INTO %i (scheduled_scans, finished_scans) VALUES (0, 0)', $this->get_table_name())
);
}

public function remove(): void {
$this->wpdb->query(
$this->wpdb->prepare('DROP TABLE IF EXISTS %i', $this->get_table_name())
global $wpdb;

$wpdb->query(
$wpdb->prepare('DROP TABLE IF EXISTS %i', $this->get_table_name())
);
}

public function write_lock(): void {
$this->wpdb->query(
$this->wpdb->prepare('LOCK TABLES %i WRITE', $this->get_table_name())
global $wpdb;

$wpdb->query(
$wpdb->prepare('LOCK TABLES %i WRITE', $this->get_table_name())
);
}

public function write_unlock(): void {
$this->wpdb->query(
$this->wpdb->prepare('UNLOCK TABLES %i WRITE', $this->get_table_name())
global $wpdb;

$wpdb->query(
$wpdb->prepare('UNLOCK TABLES %i WRITE', $this->get_table_name())
);
}

public function scheduled_count(): int {
return $this->wpdb->get_var(
$this->wpdb->prepare('SELECT scheduled_scans FROM %i', $this->get_table_name())
global $wpdb;

return $wpdb->get_var(
$wpdb->prepare('SELECT scheduled_scans FROM %i', $this->get_table_name())
);
}

public function increase_scheduled(): void {
$this->wpdb->query(
$this->wpdb->prepare('UPDATE %i SET scheduled_scans = scheduled_scans + 1', $this->get_table_name())
global $wpdb;

$wpdb->query(
$wpdb->prepare('UPDATE %i SET scheduled_scans = scheduled_scans + 1', $this->get_table_name())
);
}

public function finished_count(): int {
return $this->wpdb->get_var(
$this->wpdb->prepare('SELECT finished_scans FROM %i', $this->get_table_name())
global $wpdb;

return $wpdb->get_var(
$wpdb->prepare('SELECT finished_scans FROM %i', $this->get_table_name())
);
}

public function increase_finished(): void {
$this->wpdb->query(
$this->wpdb->prepare('UPDATE %i SET finished_scans = finished_scans + 1', $this->get_table_name())
global $wpdb;

$wpdb->query(
$wpdb->prepare('UPDATE %i SET finished_scans = finished_scans + 1', $this->get_table_name())
);
}

public function reset(): void {
$this->wpdb->query(
$this->wpdb->prepare('UPDATE %i SET scheduled_scans = 0, finished_scans = 0', $this->get_table_name())
global $wpdb;

$wpdb->query(
$wpdb->prepare('UPDATE %i SET scheduled_scans = 0, finished_scans = 0', $this->get_table_name())
);
}
}
6 changes: 5 additions & 1 deletion Vaas/ScanClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,11 @@ public function scan_single_upload( $file ) {
$is_plugin_uplad = false;

$action = \sanitize_key($_GET['action'] ?? $_POST['action'] ?? '');
$nonce = \sanitize_key($_POST['nonce'] ?? $_POST['_wpnonce']);
if (isset($_POST['_wpnonce'])) {
$nonce = \sanitize_key($_POST['nonce'] ?? $_POST['_wpnonce']);
} else {
$nonce = \sanitize_key($_GET['nonce'] ?? '');
}
if ($action === 'upload-plugin') {
if (wp_verify_nonce($nonce, $action) === false) {
return $file;
Expand Down
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 45ffb83

Please sign in to comment.