|
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\Shorten; |
|
16
|
|
|
use badams\GoogleUrl\Exceptions\GoogleUrlException; |
|
17
|
|
|
use badams\GoogleUrl\Exceptions\InvalidKeyException; |
|
18
|
|
|
use badams\GoogleUrl\Exceptions\InvalidValueException; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Class GoogleUrl |
|
22
|
|
|
* @package badams\GoogleUrl |
|
23
|
|
|
*/ |
|
24
|
|
|
class GoogleUrl |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* |
|
28
|
|
|
*/ |
|
29
|
|
|
const BASE_URL = 'https://www.googleapis.com/urlshortener/v1/url'; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var \GuzzleHttp\ClientInterface |
|
33
|
|
|
*/ |
|
34
|
|
|
private $http; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var |
|
38
|
|
|
*/ |
|
39
|
|
|
private $key; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* GoogleUrl constructor. |
|
43
|
|
|
* @param $key |
|
44
|
|
|
* @param \GuzzleHttp\ClientInterface|null $httpClient |
|
45
|
|
|
*/ |
|
46
|
|
|
public function __construct($key, \GuzzleHttp\ClientInterface $httpClient = null) |
|
47
|
|
|
{ |
|
48
|
|
|
if (is_null($httpClient)) { |
|
49
|
|
|
$httpClient = new \GuzzleHttp\Client(); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
$this->setKey($key); |
|
53
|
|
|
$this->http = $httpClient; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param $key |
|
58
|
|
|
*/ |
|
59
|
|
|
public function setKey($key) |
|
60
|
|
|
{ |
|
61
|
|
|
$this->key = $key; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param ActionInterface $method |
|
66
|
|
|
* @return \GuzzleHttp\Message\Request|\GuzzleHttp\Message\RequestInterface |
|
67
|
|
|
*/ |
|
68
|
|
|
private function createRequest(ActionInterface $method) |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->http->createRequest( |
|
71
|
|
|
$method->getRequestMethod(), |
|
72
|
|
|
self::BASE_URL, |
|
73
|
|
|
array_merge([ |
|
74
|
|
|
'exceptions' => false, |
|
75
|
|
|
'headers' => ['Content-Type' => 'application/json'], |
|
76
|
|
|
'query' => ['key' => $this->key], |
|
77
|
|
|
], $method->getRequestOptions()) |
|
78
|
|
|
); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param ActionInterface $method |
|
83
|
|
|
* @return mixed |
|
84
|
|
|
*/ |
|
85
|
|
|
private function execute(ActionInterface $method) |
|
86
|
|
|
{ |
|
87
|
|
|
$response = $this->http->send($this->createRequest($method)); |
|
88
|
|
|
|
|
89
|
|
|
if ($response->getStatusCode() != 200) { |
|
90
|
|
|
$json = json_decode($response->getBody()->getContents()); |
|
91
|
|
|
if (isset($json->error->errors[0])) { |
|
92
|
|
|
$this->assertInvalidKey($json); |
|
93
|
|
|
$this->assertInvalidValue($json); |
|
94
|
|
|
} |
|
95
|
|
|
throw new GoogleUrlException($response->getBody()); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return $method->processResponse($response); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param $response |
|
103
|
|
|
* @throws InvalidKeyException |
|
104
|
|
|
*/ |
|
105
|
|
|
private function assertInvalidKey($response) |
|
106
|
|
|
{ |
|
107
|
|
|
if ($response->error->errors[0]->reason == 'keyInvalid') { |
|
108
|
|
|
throw new InvalidKeyException; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @param $response |
|
114
|
|
|
*/ |
|
115
|
|
|
private function assertInvalidValue($response) |
|
116
|
|
|
{ |
|
117
|
|
|
if ( |
|
118
|
|
|
$response->error->errors[0]->message == 'Invalid Value' && |
|
119
|
|
|
$response->error->errors[0]->locationType == 'parameter' |
|
120
|
|
|
) { |
|
121
|
|
|
throw new InvalidValueException($response->error->errors[0]->location); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @param $longUrl |
|
127
|
|
|
* @return UrlResource |
|
128
|
|
|
*/ |
|
129
|
|
|
public function shorten($longUrl) |
|
130
|
|
|
{ |
|
131
|
|
|
return $this->execute(new Shorten($longUrl)); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|