Client::send()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 2
nop 5
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ExileeD\Inoreader;
6
7
use ExileeD\Inoreader\HttpClient\HttpClient;
8
use ExileeD\Inoreader\HttpClient\GuzzleHttpClient;
9
use ExileeD\Inoreader\Exception\InoreaderException;
10
use Psr\Http\Message\ResponseInterface;
11
12
class Client
13
{
14
15
    /**
16
     * The default base URL.
17
     *
18
     * @var string
19
     */
20
    private const BASE_URL = 'https://www.inoreader.com/reader/api/0/';
21
22
    /**
23
     * The default user agent header.
24
     *
25
     * @var string
26
     */
27
    private const USER_AGENT = 'inoreader-php/1.0.0 (+https://github.com/exileed/inoreader-api)';
28
29
    /**
30
     * @var array
31
     */
32
    private $defaultHeaders = [
33
        'User-Agent' => self::USER_AGENT,
34
    ];
35
36
    /**
37
     * @var HttpClient
38
     */
39
    private $httpClient;
40
41
42
    /** @var string|null */
43
    private $accessToken = null;
44
45
    /**
46
     * Instantiates a new Client object.
47
     *
48
     * @param HttpClient|null $httpClient
49
     */
50 60
    public function __construct(HttpClient $httpClient = null)
51
    {
52 60
        $this->httpClient = $httpClient ?? new GuzzleHttpClient();
53 60
    }
54
55
    /**
56
     * @return string
57
     * @todo
58
     */
59 9
    public function getAccessToken(): ?string
60
    {
61 9
        return $this->accessToken;
62
    }
63
64
    /**
65
     * @param string|null $token
66
     *
67
     * @return void
68
     */
69 9
    public function setAccessToken(string $token = null)
70
    {
71 9
        if ($token === null) {
72 3
            unset($this->defaultHeaders[ 'Authorization' ]);
73 3
            $this->accessToken = null;
74
        } else {
75 6
            $this->defaultHeaders[ 'Authorization' ] = \sprintf('Bearer %s', $token);
76 6
            $this->accessToken                       = $token;
77
        }
78 9
    }
79
80
    /**
81
     * Returns the inoreader client's http client to the given http client. Client.
82
     *
83
     * @return  HttpClient
84
     */
85
    public function getHttpClient(): HttpClient
86
    {
87
        return $this->httpClient;
88
    }
89
90
    /**
91
     * Makes a GET request to the Inoreader API and returns the response
92
     *
93
     * @param string $endpoint
94
     * @param array  $params
95
     *
96
     * @return \stdClass
97
     * @throws InoreaderException
98
     */
99 21
    public function get(string $endpoint, $params = [])
100
    {
101 21
        return $this->send('GET', $endpoint, $params);
102
    }
103
104
    /**
105
     * Makes a POST request to the Inoreader API and returns the response
106
     *
107
     * @param string       $endpoint
108
     * @param array $params
109
     * @param string|array|null $body
110
     *
111
     *
112
     * @return \stdClass
113
     */
114 27
    public function post(string $endpoint, $params = [], $body = null)
115
    {
116 27
        return $this->send('POST', $endpoint, $params, $body);
117
    }
118
119
    /**
120
     * Makes a request to the Inoreader API and returns the response
121
     *
122
     * @param string $method
123
     * @param string $uri
124
     * @param        $body
125
     * @param array  $headers
126
     *
127
     * @return bool|\stdClass
128
     */
129 48
    private function send(string $method, string $uri, array $params, $body = null, array $headers = [])
130
    {
131 48
        $url = mb_substr($uri, 0, 4) === 'http' ?  $uri : \sprintf('%s%s', self::BASE_URL, $uri);
132
133 48
        $headers = \array_merge($this->defaultHeaders, $headers);
134
135 48
        $response = $this->httpClient->request($url, $params, $body, $method, $headers);
136
137 48
        return $this->processResponse($response);
138
    }
139
140
    /**
141
     *
142
     * @param ResponseInterface $response
143
     *
144
     * @return   \stdClass|bool The JSON response from the request
145
     * @throws   InoreaderException
146
     */
147 48
    private function processResponse(ResponseInterface $response)
148
    {
149 48
        $content = $response->getBody()->getContents();
150 48
        if ($content === 'OK') {
151 21
            return true;
152
        }
153
154 27
        return json_decode($content, false, 512, JSON_THROW_ON_ERROR);
155
    }
156
}
157