<?php
namespace SPAATG\Controller;

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

use SPAATG\ShortPixelLogger\ShortPixelLogger as Log;

class ErrorController
{

			public function __construct()
			{

			}

			public static function start()
			{
					if (true === \wpSPAATG()->env()->is_debug)
					{
				 		register_shutdown_function(array(self::class, 'checkErrors'));
					}
			}

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

				 // Nothing, happy us.
				 if (is_null($error))
				 {
					  return;
				 }
				 elseif (1 !== $error['type']) // Nothing fatal.
				 {
					  return;
				 }
				 else {
					 if (ob_get_length() > 0)
					 {
					  ob_clean(); // try to scrub other stuff
					 }
				 		wp_die(
							'<pre>' . esc_html($error['message']) . ' in ' . esc_html($error['file']) . ' on line ' . esc_html($error['line']) . '<br> Last Item ID: ' . esc_html(QueueController::getLastId()) . '</pre><small><br> -ShortPixel Error Handler- </small>',
							esc_html__('ShortPixel Error Handler', 'shortpixel-ai-alt-text-generator')
						);
				 }
			}
}
