GuzzleAdapter::get()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 2
crap 2
1
<?php
2
3
namespace CryptoPete\Frost\Adapter\Http;
4
5
use CryptoPete\Frost\Exception\HttpException;
6
use GuzzleHttp\Client as Guzzle;
7
8
/**
9
 * Class GuzzleAdapter
10
 *
11
 * Fetching the HTTP resource via Guzzle
12
 */
13
class GuzzleAdapter implements HttpInterface
14
{
15
    /**
16
     * @var Guzzle
17
     */
18
    protected $client;
19
20
    /**
21
     * GuzzleAdapter constructor
22
     *
23
     * @param Guzzle $client
24
     */
25 6
    public function __construct(Guzzle $client)
26
    {
27 6
        $this->client = $client;
28 6
    }
29
30
    /**
31
     * Perform GET request
32
     *
33
     * @param string $url   The request URL
34
     * @param string $token The API token
35
     *
36
     * @throws \Exception For failed requests
37
     *
38
     * @return string The GET response
39
     */
40 2
    public function get(string $url, string $token): string
41
    {
42
        try {
43 2
            $response = $this->client->get($url, ['headers' => ['Accept' => 'application/json', 'token' => $token]]);
44 1
        } catch (\Throwable $t) {
45 1
            throw new HttpException("GET Request failed: {$t->getMessage()}");
46
        }
47
48 1
        return (string) $response->getBody();
49
    }
50
51
    /**
52
     * Perform POST request
53
     *
54
     * @param string $url    The request URL
55
     * @param string $token  The API token
56
     * @param array  $params Additional POST params
57
     *
58
     * @throws \Exception For failed requests
59
     *
60
     * @return string The POST response
61
     */
62 2
    public function post(string $url, string $token, array $params): string
63
    {
64
        try {
65 2
            $response = $this->client->post($url, ['headers' => ['Accept' => 'application/json', 'token' => $token], 'form_params' => $params]);
66 1
        } catch (\Throwable $t) {
67 1
            throw new HttpException("POST Request failed: {$t->getMessage()}");
68
        }
69
70 1
        return (string) $response->getBody();
71
    }
72
}
73