1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Phpsa\LaravelYourlsPlugin; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
|
7
|
|
|
class LaravelYourlsPlugin |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Athentication params. |
11
|
|
|
* |
12
|
|
|
* @var array |
13
|
|
|
*/ |
14
|
|
|
protected $authParam; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Request Client. |
18
|
|
|
* |
19
|
|
|
* @var Client::class |
20
|
|
|
*/ |
21
|
|
|
protected $client; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Available file formats to comunicate with Yourls API. |
25
|
|
|
* |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
private $formats = ['json', 'xml', 'simple']; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Available filters for statistics. |
32
|
|
|
* |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
private $filters = ['top', 'bottom', 'rand', 'last']; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Response format. |
39
|
|
|
* |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
public $format = 'json'; |
43
|
|
|
/** |
44
|
|
|
* Response filter for stats. |
45
|
|
|
* |
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
public $filter = 'top'; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Last request. |
52
|
|
|
* |
53
|
|
|
* @var Response |
54
|
|
|
*/ |
55
|
|
|
protected $lastResponse; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Construct our instance. |
59
|
|
|
* |
60
|
|
|
* @param array $configs |
61
|
|
|
*/ |
62
|
|
|
public function __construct(array $configs) |
63
|
|
|
{ |
64
|
|
|
$this->format = $configs['format']; |
65
|
|
|
$this->authParam = $this->getRequestAuthentication($configs); |
66
|
|
|
$this->client = new Client(['base_uri' => $configs['url']]); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* setup our authentication params. |
71
|
|
|
* |
72
|
|
|
* @param array $config |
73
|
|
|
* |
74
|
|
|
* @return array array for authentication |
75
|
|
|
*/ |
76
|
|
|
protected function getRequestAuthentication(array $config) |
77
|
|
|
{ |
78
|
|
|
if ($config['signature']) { |
79
|
|
|
return ['signature' => $config['signature']]; |
80
|
|
|
} else { |
81
|
|
|
return [ |
82
|
|
|
'username' => $config['username'], |
83
|
|
|
'password' => $config['password'], |
84
|
|
|
]; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Get short URL for a link. |
90
|
|
|
* |
91
|
|
|
* @param string $url to shorten |
92
|
|
|
* @param string $title title for url |
93
|
|
|
* @param string $keyword [optional] for custom short URLs |
94
|
|
|
* @param string $format [optional] either "json" or "xml" |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
|
|
public function shorturl(string $url, string $title = null, string $keyword = null, string $format = null) |
98
|
|
|
{ |
99
|
|
|
$params = [ |
100
|
|
|
'action' => 'shorturl', |
101
|
|
|
'url' => $url, |
102
|
|
|
'format' => $this->setFormat($format), |
103
|
|
|
]; |
104
|
|
|
if ($title) { |
105
|
|
|
$params['title'] = $title; |
106
|
|
|
} |
107
|
|
|
if ($keyword) { |
108
|
|
|
$params['keyword'] = $keyword; |
109
|
|
|
} |
110
|
|
|
$body = $this->process($params); |
111
|
|
|
|
112
|
|
|
return $body->shorturl; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Get long URL of a shorturl. |
117
|
|
|
* |
118
|
|
|
* @param string $shorturl to expand (can be either 'abc' or 'http://site/abc') |
119
|
|
|
* @param string $format [optional] either "json" or "xml" |
120
|
|
|
* @return Response |
121
|
|
|
*/ |
122
|
|
|
public function expand(string $shorturl, string $format = null) |
123
|
|
|
{ |
124
|
|
|
$params = [ |
125
|
|
|
'action' => 'expand', |
126
|
|
|
'shorturl' => $shorturl, |
127
|
|
|
'format' => $this->setFormat($format), |
128
|
|
|
]; |
129
|
|
|
|
130
|
|
|
return $this->process($params); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Get stats about one short URL. |
135
|
|
|
* |
136
|
|
|
* @param string $shorturl for which to get stats (can be either 'abc' or 'http://site/abc') |
137
|
|
|
* @param string $format [optional] either "json" or "xml" |
138
|
|
|
* @return Response |
139
|
|
|
*/ |
140
|
|
|
public function urlStats(string $shorturl, string $format = null) |
141
|
|
|
{ |
142
|
|
|
$params = [ |
143
|
|
|
'action' => 'url-stats', |
144
|
|
|
'shorturl' => $shorturl, |
145
|
|
|
'format' => $this->setFormat($format), |
146
|
|
|
]; |
147
|
|
|
|
148
|
|
|
return $this->process($params); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Get stats about your links. |
153
|
|
|
* |
154
|
|
|
* @param string $filter [optional] either "top", "bottom" , "rand" or "last" |
155
|
|
|
* @param int [optional] $limit maximum number of links to return |
156
|
|
|
* @param string $format [optional] either "json" or "xml" |
157
|
|
|
* @return Response |
158
|
|
|
*/ |
159
|
|
|
public function stats(string $filter = null, int $limit = null, string $format = null) |
160
|
|
|
{ |
161
|
|
|
$filter = empty($filter) || ! in_array($filter, $this->filters) ? $this->filter : $filter; |
162
|
|
|
|
163
|
|
|
$params = [ |
164
|
|
|
'action' => 'stats', |
165
|
|
|
'filter' => $filter, |
166
|
|
|
'format' => $this->setFormat($format), |
167
|
|
|
]; |
168
|
|
|
if (! empty($limit)) { |
169
|
|
|
$params = array_merge($params, ['limit' => $limit]); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
return $this->process($params); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Get database stats. |
177
|
|
|
* |
178
|
|
|
* @param string $format [optional] either "json" or "xml" |
179
|
|
|
* @return Response |
180
|
|
|
*/ |
181
|
|
|
public function dbStats(string $format = null) |
182
|
|
|
{ |
183
|
|
|
$params = [ |
184
|
|
|
'action' => 'db-stats', |
185
|
|
|
'format' => $this->setFormat($format), |
186
|
|
|
]; |
187
|
|
|
|
188
|
|
|
return $this->process($params); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* processes our request. |
193
|
|
|
* |
194
|
|
|
* @param array $request |
195
|
|
|
* |
196
|
|
|
* @throws \Exception |
197
|
|
|
* |
198
|
|
|
* @return Response |
199
|
|
|
*/ |
200
|
|
|
protected function process(array $request) |
201
|
|
|
{ |
202
|
|
|
$form_params = array_merge($request, $this->authParam); |
203
|
|
|
|
204
|
|
|
$result = $this->client->request('POST', 'yourls-api.php', ['form_params' => $form_params]); |
205
|
|
|
if (! $result || '200' != $result->getStatusCode()) { |
206
|
|
|
throw new \Exception('Failed to process request'); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
$body = $result->getBody(); |
210
|
|
|
$this->lastResponse = new Response($body, $request['format']); |
211
|
|
|
|
212
|
|
|
return $this->lastResponse; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Returns the result of the last request in. |
217
|
|
|
* |
218
|
|
|
* @return Response |
219
|
|
|
*/ |
220
|
|
|
public function getLastResponse() |
221
|
|
|
{ |
222
|
|
|
return $this->lastResponse; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* checks the format is of a correct value else defaults to the default format. |
227
|
|
|
* |
228
|
|
|
* @param string $format |
229
|
|
|
* |
230
|
|
|
* @return string |
231
|
|
|
*/ |
232
|
|
|
protected function setFormat(string $format = null) |
233
|
|
|
{ |
234
|
|
|
return empty($format) || ! in_array($format, $this->formats) ? $this->format : $format; |
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
|