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.
Completed
Pull Request — master (#26)
by Choraimy
08:11
created

BitLyShortener   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 66.67%

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 40
ccs 10
cts 15
cp 0.6667
rs 10
c 0
b 0
f 0
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
A shortenAsync() 0 7 1
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