1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LeadThread\GoogleShortener; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use LeadThread\GoogleShortener\Exceptions\GoogleAuthException; |
7
|
|
|
use LeadThread\GoogleShortener\Exceptions\GoogleErrorException; |
8
|
|
|
use LeadThread\GoogleShortener\Exceptions\GoogleRateLimitException; |
9
|
|
|
use GuzzleHttp\Client; |
10
|
|
|
|
11
|
|
|
class Google |
12
|
|
|
{ |
13
|
|
|
const V1 = 'v1'; |
14
|
|
|
|
15
|
|
|
protected $token; |
16
|
|
|
protected $host; |
17
|
|
|
protected $version; |
18
|
|
|
protected $client; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Creates a Calendly instance that can register and unregister webhooks with the API |
22
|
|
|
* @param string $token The API token to use |
23
|
|
|
* @param string $version The API version to use |
24
|
|
|
* @param string $host The Host URL |
25
|
|
|
* @param string $client The Client instance that will handle the http request |
26
|
|
|
*/ |
27
|
24 |
|
public function __construct($token, $version = self::V1, $host = "www.googleapis.com", Client $client = null){ |
28
|
24 |
|
$this->client = $client; |
29
|
24 |
|
$this->token = $token; |
30
|
24 |
|
$this->version = $version; |
31
|
24 |
|
$this->host = $host; |
32
|
24 |
|
} |
33
|
|
|
|
34
|
12 |
|
public function shorten($url, $encode = true) |
35
|
|
|
{ |
36
|
12 |
|
if (empty($url)) { |
37
|
3 |
|
throw new GoogleErrorException("The URL is empty!"); |
38
|
|
|
} |
39
|
|
|
|
40
|
9 |
|
$url = $this->fixUrl($url, $encode); |
41
|
|
|
|
42
|
9 |
|
$data = $this->exec($url); |
43
|
|
|
|
44
|
6 |
|
return $data['id']; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Returns the response data or throws an Exception if it was unsuccessful |
49
|
|
|
* @param string $raw The data from the response |
50
|
|
|
* @return array |
51
|
|
|
*/ |
52
|
9 |
|
protected function handleResponse($raw){ |
53
|
9 |
|
$data = json_decode($raw,true); |
54
|
|
|
|
55
|
9 |
|
if(!empty($data["error"])){ |
56
|
3 |
|
$reason = $data["error"]["errors"][0]["reason"]; |
57
|
3 |
|
$msg = $data["error"]["errors"][0]["message"]; |
58
|
|
|
|
59
|
|
|
switch ($reason) { |
60
|
3 |
|
case 'keyInvalid': |
61
|
3 |
|
throw new GoogleAuthException; |
62
|
|
|
break; |
|
|
|
|
63
|
|
|
|
64
|
|
|
default: |
65
|
|
|
throw new GoogleErrorException("Reason: {$reason}. Message: {$msg}."); |
66
|
|
|
break; |
|
|
|
|
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
6 |
|
return $data; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Returns a corrected URL |
75
|
|
|
* @param string $url The URL to modify |
76
|
|
|
* @param boolean $encode Whether or not to encode the URL |
77
|
|
|
* @return string The corrected URL |
78
|
|
|
*/ |
79
|
15 |
|
protected function fixUrl($url, $encode){ |
80
|
15 |
|
if(strpos($url, "http") !== 0){ |
81
|
12 |
|
$url = "http://".$url; |
82
|
12 |
|
} |
83
|
|
|
|
84
|
15 |
|
if($encode){ |
|
|
|
|
85
|
|
|
// Google does not support an encoded url |
86
|
9 |
|
} |
87
|
|
|
|
88
|
15 |
|
return $url; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Builds the request URL to the Google API for a specified action |
93
|
|
|
* @param string $action The long URL |
94
|
|
|
* @param string $action The API action |
95
|
|
|
* @return string The URL |
96
|
|
|
*/ |
97
|
12 |
|
protected function buildRequestUrl($action = "url"){ |
98
|
12 |
|
return "https://{$this->host}/urlshortener/{$this->version}/{$action}?key={$this->token}"; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Returns the Client instance |
103
|
|
|
* @return Client |
104
|
|
|
*/ |
105
|
9 |
|
protected function getRequest(){ |
106
|
9 |
|
$client = $this->client; |
107
|
9 |
|
if(!$client instanceof Client){ |
108
|
|
|
$client = new Client(); |
109
|
|
|
} |
110
|
9 |
|
return $client; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Executes a CURL request to the Google API |
115
|
|
|
* @param string $url The URL to shorten |
116
|
|
|
* @return mixed The response data |
117
|
|
|
*/ |
118
|
9 |
|
protected function exec($url) |
119
|
|
|
{ |
120
|
9 |
|
$client = $this->getRequest(); |
121
|
|
|
try{ |
122
|
9 |
|
$response = $client->request('POST',$this->buildRequestUrl(),[ |
123
|
|
|
'json' => [ |
124
|
|
|
'longUrl' => $url |
125
|
9 |
|
] |
126
|
9 |
|
]); |
127
|
9 |
|
} catch (\GuzzleHttp\Exception\ClientException $e){ |
128
|
|
|
$response = $e->getResponse(); |
129
|
|
|
} |
130
|
9 |
|
return $this->handleResponse($response->getBody()); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of
return
,die
orexit
statements that have been added for debug purposes.In the above example, the last
return false
will never be executed, because a return statement has already been met in every possible execution path.