remove(Path::join(sys_get_temp_dir(), 'cookie.txt')); } } class Response { private array $decodedResponse; public function __construct(private readonly int $code, private readonly string $rawResponse) { $decoded = json_decode($this->rawResponse, true); if (json_last_error() !== JSON_ERROR_NONE || !is_array($decoded)) { $preview = strlen($this->rawResponse) > 500 ? substr($this->rawResponse, 0, 500) . '...' : $this->rawResponse; throw new \RuntimeException('Invalid JSON response: ' . json_last_error_msg() . ' (HTTP ' . $this->code . '). Response: ' . $preview); } $this->decodedResponse = $decoded; } public function getHttpCode(): int { return $this->code; } public function getResponse(): array { return $this->decodedResponse; } public function getRawResponse(): string { return $this->rawResponse; } public function wasSuccessful(): bool { return !empty($this->decodedResponse) && array_key_exists('error', $this->decodedResponse) && empty($this->decodedResponse['error']); } public function getErrorMessage(): string { return $this->decodedResponse['error']['message'] ?? 'None'; } public function getErrorCode(): int { return intval($this->decodedResponse['error']['code'] ?? 0); } public function getError(): string { return $this->getErrorMessage() . ' (Code ' . $this->getErrorCode() . ')'; } public function generatePHPUnitMessage(): string { return 'The API request failed with the following message: ' . $this->getError(); } public function getResult(): mixed { return $this->decodedResponse['result'] ?? ''; } }