<?php

namespace App\Http\Middleware;

use App\Services\CorsService;
use Illuminate\Http\Request;

class HandleCors
{
    private CorsService $corsService;

    public function __construct(CorsService $corsService)
    {
        $this->corsService = $corsService;
    }

    public function handle(Request $request, \Closure $next)
    {
        /** @var \Illuminate\Http\Response */
        $response = $next($request);

        if ($this->corsService->isRequestValid($request)) {
            $this->corsService->addHeadersToResponse($response);
        }

        return $response;
    }
}
