TwitApp::getClient()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Tequilarapido\Twit;
4
5
use Abraham\TwitterOAuth\TwitterOAuth;
6
use Tequilarapido\Twit\Endpoints\EndpointAliases;
7
8
class TwitApp
9
{
10
    use EndpointAliases;
11
12
    /** @var string */
13
    public $key;
14
15
    /** @var RateLimits */
16
    public $rateLimits;
17
18
    /** @var TwitterOAuth */
19
    private $client;
20
21
    /** @var callable */
22
    private $tolerateTimeoutCallback;
23
24
    /**
25
     * TwitApp constructor.
26
     *
27
     * @param $key
28
     * @param $config
29
     */
30
    public function __construct($key, $config)
31
    {
32
        $this->key = $key;
33
34
        $this->client = new TwitterOAuth(
35
            $config['consumer_key'],
36
            $config['consumer_secret'],
37
            $config['access_token'],
38
            $config['access_token_secret']
39
        );
40
    }
41
42
    /**
43
     * Tolerate twitter timeout for next call.
44
     *
45
     * @param callable $callable
46
     *
47
     * @return $this
48
     */
49
    public function tolerateTimeout(callable $callable)
50
    {
51
        $this->tolerateTimeoutCallback = $callable;
52
53
        return $this;
54
    }
55
56
    /**
57
     * Get TwitterOAuth client.
58
     *
59
     * @return TwitterOAuth
60
     */
61
    public function getClient()
62
    {
63
        return $this->client;
64
    }
65
66
    /**
67
     * Determine if the app is available for use (Rate limit not hit yet).
68
     *
69
     *
70
     * @return bool
71
     */
72
    public function available()
73
    {
74
        if (empty($this->rateLimits)) {
75
            return true;
76
        }
77
78
        return $this->rateLimits->remaining > 0;
79
    }
80
81
    /**
82
     * Remaining requests before hitting the limit.
83
     *
84
     * @return int
85
     */
86
    public function remaining()
87
    {
88
        return $this->rateLimits ? $this->rateLimits->remaining : 0;
89
    }
90
91
    /**
92
     * Forwarded get call to client and catches rate limits.
93
     *
94
     * @param $endpoint
95
     * @param $parameters
96
     *
97
     * @return object
98
     */
99
    public function get($endpoint, $parameters)
100
    {
101
        $response = $this->client->get($endpoint, $parameters);
102
103
        $this->rateLimits = RateLimits::fromHeaders($this->client->getLastXHeaders());
104
105
        return $response;
106
    }
107
108
    /**
109
     * Return the current rate limit status for a specific endpoint.
110
     *
111
     * ie. getRatelimitStatus('followers', '/followers/list')
112
     *
113
     * @param $resource
114
     * @param $endpoint
115
     * @return object
116
     */
117
    public function getRatelimitStatus($resource, $endpoint)
118
    {
119
        $response = $this->client->get('application/rate_limit_status', ['resource' => $resource]);
120
121
        return object_get($response, "resources.{$resource}.{$endpoint}");
0 ignored issues
show
Bug introduced by
It seems like $response can also be of type array; however, parameter $object of object_get() does only seem to accept object, maybe add an additional type check? ( Ignorable by Annotation )

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

121
        return object_get(/** @scrutinizer ignore-type */ $response, "resources.{$resource}.{$endpoint}");
Loading history...
122
    }
123
}
124