| 1 |
<?php |
| 2 |
|
| 3 |
namespace AATXT\App\Infrastructure\Database; |
| 4 |
|
| 5 |
/** |
| 6 |
* Manages the database schema for error logs table |
| 7 |
* |
| 8 |
* This class is responsible for creating, dropping, and checking the existence |
| 9 |
* of the error logs table in the WordPress database. |
| 10 |
*/ |
| 11 |
final class ErrorLogSchema |
| 12 |
{ |
| 13 |
/** |
| 14 |
* WordPress database abstraction object |
| 15 |
* |
| 16 |
* @var \wpdb |
| 17 |
*/ |
| 18 |
private $wpdb; |
| 19 |
|
| 20 |
/** |
| 21 |
* Table name without prefix |
| 22 |
* |
| 23 |
* @var string |
| 24 |
*/ |
| 25 |
private $tableName; |
| 26 |
|
| 27 |
/** |
| 28 |
* Constructor |
| 29 |
* |
| 30 |
* @param \wpdb $wpdb WordPress database object |
| 31 |
*/ |
| 32 |
public function __construct(\wpdb $wpdb) |
| 33 |
{ |
| 34 |
$this->wpdb = $wpdb; |
| 35 |
$this->tableName = 'aatxt_logs'; |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Create the error logs table |
| 40 |
* |
| 41 |
* Creates the table if it doesn't exist using dbDelta for safe schema updates. |
| 42 |
* |
| 43 |
* @return void |
| 44 |
*/ |
| 45 |
public function create(): void |
| 46 |
{ |
| 47 |
if ($this->exists()) { |
| 48 |
return; |
| 49 |
} |
| 50 |
|
| 51 |
$charset_collate = $this->wpdb->get_charset_collate(); |
| 52 |
$tableName = $this->getTableName(); |
| 53 |
|
| 54 |
$sql = "CREATE TABLE {$tableName} ( |
| 55 |
id mediumint(9) NOT NULL AUTO_INCREMENT, |
| 56 |
time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL, |
| 57 |
image_id mediumint(9) NOT NULL, |
| 58 |
error_message text NOT NULL, |
| 59 |
PRIMARY KEY (id) |
| 60 |
) $charset_collate;"; |
| 61 |
|
| 62 |
require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); |
| 63 |
dbDelta($sql); |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Drop the error logs table |
| 68 |
* |
| 69 |
* Permanently removes the table from the database. |
| 70 |
* |
| 71 |
* @return void |
| 72 |
*/ |
| 73 |
public function drop(): void |
| 74 |
{ |
| 75 |
$tableName = $this->getTableName(); |
| 76 |
$sql = "DROP TABLE IF EXISTS {$tableName};"; |
| 77 |
$this->wpdb->query($sql); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Check if the error logs table exists |
| 82 |
* |
| 83 |
* @return bool True if table exists, false otherwise |
| 84 |
*/ |
| 85 |
public function exists(): bool |
| 86 |
{ |
| 87 |
$tableName = $this->getTableName(); |
| 88 |
$query = $this->wpdb->prepare("SHOW TABLES LIKE %s", $tableName); |
| 89 |
|
| 90 |
return $this->wpdb->get_var($query) === $tableName; |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Get the full table name with WordPress prefix |
| 95 |
* |
| 96 |
* @return string Full table name including WordPress prefix |
| 97 |
*/ |
| 98 |
public function getTableName(): string |
| 99 |
{ |
| 100 |
return $this->wpdb->prefix . $this->tableName; |
| 101 |
} |
| 102 |
} |
| 103 |
|