Completed
Push — master ( b77f02...053cae )
by Alessandro
01:47 queued 23s
created

Bitly::setToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * Main Bit.ly PHP class
4
 */
5
6
namespace sineverba\Bitly;
7
8
9
use sineverba\Bitly\Exceptions\SineverbaException;
10
11
class Bitly
12
{
13
14
    /**
15
     * @var the token
16
     */
17
    private $token;
18
19
    /**
20
     * Base API V4 URL
21
     *
22
     */
23
    private $api_url = "https://api-ssl.bitly.com/v4/";
0 ignored issues
show
Coding Style introduced by
$api_url does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
24
25
    /**
26
     * Final short url
27
     *
28
     * @since 1.0.0
29
     * @author sineverba
30
     */
31
    private $short_url;
0 ignored issues
show
Coding Style introduced by
$short_url does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
32
33
    /**
34
     * Bitly constructor.
35
     * We need the token from your personal page:
36
     *
37
     * https://bitly.com/a/oauth_apps
38
     *
39
     * @param $token
40
     */
41
42
    public function __construct($token=null)
43
    {
44
       if ($token===null) {
45
           throw new SineverbaException("Token cannot be empty");
46
       }
47
48
       $this->setToken($token);
49
    }
50
51
    /**
52
     * Get shorten link.
53
     * @param $url original URL
54
     *
55
     * @author @sineverba
56
     * @since 1.0.0
57
     *
58
     */
59
    public function createShortLink($url=null)
60
    {
61
        if ($url===null) {
62
            throw new SineverbaException("URL cannot be empty");
63
        }
64
65
        $option = array();
66
        $option['base_uri'] = $this->getApiUrl();
67
        $option['verify'] = false;
68
        $client = new \GuzzleHttp\Client($option);
69
70
        $headers = [
71
            'Authorization' => 'Bearer ' . $this->getToken(),
72
            'Accept'        => 'application/json',
73
            'Content-Type'  => 'application/json'
74
        ];
75
76
        $post = array();
77
        $post['long_url'] = $url;
78
79
        try {
80
81
            $response = $client->request('POST', 'shorten', [
82
                'headers'    =>  $headers,
83
                'json'      => $post
84
            ]);
85
86
            $response = json_decode($response->getBody());
87
88
            if (is_object($response) && isset($response->link)) {
89
                $short_url = $response->link;
0 ignored issues
show
Coding Style introduced by
$short_url does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
90
                $this->setShortUrl($short_url);
0 ignored issues
show
Coding Style introduced by
$short_url does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
91
            } else {
92
                throw new SineverbaException("No short link found. Probably missing/wrong token");
93
            }
94
95
        } catch (\GuzzleHttp\Exception\ClientException $e) {
96
97
            //echo $e->getMessage();
98
            throw new SineverbaException("No short link found. Probably missing/wrong token");
99
100
        }
101
    }
102
103
104
    /**
105
     * @return null
0 ignored issues
show
Documentation introduced by
Should the return type not be the?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
106
     */
107
    private function getToken()
108
    {
109
        return $this->token;
110
    }
111
112
    /**
113
     * @param null $token
114
     */
115
    private function setToken($token): void
116
    {
117
        $this->token = $token;
118
    }
119
120
    /**
121
     * Return the base API Url
122
     *
123
     * @since 1.0.0
124
     * @author @sineverba
125
     */
126
    private function getApiUrl()
127
    {
128
        return $this->api_url;
129
    }
130
131
    /**
132
     * @return mixed
133
     */
134
    public function getShortUrl()
135
    {
136
        return $this->short_url;
137
    }
138
139
    /**
140
     * @param mixed $short_url
141
     */
142
    private function setShortUrl($short_url): void
0 ignored issues
show
Coding Style introduced by
$short_url does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
143
    {
144
        $this->short_url = $short_url;
0 ignored issues
show
Coding Style introduced by
$short_url does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
145
    }
146
147
}