<?php

namespace PostenBringCheckout\admin\log;

use PostenBringCheckout\admin\bring\CheckoutApi;


class FatalErrorLogger {

	public static function init() {
		register_shutdown_function([self::class, 'handle_shutdown']);
	}

	public static function handle_shutdown() {
		$error = error_get_last();

		if ($error && in_array($error['type'], [E_ERROR, E_CORE_ERROR, E_COMPILE_ERROR, E_PARSE])) {
			if (strpos($error['file'], '/posten-bring-checkout') !== false) {
				$log_message = "Fatal Error in namespace PostenBringCheckout:\n";
				$log_message .= "Message: {$error['message']}\n";
				$log_message .= "File: {$error['file']}\n";
				$log_message .= "Line: {$error['line']}\n";

				CheckoutApi::get_instance()->remote_log( $log_message, 'ERROR', wp_json_encode(debug_backtrace()) );
			}
		}
	}

}
