Lines 82.35% 14 / 17
Methods 33.33% 1 / 3
Classes 0.00% 0 / 1
Name Lines Methods CRAP
 readResponse 100.00% 4 / 4 100.00% 1 / 1 2
 getCurlClient 83.33% 10 / 12 0.00% 0 / 1 4.07
 send 0.00% 0 / 1 0.00% 0 / 1 2
15class Client
16{
17    /**
18     * @var array $curlopt List of curl options like [CURLOPT_TIMEOUT => 10]
19     *      defined with each component's bootstrap.php
20     */
21    public static $curlopt = [];
22
23    /**
24     * @param RequestInterface $request
25     * @param array $curlOptions Additional or special curl options
26     *
27     * @return ResponseInterface
28     * @throws RequestException|ClientCreationException|ClientExceptionInterface
29     */
30    public static function readResponse(RequestInterface $request, array $curlOptions = array()): ResponseInterface
31    {
32        $client = static::getCurlClient($curlOptions);
33
34        try {
35            return $client->sendRequest($request);
36        } catch (Exception $exception) {
37            throw new RequestException($exception->getMessage(), $request, $exception);
38        }
39    }
40
41    /**
42     * @param $curlOptions
43     * @return \Http\Client\Curl\Client
44     * @throws ClientCreationException
45     */
46    public static function getCurlClient(array $curlOptions): ClientInterface
47    {
48        $curlOptions = $curlOptions + static::$curlopt;
49        if (!isset($curlOptions[CURLOPT_USERAGENT])) {
50            $curlOptions[CURLOPT_USERAGENT] =
51                'Client' . (defined("\App::IDENTIFIER") ? constant("\App::IDENTIFIER") : 'ZMS');
52        }
53
54        try {
55            $client = new \Http\Client\Curl\Client(
56                new ResponseFactory(),
57                new StreamFactory(),
58                $curlOptions
59            );
60        } catch (InvalidArgumentException $exception) {
61            throw new ClientCreationException($exception->getMessage(), 0, $exception);
62        }
63
64        return $client;
65    }
66
67    /**
68     * @psalm-api
69     */
70    public function send(RequestInterface $request): ResponseInterface
71    {
72        return static::readResponse($request);
73    }
74}