GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Pull Request — master (#25)
by Choraimy
05:53
created

BitLyShortener::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 3
dl 0
loc 13
ccs 9
cts 9
cp 1
crap 1
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
namespace LaraCrafts\UrlShortener\Http;
4
5
use GuzzleHttp\ClientInterface;
6
use GuzzleHttp\Psr7\Request;
7
use Illuminate\Support\Arr;
8
use Psr\Http\Message\ResponseInterface;
9
use function GuzzleHttp\json_decode;
10
11
class BitLyShortener extends RemoteShortener
12
{
13
    protected $client;
14
    protected $defaults;
15
16
    /**
17
     * Create a new Bit.ly shortener.
18
     *
19
     * @param \GuzzleHttp\ClientInterface $client
20
     * @param string $token
21
     * @param string $domain
22
     * @return void
23
     */
24 24
    public function __construct(ClientInterface $client, string $token, string $domain)
25
    {
26 24
        $this->client = $client;
27 24
        $this->defaults = [
28 24
            'allow_redirects' => false,
29 24
            'base_uri' => 'https://api-ssl.bitly.com',
30
            'headers' => [
31 24
                'Accept' => 'application/json',
32 24
                'Authorization' => "Bearer $token",
33 24
                'Content-Type' => 'application/json',
34
            ],
35
            'json' => [
36 24
                'domain' => $domain,
37
            ],
38
        ];
39 24
    }
40
41
    /**
42
     * {@inheritDoc}
43
     */
44
    public function shortenAsync($url, array $options = [])
45
    {
46
        $options = array_merge_recursive(Arr::add($this->defaults, 'json.long_url', $url), $options);
47
        $request = new Request('POST', '/v4/shorten');
48
49
        return $this->client->sendAsync($request, $options)->then(function (ResponseInterface $response) {
50
            return str_replace('http://', 'https://', json_decode($response->getBody()->getContents())->link);
51
        });
52
    }
53
}
54