PluginProbe
Auto Alt Text / 2.7.0
Auto Alt Text v2.7.0
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 / Logging / DBLogger.php

DBLogger.php in Auto Alt Text 2.7.0, at src/App/Logging/DBLogger.php

139 lines 3.6 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\Logging;
4
5 use AATXT\App\Domain\Entities\ErrorLog;
6 use AATXT\App\Infrastructure\Database\ErrorLogSchema;
7 use AATXT\App\Infrastructure\Repositories\ErrorLogRepositoryInterface;
8
9 /**
10 * Database Logger
11 *
12 * Logs error messages to the database using the ErrorLogRepository.
13 * This class has been refactored to use dependency injection and the repository pattern.
14 */
15 class DBLogger implements LoggerInterface
16 {
17 /**
18 * Error log repository
19 *
20 * @var ErrorLogRepositoryInterface
21 */
22 private $repository;
23
24 /**
25 * Database schema manager
26 *
27 * @var ErrorLogSchema
28 */
29 private $schema;
30
31 /**
32 * Constructor
33 *
34 * @param ErrorLogRepositoryInterface $repository Error log repository
35 * @param ErrorLogSchema $schema Database schema manager
36 */
37 public function __construct(ErrorLogRepositoryInterface $repository, ErrorLogSchema $schema)
38 {
39 $this->repository = $repository;
40 $this->schema = $schema;
41 }
42
43 /**
44 * Create DBLogger instance (static factory method)
45 *
46 * @deprecated 2.6.0 Use dependency injection instead. This method will be removed in v3.0.0.
47 * @return DBLogger
48 */
49 public static function make(): DBLogger
50 {
51 _deprecated_function(__METHOD__, '2.6.0', 'Dependency injection via Container');
52
53 global $wpdb;
54
55 // Create dependencies manually for backward compatibility
56 $schema = new ErrorLogSchema($wpdb);
57 $repository = new \AATXT\App\Infrastructure\Repositories\ErrorLogRepository($wpdb, $schema);
58
59 return new self($repository, $schema);
60 }
61
62 /**
63 * Write a new record for the single error
64 *
65 * @param int $imageId WordPress attachment ID
66 * @param string $errorMessage Error message text
67 * @return void
68 */
69 public function writeImageLog(int $imageId, string $errorMessage): void
70 {
71 $errorLog = new ErrorLog($imageId, $errorMessage);
72 $this->repository->save($errorLog);
73 }
74
75 /**
76 * Get all error records from the logs table
77 *
78 * Returns a formatted string with all error logs.
79 *
80 * @return string Formatted error logs
81 */
82 public function getImageLog(): string
83 {
84 $logs = $this->repository->findAll();
85
86 if (empty($logs)) {
87 return "";
88 }
89
90 $output = "";
91 foreach ($logs as $log) {
92 $output .= sprintf(
93 "[%s] - Image ID: %d - Error: %s\n",
94 $log->getOccurredAt()->format('Y-m-d H:i:s'),
95 $log->getImageId(),
96 $log->getErrorMessage()
97 );
98 }
99
100 return $output;
101 }
102
103 /**
104 * Create the Log table
105 *
106 * @deprecated 2.6.0 Use ErrorLogSchema::create() instead. This method will be removed in v3.0.0.
107 * @return void
108 */
109 public function createLogTable(): void
110 {
111 _deprecated_function(__METHOD__, '2.6.0', 'ErrorLogSchema::create()');
112 $this->schema->create();
113 }
114
115 /**
116 * Drop the Log table
117 *
118 * @deprecated 2.6.0 Use ErrorLogSchema::drop() instead. This method will be removed in v3.0.0.
119 * @return void
120 */
121 public function dropLogTable(): void
122 {
123 _deprecated_function(__METHOD__, '2.6.0', 'ErrorLogSchema::drop()');
124 $this->schema->drop();
125 }
126
127 /**
128 * Check if Log table exists
129 *
130 * @deprecated 2.6.0 Use ErrorLogSchema::exists() instead. This method will be removed in v3.0.0.
131 * @return bool
132 */
133 private function logTableExists(): bool
134 {
135 _deprecated_function(__METHOD__, '2.6.0', 'ErrorLogSchema::exists()');
136 return $this->schema->exists();
137 }
138 }
139