table = \ABeCe\Booking\Infrastructure\Database\Tables::name( $wpdb, 'notifications' ); } public function insert( array $data ): int { $this->write( fn() => $this->wpdb->insert( $this->table, $data ) ); return (int) $this->wpdb->insert_id; } public function update( int $id, array $data ): void { $this->write( fn() => $this->wpdb->update( $this->table, $data, [ 'id' => $id ] ) ); } public function forBooking( int $id ): array { $wpdb = $this->wpdb; // Delivery metadata only: do not expose the recipient's email through REST. $rows = $wpdb->get_results( $wpdb->prepare( 'SELECT id, notification_type, channel, status, attempts, sent_at, created_at, last_error FROM %i WHERE booking_id = %d ORDER BY id', $this->table, $id ), ARRAY_A ); if ( $wpdb->last_error ) { $this->fail(); } foreach ( $rows as &$row ) { $row['id'] = (int) $row['id']; $row['attempts'] = (int) $row['attempts']; } unset( $row ); return $rows; } private function write( callable $operation ): void { $old = $this->wpdb->suppress_errors( true ); try { if ( false === $operation() ) { $this->fail(); } } finally { $this->wpdb->suppress_errors( $old ); } } private function fail(): never { throw new CatalogException( 'abece_booking_notification_database_error', __( 'The notification could not be saved.', 'abece-booking' ), 500 ); } }