| 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 |
|