<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

class EnsureE2ETestingAccess
{
    public function handle(Request $request, Closure $next): Response
    {
        if (! config('e2e.testing_enabled')) {
            abort(404);
        }

        $token = config('e2e.testing_token');
        if ($token && $request->header('X-Testing-Token') !== $token) {
            abort(404);
        }

        return $next($request);
    }
}
