Passed
Push — master ( 0619f7...a8d415 )
by Kuts
01:34
created

Inoreader::unreadCount()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
4
namespace ExileeD\Inoreader;
5
6
7
use ExileeD\Inoreader\Client\ClientInterface;
8
use ExileeD\Inoreader\Exception\InoreaderException;
9
use ExileeD\Inoreader\Objects\AddSubscription;
10
use ExileeD\Inoreader\Objects\StreamContents;
11
use ExileeD\Inoreader\Objects\Subscriptions;
12
use ExileeD\Inoreader\Objects\Tag;
13
use ExileeD\Inoreader\Objects\UnreadCount;
14
use ExileeD\Inoreader\Objects\UserInfo;
15
16
class Inoreader
17
{
18
19
    private const API_OAUTH = 'https://www.inoreader.com/oauth2/';
20
21
    /**
22
     * Api key
23
     *
24
     * @var string
25
     */
26
    protected $api_key;
27
    /**
28
     * @var string
29
     */
30
    protected $api_secret;
31
    /**
32
     * @var Client
33
     */
34
    protected $client;
35
36
    public function __construct(int $api_key, string $api_secret, ClientInterface $client = null)
37
    {
38
39
        $this->api_key    = $api_key;
40
        $this->api_secret = $api_secret;
41
42
        $this->client = new Client($client);
43
    }
44
45
46
    /**
47
     * @return string
48
     */
49
    public function getAccessToken(): string
50
    {
51
        return $this->getClient()->getAccessToken();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getClient()->getAccessToken() could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
52
    }
53
54
    /**
55
     * @param string $access_token
56
     */
57
    public function setAccessToken(string $access_token): void
58
    {
59
        $this->getClient()->setAccessToken($access_token);
60
    }
61
62
    /**
63
     * @access public
64
     * @return Client
65
     */
66
    public function getClient(): Client
67
    {
68
        return $this->client;
69
    }
70
71
    /**
72
     * @access public
73
     *
74
     * @param  Client $client
75
     *
76
     * @return $this
77
     */
78
    public function setClient(Client $client): self
79
    {
80
        $this->client = $client;
81
82
        return $this;
83
    }
84
85
    /**
86
     * @param string $redirect_uri This is the address that the user will be redirected to when he authorizes your application from the consent page.
87
     * @param string $scope        You can pass read or read write
88
     * @param string $state        Up to 500 bytes of arbitrary data that will be passed back to your redirect URI.
89
     *
90
     * @see https://www.inoreader.com/developers/oauth
91
     * @return string
92
     */
93
    public function authUrl(string $redirect_uri, string $state, string $scope = ''): string
94
    {
95
96
        $query = [
97
            'client_id'     => $this->api_key,
98
            'redirect_uri'  => $redirect_uri,
99
            'response_type' => 'code',
100
            'scope'         => $scope,
101
            'state'         => $state,
102
        ];
103
104
        return self::API_OAUTH . 'auth' . '?' . http_build_query($query);
105
    }
106
107
    public function token(string $code, string $redirect_uri)
108
    {
109
110
        $params = [
111
            'code'          => $code,
112
            'redirect_uri'  => $redirect_uri,
113
            'client_id'     => $this->api_key,
114
            'client_secret' => $this->api_secret,
115
            'scope'         => '',
116
            'grant_type'    => 'authorization_code',
117
        ];
118
119
        $response = $this->getClient()->post(self::API_OAUTH . 'token', $params);
120
121
        var_dump($response);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($response) looks like debug code. Are you sure you do not want to remove it?
Loading history...
122
    }
123
124
    /**
125
     * Basic information about the logged in user.
126
     *
127
     * @see https://www.inoreader.com/developers/user-info
128
     * @throws InoreaderException
129
     * @return UserInfo
130
     */
131
    public function userInfo(): UserInfo
132
    {
133
        $response = $this->getClient()->get('user-info');
134
135
        return new UserInfo($response);
136
    }
137
138
    /**
139
     * This method is used to subscribe to feeds.
140
     *
141
     * @param string $url feedId to subscribe to
142
     *
143
     * @see https://www.inoreader.com/developers/add-subscription
144
     * @throws InoreaderException
145
     * @return AddSubscription
146
     */
147
    public function addSubscription(string $url): AddSubscription
148
    {
149
        $params   = [
150
            'quickadd' => $url,
151
        ];
152
        $response = $this->getClient()->post('subscription/quickadd', $params);
153
154
        return new AddSubscription($response);
155
    }
156
157
    /**
158
     * Fetch the unread counters for folders, tags and feeds.
159
     *
160
     * @see https://www.inoreader.com/developers/unread-counts
161
     * @throws InoreaderException
162
     * @return UnreadCount
163
     */
164
    public function unreadCount(): UnreadCount
165
    {
166
        $response = $this->getClient()->post('unread-count');
167
168
        return new UnreadCount($response);
169
    }
170
171
172
    /**
173
     * Fetches the current subscriptions for the logged user
174
     *
175
     * @see http://www.inoreader.com/developers/subscription-list
176
     * @throws InoreaderException
177
     * @return Subscriptions
178
     */
179
    public function subscriptionList(): Subscriptions
180
    {
181
182
        $response = $this->getClient()->get('active_search/create');
183
184
        return new Subscriptions($response);
185
186
    }
187
188
189
    /**
190
     * Folders and tags list
191
     *
192
     * @param int|string $types  Set to 1 to get the item type. Can be tag, folder or active_search
193
     * @param int        $counts Set to 1 to get unread counts for tags and active searches.
194
     *
195
     * @see http://www.inoreader.com/developers/tag-list
196
     * @throws InoreaderException
197
     * @return Tag[]
198
     */
199
    public function tagsList($types = 1, $counts = 1): array
200
    {
201
202
        $response = $this->getClient()->get('tag/list', ['types' => $types, 'counts' => $counts]);
203
204
        $result = [];
205
206
        foreach ($response->tags as $tag) {
207
            $result[] = new Tag($tag);
208
        }
209
210
        return $result;
211
212
    }
213
214
    /**
215
     * Returns the articles for a given collection.
216
     *
217
     * @param array $params
218
     *
219
     * @see http://www.inoreader.com/developers/stream-contents
220
     * @throws InoreaderException
221
     * @return StreamContents
222
     */
223
    public function streamContents(array $params = []): StreamContents
224
    {
225
226
        $response = $this->getClient()->get('stream/contents', $params);
227
228
        return new StreamContents($response);
229
230
    }
231
232
    /**
233
     * List of folders and the system.
234
     *
235
     *
236
     * @see http://www.inoreader.com/developers/preference-list
237
     * @throws InoreaderException
238
     * @return StreamContents
239
     */
240
    public function streamPreferenceList(): StreamContents
241
    {
242
243
        $response = $this->getClient()->get('preference/stream/list');
244
245
        return new StreamContents($response);
246
247
    }
248
249
250
    /**
251
     * List of folders and the system.
252
     *
253
     * @param string $stream_id Stream ID
254
     * @param string $key       Key Only accepted is subscription-ordering
255
     * @param string $value     Value.
256
     *
257
     * @see http://www.inoreader.com/developers/preference-set
258
     * @throws InoreaderException
259
     * @return bool
260
     */
261
    public function streamPreferenceSet(string $stream_id, $key = null, $value = null): bool
262
    {
263
        $this->getClient()->get('preference/stream/set',
264
            [
265
                's' => $stream_id,
266
                'k' => $key,
267
                'v' => $value,
268
            ]
269
        );
270
271
        return true;
272
273
    }
274
275
276
    /**
277
     * This method is used to rename tags and folders
278
     *
279
     * @param string $source Source name
280
     * @param string $target Target name
281
     *
282
     * @see http://www.inoreader.com/developers/rename-tag
283
     * @throws InoreaderException
284
     * @return bool
285
     */
286
    public function renameTag(string $source, string $target): bool
287
    {
288
        $this->getClient()->get('rename-tag',
289
            [
290
                's'    => $source,
291
                'dest' => $target,
292
            ]
293
        );
294
295
        return true;
296
    }
297
298
299
    /**
300
     * This method is used to delete tags and folders.
301
     *
302
     * @param string $source Full tag name
303
     *
304
     * @see http://www.inoreader.com/developers/delete-tag
305
     * @throws InoreaderException
306
     * @return bool
307
     */
308
    public function deleteTag(string $source): bool
309
    {
310
        $this->getClient()->get('disable-tag',
311
            [
312
                's' => $source,
313
            ]
314
        );
315
316
        return true;
317
    }
318
319
    /**
320
     * This method is used to mark articles as read, or to star them.
321
     *
322
     * @param array  $items  Item ID
323
     * @param string $add    Tag to add
324
     * @param string $remove Tag to remove
325
     *
326
     * @see http://www.inoreader.com/developers/edit-tag
327
     * @throws InoreaderException
328
     * @return bool
329
     */
330
    public function editTag(array $items, string $add = null, string $remove = null): bool
331
    {
332
333
        $i = http_build_query($items);
334
335
        $params = [
336
            'i' => $i,
337
            'a' => $add,
338
            'r' => $remove,
339
        ];
340
341
        $this->getClient()->get('edit-tag', $params);
342
343
        return true;
344
    }
345
346
347
    /**
348
     * This method marks all items in a given stream as read.
349
     *
350
     * @param int    $timestamp Unix Timestamp in seconds or microseconds.
351
     * @param string $stream_id Stream ID
352
     *
353
     * @see http://www.inoreader.com/developers/mark-all-as-read
354
     * @throws InoreaderException
355
     * @return bool
356
     */
357
    public function markAllAsRead(int $timestamp, string $stream_id): bool
358
    {
359
360
        $this->getClient()->get('mark-all-as-read', [
361
            'ts' => $timestamp,
362
            's'  => $stream_id,
363
        ]);
364
365
        return true;
366
    }
367
368
369
}