Passed
Push — master ( a8d415...e9058e )
by Kuts
01:33
created

Inoreader::itemsIds()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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