Files

43 lines
1.2 KiB
PHP

<?php
namespace App\Process\Tester;
use GuzzleHttp\Promise\PromiseInterface;
use Illuminate\Http\Client\Request;
use Illuminate\Support\Facades\Http;
/**
* The scripted network answers of a scenario (e.g. the landlord's AI access answering an extraction). Everything else
* is refused by SandboxServiceProvider.
*/
final class SandboxNetwork
{
/** @var array<string, callable(Request): (PromiseInterface|array<string, mixed>|null)> host => answer */
private static array $answers = [];
/**
* @param callable(Request): (PromiseInterface|array<string, mixed>|null) $answer
*/
public static function script(string $host, callable $answer): void
{
self::$answers[$host] = $answer;
}
public static function reset(): void
{
self::$answers = [];
}
public static function answer(Request $request): ?PromiseInterface
{
$host = (string) parse_url($request->url(), PHP_URL_HOST);
$answer = isset(self::$answers[$host]) ? (self::$answers[$host])($request) : null;
return match (true) {
$answer === null => null,
$answer instanceof PromiseInterface => $answer,
default => Http::response($answer),
};
}
}