Bitly::expand()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 15
ccs 9
cts 9
cp 1
rs 9.4285
cc 1
eloc 8
nc 1
nop 1
crap 1
1
<?php
2
3
/*
4
 * This file is part of the Concise package.
5
 *
6
 * (c) Antoine Corcy <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Concise\Provider;
13
14
use Concise\Provider;
15
use Http\Client\HttpClient;
16
use Http\Message\RequestFactory;
17
18
/**
19
 * @author Márk Sági-Kazár <[email protected]>
20
 */
21
class Bitly implements Provider
22
{
23
    /**
24
     * @var string
25
     */
26
    const ENDPOINT = 'https://api-ssl.bitly.com/v3';
27
28
    /**
29
     * @var string
30
     */
31
    private $accessToken;
32
33
    /**
34
     * @var HttpClient
35
     */
36
    private $httpClient;
37
38
    /**
39
     * @var RequestFactory
40
     */
41
    private $requestFactory;
42
43
    /**
44
     * @param string         $accessToken
45
     * @param HttpClient     $httpClient
46
     * @param RequestFactory $requestFactory
47
     */
48 4
    public function __construct($accessToken, HttpClient $httpClient, RequestFactory $requestFactory)
49
    {
50 4
        $this->accessToken = $accessToken;
51 4
        $this->httpClient = $httpClient;
52 4
        $this->requestFactory = $requestFactory;
53 4
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 1
    public function shorten($url)
59
    {
60 1
        $url = sprintf('%s/shorten?%s', self::ENDPOINT, http_build_query([
61 1
            'access_token' => $this->accessToken,
62 1
            'longUrl'      => trim($url),
63 1
        ]));
64
65 1
        $request = $this->requestFactory->createRequest('GET', $url);
66
67 1
        $response = $this->httpClient->sendRequest($request);
68
69 1
        $response = json_decode((string) $response->getBody());
70
71 1
        return $response->data->url;
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77 1
    public function expand($url)
78
    {
79 1
        $url = sprintf('%s/expand?%s', self::ENDPOINT, http_build_query([
80 1
            'access_token' => $this->accessToken,
81 1
            'shortUrl'     => trim($url),
82 1
        ]));
83
84 1
        $request = $this->requestFactory->createRequest('GET', $url);
85
86 1
        $response = $this->httpClient->sendRequest($request);
87
88 1
        $response = json_decode((string) $response->getBody());
89
90 1
        return $response->data->expand[0]->long_url;
91
    }
92
}
93