Lines 50.00% 6 / 12
Methods 33.33% 3 / 9
Classes 0.00% 0 / 1
Name Lines Methods CRAP
 client 100.00% 4 / 4 100.00% 1 / 1 2
 sendRequest 0.00% 0 / 1 0.00% 0 / 1 2
 get 100.00% 1 / 1 100.00% 1 / 1 1
 post 0.00% 0 / 1 0.00% 0 / 1 2
 put 0.00% 0 / 1 0.00% 0 / 1 2
 patch 0.00% 0 / 1 0.00% 0 / 1 2
 delete 0.00% 0 / 1 0.00% 0 / 1 2
 head 0.00% 0 / 1 0.00% 0 / 1 2
 swap 100.00% 1 / 1 100.00% 1 / 1 1
25class Http
26{
27    /** @var Client|null Cached shared client instance */
28    private static ?Client $client = null;
29
30    /**
31     * Get the shared Client instance, registering it on the container.
32     */
33    public static function client(): Client
34    {
35        if (self::$client === null) {
36            self::$client = new Client();
37            Application::getInstance()->container()->instance(Client::class, self::$client);
38        }
39
40        return self::$client;
41    }
42
43    /**
44     * Send a PSR-7 request.
45     *
46     * @param array<string, mixed> $options Per-request options (sink, timeout, verify_ssl, headers, curl, ...)
47     */
48    public static function sendRequest(RequestInterface $request, array $options = []): ResponseInterface
49    {
50        return self::client()->sendRequest($request, $options);
51    }
52
53    /**
54     * Send a GET request.
55     *
56     * @param string $uri Request URI (absolute, or relative to `base_uri`)
57     * @param array<string, mixed> $params Query parameters to append
58     * @param array<string, mixed> $options Per-request options (sink, timeout, verify_ssl, headers, curl, ...)
59     */
60    public static function get(string $uri, array $params = [], array $options = []): ResponseInterface
61    {
62        return self::client()->get($uri, $params, $options);
63    }
64
65    /**
66     * Send a POST request. Arrays are JSON-encoded.
67     *
68     * @param string $uri Request URI
69     * @param array<mixed>|string|StreamInterface $body Request body
70     * @param array<string, mixed> $options Per-request options (sink, timeout, verify_ssl, headers, curl, ...)
71     */
72    public static function post(string $uri, array|string|StreamInterface $body = [], array $options = []): ResponseInterface
73    {
74        return self::client()->post($uri, $body, $options);
75    }
76
77    /**
78     * Send a PUT request. Arrays are JSON-encoded.
79     *
80     * @param string $uri Request URI
81     * @param array<mixed>|string|StreamInterface $body Request body
82     * @param array<string, mixed> $options Per-request options (sink, timeout, verify_ssl, headers, curl, ...)
83     */
84    public static function put(string $uri, array|string|StreamInterface $body = [], array $options = []): ResponseInterface
85    {
86        return self::client()->put($uri, $body, $options);
87    }
88
89    /**
90     * Send a PATCH request. Arrays are JSON-encoded.
91     *
92     * @param string $uri Request URI
93     * @param array<mixed>|string|StreamInterface $body Request body
94     * @param array<string, mixed> $options Per-request options (sink, timeout, verify_ssl, headers, curl, ...)
95     */
96    public static function patch(string $uri, array|string|StreamInterface $body = [], array $options = []): ResponseInterface
97    {
98        return self::client()->patch($uri, $body, $options);
99    }
100
101    /**
102     * Send a DELETE request. Arrays are JSON-encoded.
103     *
104     * @param string $uri Request URI
105     * @param array<mixed>|string|StreamInterface $body Request body
106     * @param array<string, mixed> $options Per-request options (sink, timeout, verify_ssl, headers, curl, ...)
107     */
108    public static function delete(string $uri, array|string|StreamInterface $body = [], array $options = []): ResponseInterface
109    {
110        return self::client()->delete($uri, $body, $options);
111    }
112
113    /**
114     * Send a HEAD request.
115     *
116     * @param string $uri Request URI
117     * @param array<string, mixed> $params Query parameters to append
118     * @param array<string, mixed> $options Per-request options (sink, timeout, verify_ssl, headers, curl, ...)
119     */
120    public static function head(string $uri, array $params = [], array $options = []): ResponseInterface
121    {
122        return self::client()->head($uri, $params, $options);
123    }
124
125    /**
126     * Swap the shared client instance (primarily for tests).
127     */
128    public static function swap(?Client $client): void
129    {
130        self::$client = $client;
131    }
132}