PluginProbe
Auto Alt Text / trunk
Auto Alt Text vtrunk
3.0.3 2.8.2 1.3.1 1.3.2 2.0.0 2.1.0 2.1.1 2.2.0 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 2.4.0 2.4.1 2.4.2 2.5.0 2.5.1 2.5.2 2.5.3 2.6.0 2.6.1 2.7.0 2.8.0 2.8.1 All 28 releases
auto-alt-text / src / App / Infrastructure / Repositories / ErrorLogRepository.php

ErrorLogRepository.php in Auto Alt Text trunk, at src/App/Infrastructure/Repositories/ErrorLogRepository.php

170 lines 4.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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