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 / Domain / Entities / ErrorLog.php

ErrorLog.php in Auto Alt Text trunk, at src/App/Domain/Entities/ErrorLog.php

180 lines 4.4 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\Domain\Entities;
4
5 use AATXT\App\Domain\ValueObjects\ImageId;
6 use DateTimeImmutable;
7 use InvalidArgumentException;
8
9 /**
10 * ErrorLog Entity
11 *
12 * Represents a single error log entry in the domain model.
13 * This is an immutable entity that encapsulates error log data.
14 */
15 final class ErrorLog
16 {
17 /**
18 * Error log ID
19 *
20 * @var int|null
21 */
22 private $id;
23
24 /**
25 * WordPress attachment/image ID
26 *
27 * @var ImageId
28 */
29 private $imageId;
30
31 /**
32 * Error message text
33 *
34 * @var string
35 */
36 private $errorMessage;
37
38 /**
39 * Timestamp when error occurred
40 *
41 * @var DateTimeImmutable
42 */
43 private $occurredAt;
44
45 /**
46 * Constructor
47 *
48 * @param int|ImageId $imageId WordPress attachment ID (accepts both int and ImageId for backward compatibility)
49 * @param string $errorMessage Error message text
50 * @param DateTimeImmutable|null $occurredAt Timestamp (defaults to current time)
51 * @param int|null $id Database record ID (null for new records)
52 *
53 * @throws InvalidArgumentException If imageId is invalid or errorMessage is empty
54 */
55 public function __construct(
56 $imageId,
57 string $errorMessage,
58 ?DateTimeImmutable $occurredAt = null,
59 ?int $id = null
60 ) {
61 // Accept both int and ImageId for backward compatibility
62 if ($imageId instanceof ImageId) {
63 $this->imageId = $imageId;
64 } elseif (is_int($imageId)) {
65 $this->imageId = ImageId::fromInt($imageId);
66 } else {
67 throw new InvalidArgumentException('Image ID must be an integer or ImageId instance');
68 }
69
70 if (empty(trim($errorMessage))) {
71 throw new InvalidArgumentException('Error message cannot be empty');
72 }
73
74 $this->id = $id;
75 $this->errorMessage = trim($errorMessage);
76 $this->occurredAt = $occurredAt ?? new DateTimeImmutable();
77 }
78
79 /**
80 * Create ErrorLog from database row
81 *
82 * Factory method to construct an ErrorLog entity from a database result row.
83 *
84 * @param array $row Associative array with keys: id, image_id, error_message, time
85 * @return self
86 *
87 * @throws InvalidArgumentException If required fields are missing
88 */
89 public static function fromDatabaseRow(array $row): self
90 {
91 if (!isset($row['image_id'], $row['error_message'], $row['time'])) {
92 throw new InvalidArgumentException('Missing required fields in database row');
93 }
94
95 $occurredAt = DateTimeImmutable::createFromFormat('Y-m-d H:i:s', $row['time']);
96
97 if ($occurredAt === false) {
98 // Fallback to current time if parsing fails
99 $occurredAt = new DateTimeImmutable();
100 }
101
102 return new self(
103 ImageId::fromInt((int) $row['image_id']),
104 (string) $row['error_message'],
105 $occurredAt,
106 isset($row['id']) ? (int) $row['id'] : null
107 );
108 }
109
110 /**
111 * Get error log ID
112 *
113 * @return int|null Database record ID, or null if not persisted
114 */
115 public function getId(): ?int
116 {
117 return $this->id;
118 }
119
120 /**
121 * Get image/attachment ID as integer.
122 *
123 * @return int WordPress attachment ID
124 */
125 public function getImageId(): int
126 {
127 return $this->imageId->toInt();
128 }
129
130 /**
131 * Get image/attachment ID as ImageId Value Object.
132 *
133 * @return ImageId The ImageId value object
134 */
135 public function getImageIdObject(): ImageId
136 {
137 return $this->imageId;
138 }
139
140 /**
141 * Get error message
142 *
143 * @return string Error message text
144 */
145 public function getErrorMessage(): string
146 {
147 return $this->errorMessage;
148 }
149
150 /**
151 * Get timestamp when error occurred
152 *
153 * @return DateTimeImmutable Immutable datetime object
154 */
155 public function getOccurredAt(): DateTimeImmutable
156 {
157 return $this->occurredAt;
158 }
159
160 /**
161 * Check if this error log has been persisted to database
162 *
163 * @return bool True if record has an ID, false otherwise
164 */
165 public function isPersisted(): bool
166 {
167 return $this->id !== null;
168 }
169
170 /**
171 * Get formatted timestamp for database storage
172 *
173 * @return string Formatted datetime string (Y-m-d H:i:s)
174 */
175 public function getFormattedTime(): string
176 {
177 return $this->occurredAt->format('Y-m-d H:i:s');
178 }
179 }
180