$events */ public function append(string $subject, int $subjectId, array $events): void { foreach ($events as $event) { $suppress = $this->db->suppress_errors(true); $ok = $this->db->insert(Schema::eventsTable(), [ 'subject' => $subject, 'subject_id' => $subjectId, 'event_type' => $event->type, 'actor_id' => $event->actorId, 'reason' => mb_substr($event->reason, 0, 255), 'before_json' => wp_json_encode($event->before), 'after_json' => wp_json_encode($event->after), 'external_ref' => null !== $event->externalRef ? mb_substr($event->externalRef, 0, 191) : null, 'idempotency_key' => mb_substr($event->idempotencyKey, 0, 191), 'occurred_at' => $event->occurredAt->setTimezone(new \DateTimeZone('UTC'))->format('Y-m-d H:i:s'), ]); $this->db->suppress_errors($suppress); if (false === $ok) { if (str_contains($this->db->last_error, 'Duplicate entry')) { throw DomainError::idempotencyMismatch(); } throw new \RuntimeException('Could not insert event: ' . esc_html($this->db->last_error)); } } } public static function now(): string { return gmdate('Y-m-d H:i:s'); } }