<?php
declare(strict_types=1);

namespace App\Framework\ExceptionHandling;

use App\Framework\Config\EnvironmentType;
use App\Framework\ExceptionHandling\Strategy\ErrorPolicyResolver;

final readonly class ExceptionHandlerManager
{
    public function __construct()
    {
        $resolver = new ErrorPolicyResolver();
        $this->registerErrorHandler(new ErrorHandler($resolver->resolve(EnvironmentType::DEV)));

        $this->registerExceptionHandler(new GlobalExceptionHandler());

        $this->registerShutdownHandler(new ShutdownHandler());
    }

    public function registerExceptionHandler(ExceptionHandler $handler): void
    {
        set_exception_handler($handler->handle(...));
    }

    private function registerErrorHandler(ErrorHandler $handler):void
    {
        set_error_handler($handler->handle(...), E_ALL);
    }

    public function registerShutdownHandler(ShutdownHandler $handler): void
    {
        register_shutdown_function($handler->handle(...));
    }
}
