Passed
Push — master ( 84e281...f469f1 )
by Kuts
01:36
created

Inoreader::streamContents()   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\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
     * Access token
23
     *
24
     * @var string
25
     */
26
    protected $access_token;
27
    /**
28
     * Api key
29
     *
30
     * @var string
31
     */
32
    protected $api_key;
33
    /**
34
     * @var string
35
     */
36
    protected $api_secret;
37
    /**
38
     * @var Client
39
     */
40
    protected $client;
41
42
    public function __construct(int $api_key, string $api_secret, ClientInterface $client = null)
43
    {
44
45
        $this->api_key    = $api_key;
46
        $this->api_secret = $api_secret;
47
48
        $this->client = new Client($client);
49
    }
50
51
    /**
52
     * Create a Github\Client using a HttpClient.
53
     *
54
     * @param Inoreader $httpClient
55
     *
56
     * @todo
57
     *
58
     * @return Inoreader
59
     */
60
    public static function createWithPassword(string $login, string $password)
0 ignored issues
show
Unused Code introduced by
The parameter $login is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

60
    public static function createWithPassword(/** @scrutinizer ignore-unused */ string $login, string $password)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $password is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

60
    public static function createWithPassword(string $login, /** @scrutinizer ignore-unused */ string $password)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
61
    {
62
        return new self();
0 ignored issues
show
Bug introduced by
The call to ExileeD\Inoreader\Inoreader::__construct() has too few arguments starting with api_key. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

62
        return /** @scrutinizer ignore-call */ new self();

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

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