1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the badams\GoogleUrl library |
4
|
|
|
* |
5
|
|
|
* @license http://opensource.org/licenses/MIT |
6
|
|
|
* @link https://github.com/badams/google-url |
7
|
|
|
* @package badams/google-url |
8
|
|
|
* |
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
10
|
|
|
* file that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace badams\GoogleUrl; |
14
|
|
|
|
15
|
|
|
use badams\GoogleUrl\Actions\Expand; |
16
|
|
|
use badams\GoogleUrl\Actions\Shorten; |
17
|
|
|
use badams\GoogleUrl\Exceptions\GoogleUrlException; |
18
|
|
|
use badams\GoogleUrl\Exceptions\InvalidKeyException; |
19
|
|
|
use badams\GoogleUrl\Exceptions\InvalidValueException; |
20
|
|
|
use badams\GoogleUrl\Resources\Url; |
21
|
|
|
use GuzzleHttp\ClientInterface; |
22
|
|
|
use GuzzleHttp\Client; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Class GoogleUrl |
26
|
|
|
* @package badams\GoogleUrl |
27
|
|
|
*/ |
28
|
|
|
class GoogleUrl |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* Base API URL |
32
|
|
|
*/ |
33
|
|
|
const BASE_URL = 'https://www.googleapis.com/urlshortener/v1/url'; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var \GuzzleHttp\ClientInterface |
37
|
|
|
*/ |
38
|
|
|
private $http; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var |
42
|
|
|
*/ |
43
|
|
|
private $key; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* GoogleUrl constructor. |
47
|
|
|
* @param $key |
48
|
|
|
* @param \GuzzleHttp\ClientInterface|null $httpClient |
49
|
|
|
*/ |
50
|
21 |
|
public function __construct($key, ClientInterface $httpClient = null) |
51
|
|
|
{ |
52
|
21 |
|
if (null === $httpClient) { |
53
|
3 |
|
$httpClient = new Client(); |
54
|
2 |
|
} |
55
|
|
|
|
56
|
21 |
|
$this->setKey($key); |
57
|
21 |
|
$this->http = $httpClient; |
58
|
21 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param $key |
62
|
|
|
*/ |
63
|
21 |
|
public function setKey($key) |
64
|
|
|
{ |
65
|
21 |
|
$this->key = $key; |
66
|
21 |
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param ActionInterface $method |
70
|
|
|
* @return \GuzzleHttp\Message\Request|\GuzzleHttp\Message\RequestInterface |
71
|
|
|
*/ |
72
|
18 |
|
private function createRequest(ActionInterface $method) |
73
|
|
|
{ |
74
|
18 |
|
return $this->http->createRequest( |
75
|
18 |
|
$method->getRequestMethod(), |
76
|
18 |
|
self::BASE_URL, |
77
|
12 |
|
array_merge_recursive([ |
78
|
18 |
|
'exceptions' => false, |
79
|
12 |
|
'headers' => ['Content-Type' => 'application/json'], |
80
|
18 |
|
'query' => ['key' => $this->key], |
81
|
18 |
|
], $method->getRequestOptions()) |
82
|
12 |
|
); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param ActionInterface $method |
87
|
|
|
* @return mixed |
88
|
|
|
* @throws \badams\GoogleUrl\Exceptions\InvalidValueException |
89
|
|
|
* @throws \badams\GoogleUrl\Exceptions\GoogleUrlException |
90
|
|
|
* @throws \badams\GoogleUrl\Exceptions\InvalidKeyException |
91
|
|
|
* @throws GoogleUrlException |
92
|
|
|
*/ |
93
|
18 |
|
private function execute(ActionInterface $method) |
94
|
|
|
{ |
95
|
18 |
|
$response = $this->http->send($this->createRequest($method)); |
96
|
|
|
|
97
|
18 |
|
if ($response->getStatusCode() !== 200) { |
98
|
9 |
|
$json = json_decode($response->getBody()->getContents()); |
99
|
9 |
|
if (isset($json->error) && is_array($json->error->errors)) { |
100
|
6 |
|
$this->assertInvalidKey($json); |
101
|
3 |
|
$this->assertInvalidValue($json); |
102
|
|
|
} // @codeCoverageIgnore |
103
|
3 |
|
throw new GoogleUrlException($response->getBody()); |
104
|
|
|
} |
105
|
|
|
|
106
|
9 |
|
return $method->processResponse($response); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param $response |
111
|
|
|
* @throws InvalidKeyException |
112
|
|
|
*/ |
113
|
6 |
|
private function assertInvalidKey($response) |
114
|
|
|
{ |
115
|
6 |
|
if ($response->error->errors[0]->reason === 'keyInvalid') { |
116
|
3 |
|
throw new InvalidKeyException; |
117
|
|
|
} |
118
|
3 |
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param $response |
122
|
|
|
* @throws InvalidValueException |
123
|
|
|
* @throws \badams\GoogleUrl\Exceptions\InvalidValueException |
124
|
|
|
*/ |
125
|
3 |
|
private function assertInvalidValue($response) |
126
|
|
|
{ |
127
|
|
|
if ( |
128
|
3 |
|
$response->error->errors[0]->message === 'Invalid Value' && |
129
|
3 |
|
$response->error->errors[0]->locationType === 'parameter' |
130
|
2 |
|
) { |
131
|
3 |
|
throw new InvalidValueException($response->error->errors[0]->location); |
132
|
|
|
} |
133
|
|
|
} // @codeCoverageIgnore |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param $longUrl |
137
|
|
|
* @return Url |
138
|
|
|
* @throws \badams\GoogleUrl\Exceptions\InvalidValueException |
139
|
|
|
* @throws \badams\GoogleUrl\Exceptions\GoogleUrlException |
140
|
|
|
* @throws \badams\GoogleUrl\Exceptions\InvalidKeyException |
141
|
|
|
*/ |
142
|
12 |
|
public function shorten($longUrl) |
143
|
|
|
{ |
144
|
12 |
|
return $this->execute(new Shorten($longUrl)); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param $shortUrl |
149
|
|
|
* @param $projection |
150
|
|
|
* @return Url |
151
|
|
|
* @throws \badams\GoogleUrl\Exceptions\InvalidValueException |
152
|
|
|
* @throws \badams\GoogleUrl\Exceptions\GoogleUrlException |
153
|
|
|
* @throws \badams\GoogleUrl\Exceptions\InvalidKeyException |
154
|
|
|
*/ |
155
|
6 |
|
public function expand($shortUrl, $projection = null) |
156
|
|
|
{ |
157
|
6 |
|
return $this->execute(new Expand($shortUrl, $projection)); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|