Completed
Pull Request — master (#14)
by
unknown
03:40 queued 44s
created

ApiProvider::setRateLimiter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Laravel\Forge;
4
5
use GuzzleHttp\Client;
6
use GuzzleHttp\ClientInterface;
7
8
class ApiProvider
9
{
10
    /**
11
     * Base API URI.
12
     *
13
     * @var string
14
     */
15
    const BASE_URI = 'https://forge.laravel.com/api/v1/';
16
17
    /**
18
     * API token.
19
     *
20
     * @var string
21
     */
22
    private $token;
23
24
    /**
25
     * HTTP client.
26
     *
27
     * @var \GuzzleHttp\ClientInterface
28
     */
29
    protected $client;
30
31
32
    /**
33
     * The optional rate limiting function.
34
     *
35
     * @var \Closure | null
36
     */
37
    protected $rateLimiter = null;
38
39
    /**
40
     * Create new API provider instance.
41
     *
42
     * @param string $token
43
     */
44
    public function __construct(string $token)
45
    {
46
        $this->token = $token;
47
    }
48
49
    /**
50
     * HTTP client.
51
     *
52
     * @return \GuzzleHttp\ClientInterface
53
     */
54
    public function getClient(): ClientInterface
55
    {
56
        if (!is_null($this->rateLimiter)) {
57
            call_user_func($this->rateLimiter);
58
        }
59
60
        if (!is_null($this->client)) {
61
            return $this->client;
62
        }
63
64
        return $this->client = $this->createClient();
65
    }
66
67
    /**
68
     * API token.
69
     *
70
     * @return string
71
     */
72
    public function getToken(): string
73
    {
74
        return $this->token;
75
    }
76
77
    /**
78
     * Create new HTTP client.
79
     *
80
     * @return \GuzzleHttp\ClientInterface
81
     */
82
    public function createClient(): ClientInterface
83
    {
84
        $client = new Client([
85
            'base_uri' => static::BASE_URI,
86
            'headers' => [
87
                'Authorization' => 'Bearer '.$this->getToken(),
88
                'Accept' => 'application/json',
89
                'Content-Type' => 'application/json',
90
            ],
91
        ]);
92
93
        return $client;
94
    }
95
96
    /**
97
     * Sets an optional rate limiting function.
98
     *
99
     * @param \Closure
100
     */
101
    public function setRateLimiter(callable $rateLimiter)
102
    {
103
        $this->rateLimiter = $rateLimiter;
0 ignored issues
show
Documentation Bug introduced by
It seems like $rateLimiter of type callable is incompatible with the declared type object<Closure> of property $rateLimiter.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
104
    }
105
}
106