|
| 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 | +} |
0 commit comments