| 1 |
<?php |
| 2 |
|
| 3 |
namespace AATXT\App\Infrastructure\Repositories; |
| 4 |
|
| 5 |
use AATXT\App\Domain\Entities\ErrorLog; |
| 6 |
use AATXT\App\Infrastructure\Database\ErrorLogSchema; |
| 7 |
|
| 8 |
/** |
| 9 |
* ErrorLog Repository |
| 10 |
* |
| 11 |
* Concrete implementation of ErrorLogRepositoryInterface. |
| 12 |
* Handles persistence and retrieval of ErrorLog entities using WordPress database. |
| 13 |
*/ |
| 14 |
final class ErrorLogRepository implements ErrorLogRepositoryInterface |
| 15 |
{ |
| 16 |
/** |
| 17 |
* WordPress database abstraction object |
| 18 |
* |
| 19 |
* @var \wpdb |
| 20 |
*/ |
| 21 |
private $wpdb; |
| 22 |
|
| 23 |
/** |
| 24 |
* Database schema manager |
| 25 |
* |
| 26 |
* @var ErrorLogSchema |
| 27 |
*/ |
| 28 |
private $schema; |
| 29 |
|
| 30 |
/** |
| 31 |
* Constructor |
| 32 |
* |
| 33 |
* @param \wpdb $wpdb WordPress database object |
| 34 |
* @param ErrorLogSchema $schema Database schema manager |
| 35 |
*/ |
| 36 |
public function __construct($wpdb, ErrorLogSchema $schema) |
| 37 |
{ |
| 38 |
$this->wpdb = $wpdb; |
| 39 |
$this->schema = $schema; |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Save an error log to the database |
| 44 |
* |
| 45 |
* Creates the table if it doesn't exist, then inserts a new error log record. |
| 46 |
* Note: Currently only supports INSERT, not UPDATE. |
| 47 |
* |
| 48 |
* @param ErrorLog $log The error log entity to save |
| 49 |
* @return void |
| 50 |
*/ |
| 51 |
public function save(ErrorLog $log): void |
| 52 |
{ |
| 53 |
// Ensure table exists |
| 54 |
if (!$this->schema->exists()) { |
| 55 |
$this->schema->create(); |
| 56 |
} |
| 57 |
|
| 58 |
$tableName = $this->schema->getTableName(); |
| 59 |
|
| 60 |
// For now, we only support INSERT (new records) |
| 61 |
// In future, could check if $log->isPersisted() and do UPDATE |
| 62 |
$this->wpdb->insert( |
| 63 |
$tableName, |
| 64 |
[ |
| 65 |
'time' => $log->getFormattedTime(), |
| 66 |
'image_id' => $log->getImageId(), |
| 67 |
'error_message' => sanitize_text_field($log->getErrorMessage()) |
| 68 |
], |
| 69 |
['%s', '%d', '%s'] |
| 70 |
); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Find all error logs |
| 75 |
* |
| 76 |
* @param int $limit Maximum number of records to retrieve |
| 77 |
* @return ErrorLog[] Array of ErrorLog entities |
| 78 |
*/ |
| 79 |
public function findAll(int $limit = 100): array |
| 80 |
{ |
| 81 |
if (!$this->schema->exists()) { |
| 82 |
return []; |
| 83 |
} |
| 84 |
|
| 85 |
$tableName = $this->schema->getTableName(); |
| 86 |
$query = $this->wpdb->prepare( |
| 87 |
"SELECT * FROM {$tableName} ORDER BY time DESC LIMIT %d", |
| 88 |
$limit |
| 89 |
); |
| 90 |
|
| 91 |
$rows = $this->wpdb->get_results($query, ARRAY_A); |
| 92 |
|
| 93 |
if (empty($rows)) { |
| 94 |
return []; |
| 95 |
} |
| 96 |
|
| 97 |
return array_map(function($row) { |
| 98 |
return ErrorLog::fromDatabaseRow($row); |
| 99 |
}, $rows); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Find an error log by ID |
| 104 |
* |
| 105 |
* @param int $id The error log ID |
| 106 |
* @return ErrorLog|null The ErrorLog entity if found, null otherwise |
| 107 |
*/ |
| 108 |
public function findById(int $id): ?ErrorLog |
| 109 |
{ |
| 110 |
if (!$this->schema->exists()) { |
| 111 |
return null; |
| 112 |
} |
| 113 |
|
| 114 |
$tableName = $this->schema->getTableName(); |
| 115 |
$query = $this->wpdb->prepare( |
| 116 |
"SELECT * FROM {$tableName} WHERE id = %d", |
| 117 |
$id |
| 118 |
); |
| 119 |
|
| 120 |
$row = $this->wpdb->get_row($query, ARRAY_A); |
| 121 |
|
| 122 |
if (empty($row)) { |
| 123 |
return null; |
| 124 |
} |
| 125 |
|
| 126 |
return ErrorLog::fromDatabaseRow($row); |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Delete an error log by ID |
| 131 |
* |
| 132 |
* @param int $id The error log ID to delete |
| 133 |
* @return bool True if deleted successfully, false otherwise |
| 134 |
*/ |
| 135 |
public function delete(int $id): bool |
| 136 |
{ |
| 137 |
if (!$this->schema->exists()) { |
| 138 |
return false; |
| 139 |
} |
| 140 |
|
| 141 |
$tableName = $this->schema->getTableName(); |
| 142 |
$result = $this->wpdb->delete( |
| 143 |
$tableName, |
| 144 |
['id' => $id], |
| 145 |
['%d'] |
| 146 |
); |
| 147 |
|
| 148 |
return $result !== false && $result > 0; |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Delete all error logs |
| 153 |
* |
| 154 |
* @return int The number of records deleted |
| 155 |
*/ |
| 156 |
public function deleteAll(): int |
| 157 |
{ |
| 158 |
if (!$this->schema->exists()) { |
| 159 |
return 0; |
| 160 |
} |
| 161 |
|
| 162 |
$tableName = $this->schema->getTableName(); |
| 163 |
$query = "DELETE FROM {$tableName}"; |
| 164 |
|
| 165 |
$result = $this->wpdb->query($query); |
| 166 |
|
| 167 |
return $result !== false ? (int) $result : 0; |
| 168 |
} |
| 169 |
} |
| 170 |
|