Inoreader::streamContents()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
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\Exception\InoreaderException;
9
use ExileeD\Inoreader\Objects\ActiveSearch;
10
use ExileeD\Inoreader\Objects\AddSubscription;
11
use ExileeD\Inoreader\Objects\ItemIds;
12
use ExileeD\Inoreader\Objects\StreamContents;
13
use ExileeD\Inoreader\Objects\StreamPreferenceList;
14
use ExileeD\Inoreader\Objects\Subscriptions;
15
use ExileeD\Inoreader\Objects\Tag;
16
use ExileeD\Inoreader\Objects\Token;
17
use ExileeD\Inoreader\Objects\UnreadCount;
18
use ExileeD\Inoreader\Objects\UserInfo;
19
20
class Inoreader
21
{
22
23
    /**
24
     *  The default base URL.
25
     *
26
     * @var string
27
     */
28
    private const API_OAUTH = 'https://www.inoreader.com/oauth2/';
29
30
    /**
31
     * Api key
32
     *
33
     * @var string
34
     */
35
    private $apiKey;
36
37
    /**
38
     * @var string
39
     */
40
    private $apiSecret;
41
42
    /**
43
     * @var Client
44
     */
45
    private $client;
46
47 54
    public function __construct(int $apiId, string $apiKey, HttpClient $httpClient = null)
48
    {
49 54
        $this->apiKey    = $apiId;
50 54
        $this->apiSecret = $apiKey;
51 54
        $client          = new Client($httpClient);
52 54
        $this->setClient($client);
53 54
    }
54
55
56
    /**
57
     * @return string
58
     */
59 3
    public function getAccessToken(): ?string
60
    {
61 3
        return $this->getClient()->getAccessToken();
62
    }
63
64
    /**
65
     * @access public
66
     * @return Client
67
     */
68 51
    public function getClient(): Client
69
    {
70 51
        return $this->client;
71
    }
72
73
    /**
74
     * @access public
75
     *
76
     * @param Client $client
77
     *
78
     * @return void
79
     */
80 54
    public function setClient(Client $client): void
81
    {
82 54
        $this->client = $client;
83 54
    }
84
85
    /**
86
     * @param string|null $accessToken
87
     */
88 3
    public function setAccessToken(string $accessToken = null): void
89
    {
90 3
        $this->getClient()->setAccessToken($accessToken);
91 3
    }
92
93
    /**
94
     * @param string $redirect_uri This is the address that the user will be redirected to when he authorizes
95
     *                             your application from the consent page.
96
     * @param string $scope        You can pass read or read write
97
     * @param string $state        Up to 500 bytes of arbitrary data that will be passed back to your redirect URI.
98
     *
99
     * @return string
100
     * @see https://www.inoreader.com/developers/oauth
101
     */
102 3
    public function getLoginUrl(string $redirect_uri, string $state, string $scope = ''): string
103
    {
104
        $query = [
105 3
            'client_id' => $this->apiKey,
106 3
            'redirect_uri' => $redirect_uri,
107 3
            'response_type' => 'code',
108 3
            'scope' => $scope,
109 3
            'state' => $state,
110
        ];
111
112 3
        return self::API_OAUTH . 'auth' . '?' . http_build_query($query);
113
    }
114
115
    /**
116
     *  Refreshing an access token
117
     *
118
     * @param string $code
119
     * @param string $redirect_uri
120
     *
121
     * @return Token
122
     * @throws InoreaderException
123
     * @see http://www.inoreader.com/developers/oauth
124
     */
125 3
    public function accessTokenFromCode(string $code, string $redirect_uri): Token
126
    {
127
        $params   = [
128 3
            'code' => $code,
129 3
            'redirect_uri' => $redirect_uri,
130 3
            'client_id' => $this->apiKey,
131 3
            'client_secret' => $this->apiSecret,
132 3
            'scope' => '',
133 3
            'grant_type' => 'authorization_code',
134
        ];
135 3
        $response = $this->getClient()->post(self::API_OAUTH . 'token', [], $params);
136
137 3
        return new Token($response);
138
    }
139
140
    /**
141
     *  Refreshing an access token
142
     *
143
     * @param string $refresh_token
144
     *
145
     * @return Token
146
     * @throws InoreaderException
147
     * @see http://www.inoreader.com/developers/oauth
148
     */
149 3
    public function accessTokenFromRefresh(string $refreshToken): Token
150
    {
151
        $params   = [
152 3
            'client_id' => $this->apiKey,
153 3
            'client_secret' => $this->apiSecret,
154 3
            'refresh_token' => $refreshToken,
155 3
            'grant_type' => 'refresh_token',
156
        ];
157 3
        $response = $this->getClient()->post(self::API_OAUTH . 'token', [], $params);
158
159 3
        return new Token($response);
160
    }
161
162
    /**
163
     * Basic information about the logged in user.
164
     *
165
     * @see https://www.inoreader.com/developers/user-info
166
     * @throws InoreaderException
167
     * @return UserInfo
168
     */
169 3
    public function userInfo(): UserInfo
170
    {
171 3
        $response = $this->getClient()->get('user-info');
172
173 3
        return new UserInfo($response);
174
    }
175
176
    /**
177
     * This method is used to subscribe to feeds.
178
     *
179
     * @param string $url feedId to subscribe to
180
     *
181
     * @return AddSubscription
182
     * @throws InoreaderException
183
     * @see https://www.inoreader.com/developers/add-subscription
184
     */
185 3
    public function addSubscription(string $url): AddSubscription
186
    {
187
        $params   = [
188 3
            'quickadd' => $url,
189
        ];
190 3
        $response = $this->getClient()->post('subscription/quickadd', $params);
191
192 3
        return new AddSubscription($response);
193
    }
194
195
    /**
196
     * This method is used to rename the subscription, add it to a folder, remove it from folder or unsubscribe from it.
197
     *
198
     * @param string $params['ac'] Action. Can be edit, subscribe, or unsubscribe.
199
     * @param string $params['s']  Stream ID
200
     * @param string $params['t']  Subscription title.
201
     * @param string $params['a']  Add subscription from folder.
202
     * @param string $params['r']  Remove subscription from folder.
203
     *
204
     * @return bool
205
     * @throws InoreaderException
206
     * @see http://www.inoreader.com/developers/edit-subscription
207
     */
208 3
    public function editSubscription(array $params): bool
209
    {
210 3
        $this->getClient()->post('subscription/edit', $params);
211
212 3
        return true;
213
    }
214
215
216
    /**
217
     * Fetch the unread counters for folders, tags and feeds.
218
     *
219
     * @see https://www.inoreader.com/developers/unread-counts
220
     * @throws InoreaderException
221
     * @return UnreadCount
222
     */
223 3
    public function unreadCount(): UnreadCount
224
    {
225 3
        $response = $this->getClient()->get('unread-count');
226
227 3
        return new UnreadCount($response);
228
    }
229
230
231
    /**
232
     * Fetches the current subscriptions for the logged user
233
     *
234
     * @see http://www.inoreader.com/developers/subscription-list
235
     * @throws InoreaderException
236
     * @return Subscriptions
237
     */
238 3
    public function subscriptionList(): Subscriptions
239
    {
240 3
        $response = $this->getClient()->get('subscription/list');
241
242 3
        return new Subscriptions($response);
243
    }
244
245
246
    /**
247
     * Folders and tags list
248
     *
249
     * @param int|string $types  Set to 1 to get the item type. Can be tag, folder or active_search
250
     * @param int        $counts Set to 1 to get unread counts for tags and active searches.
251
     *
252
     * @return Tag[]
253
     * @throws InoreaderException
254
     * @see http://www.inoreader.com/developers/tag-list
255
     */
256 3
    public function tagsList($types = 1, $counts = 1): array
257
    {
258 3
        $response = $this->getClient()->get('tag/list', ['types' => $types, 'counts' => $counts]);
259 3
        $result   = [];
260 3
        foreach ($response->tags as $tag) {
261 3
            $result[] = new Tag($tag);
262
        }
263
264 3
        return $result;
265
    }
266
267
    /**
268
     * Returns the articles for a given collection.
269
     *
270
     * @param string $streamId Streams can be feeds, tags (folders) or system types
271
     * @param array  $params
272
     *
273
     * @return StreamContents
274
     * @throws InoreaderException
275
     * @see http://www.inoreader.com/developers/stream-contents
276
     */
277 3
    public function streamContents(string $streamId, array $params = []): StreamContents
278
    {
279 3
        $response = $this->getClient()->get(sprintf('stream/contents/%s', $streamId), $params);
280
281 3
        return new StreamContents($response);
282
    }
283
284
285
    /**
286
     * This method is used to return only the article ids for a given stream.
287
     *
288
     * @param array $params
289
     *
290
     * @return ItemIds
291
     * @throws InoreaderException
292
     * @see http://www.inoreader.com/developers/stream-contents
293
     */
294
    public function itemsIds(array $params = []): ItemIds
295
    {
296
        $response = $this->getClient()->get('stream/items/ids', $params);
297
298
        return new ItemIds($response);
299
    }
300
301
302
    /**
303
     * List of folders and the system.
304
     *
305
     *
306
     * @see http://www.inoreader.com/developers/preference-list
307
     * @throws InoreaderException
308
     * @return StreamPreferenceList
309
     */
310
    public function streamPreferenceList(): StreamPreferenceList
311
    {
312
        $response = $this->getClient()->get('preference/stream/list');
313
314
        return new StreamPreferenceList($response);
315
    }
316
317
318
    /**
319
     * List of folders and the system.
320
     *
321
     * @param string      $streamId Stream ID
322
     * @param string|null $key      Key Only accepted is subscription-ordering
323
     * @param string|null $value    Value.
324
     *
325
     * @return bool
326
     * @throws InoreaderException
327
     * @see http://www.inoreader.com/developers/preference-set
328
     */
329 3
    public function streamPreferenceSet(string $streamId, $key = null, $value = null): bool
330
    {
331 3
        $this->getClient()->post(
332 3
            'preference/stream/set',
333
            [
334 3
                's' => $streamId,
335 3
                'k' => $key,
336 3
                'v' => $value,
337
            ]
338
        );
339
340 3
        return true;
341
    }
342
343
344
    /**
345
     * This method is used to rename tags and folders
346
     *
347
     * @param string $source Source name
348
     * @param string $target Target name
349
     *
350
     * @return bool
351
     * @throws InoreaderException
352
     * @see http://www.inoreader.com/developers/rename-tag
353
     */
354 3
    public function renameTag(string $source, string $target): bool
355
    {
356 3
        $this->getClient()->post(
357 3
            'rename-tag',
358
            [
359 3
                's' => $source,
360 3
                'dest' => $target,
361
            ]
362
        );
363
364 3
        return true;
365
    }
366
367
368
    /**
369
     * This method is used to delete tags and folders.
370
     *
371
     * @param string $source Full tag name
372
     *
373
     * @return bool
374
     * @throws InoreaderException
375
     * @see http://www.inoreader.com/developers/delete-tag
376
     */
377 3
    public function deleteTag(string $source): bool
378
    {
379 3
        $this->getClient()->post(
380 3
            'disable-tag',
381
            [
382 3
                's' => $source,
383
            ]
384
        );
385
386 3
        return true;
387
    }
388
389
    /**
390
     * This method is used to mark articles as read, or to star them.
391
     *
392
     * @param array       $items   Item IDs
393
     * @param string|null $add    Tag to add
394
     * @param string|null $remove Tag to remove
395
     *
396
     * @return bool
397
     * @throws InoreaderException
398
     * @see http://www.inoreader.com/developers/edit-tag
399
     */
400 3
    public function editTag(array $items, string $add = null, string $remove = null): bool
401
    {
402
        $params = [
403 3
            'i' => implode(',', $items),
404 3
            'a' => $add,
405 3
            'r' => $remove,
406
        ];
407 3
        $this->getClient()->post('edit-tag', $params);
408
409 3
        return true;
410
    }
411
412
413
    /**
414
     * This method marks all items in a given stream as read.
415
     *
416
     * @param int    $timestamp Unix Timestamp in seconds or microseconds.
417
     * @param string $streamId  Stream ID
418
     *
419
     * @return bool
420
     * @throws InoreaderException
421
     * @see https://www.inoreader.com/developers/mark-all-as-read
422
     */
423 3
    public function markAllAsRead(int $timestamp, string $streamId): bool
424
    {
425 3
        $this->getClient()->get(
426 3
            'mark-all-as-read',
427
            [
428 3
                'ts' => $timestamp,
429 3
                's' => $streamId,
430
            ]
431
        );
432
433 3
        return true;
434
    }
435
436
    /**
437
     * This method create an active search.
438
     *
439
     * @param array $params
440
     *
441
     * @return ActiveSearch
442
     * @throws InoreaderException
443
     * @see https://www.inoreader.com/developers/active-search-create
444
     */
445 3
    public function createActiveSearch(array $params): ActiveSearch
446
    {
447 3
        $response = $this->getClient()->post('active_search/create', [], $params);
448
449 3
        return new ActiveSearch($response);
450
    }
451
452
    /**
453
     * This method delete an active search.
454
     *
455
     * @param string $id Mandatory parameter
456
     *
457
     * @return bool
458
     * @throws InoreaderException
459
     * @see https://www.inoreader.com/developers/active-search-delete
460
     */
461 3
    public function deleteActiveSearch(string $id): bool
462
    {
463 3
        $this->getClient()->get('active_search/delete', [
464 3
            'id' => $id,
465
        ]);
466 3
        return true;
467
    }
468
}
469