1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Vinelab\UrlShortener\Drivers; |
4
|
|
|
|
5
|
|
|
use Vinelab\UrlShortener\Base\DriversAbstract; |
6
|
|
|
use Vinelab\UrlShortener\Contracts\DriverInterface; |
7
|
|
|
use Vinelab\UrlShortener\Validators\ValidatorTrait; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class Bitly the driver of the Bitly URL shortener provider. |
11
|
|
|
* |
12
|
|
|
* @category Bitly Driver |
13
|
|
|
* |
14
|
|
|
* @author Mahmoud Zalt <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
class Bitly extends DriversAbstract implements DriverInterface |
17
|
|
|
{ |
18
|
|
|
use ValidatorTrait; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* the response data format. |
22
|
|
|
*/ |
23
|
|
|
const RESPONSE_FORMAT = 'json'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* the bitly API domain. |
27
|
|
|
*/ |
28
|
|
|
protected $domain; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* bitly endpoint to shorten URL. |
32
|
|
|
*/ |
33
|
|
|
protected $endpoint; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* bitly app access token collected from the config file. |
37
|
|
|
* |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
private $access_token; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param array $parameters |
44
|
|
|
* @param Object $httpClient |
45
|
|
|
*/ |
46
|
|
|
public function __construct($parameters, $httpClient = null) |
47
|
|
|
{ |
48
|
|
|
// must call the constructor of the abstracted class (the Client) |
49
|
|
|
parent::__construct(); |
50
|
|
|
|
51
|
|
|
// set the HTTP client |
52
|
|
|
$this->setClient($httpClient); |
53
|
|
|
|
54
|
|
|
// read the configuration parameters and set them as attributes of this class |
55
|
|
|
$this->setParameters($parameters); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* set the attributes on the class after validating them. |
60
|
|
|
* |
61
|
|
|
* @param $parameters |
62
|
|
|
*/ |
63
|
|
|
private function setParameters($parameters) |
64
|
|
|
{ |
65
|
|
|
$this->domain = $this->validateConfiguration($parameters['domain']); |
66
|
|
|
$this->endpoint = $this->validateConfiguration($parameters['endpoint']); |
67
|
|
|
$this->access_token = $this->validateConfiguration($parameters['token']); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* shorten the input $url and return the shorted version. |
72
|
|
|
* |
73
|
|
|
* @param $url |
74
|
|
|
* |
75
|
|
|
* @return mixed |
76
|
|
|
*/ |
77
|
|
|
public function shorten($url) |
78
|
|
|
{ |
79
|
|
|
// make the API call through the extended client |
80
|
|
|
$response = $this->fetchUrl($this->url(), $this->parameters($url)); |
81
|
|
|
|
82
|
|
|
// read the shorted url from the response object |
83
|
|
|
$shorter_url = $this->parse($response); |
84
|
|
|
|
85
|
|
|
return $shorter_url; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Build the request parameters. |
90
|
|
|
* |
91
|
|
|
* @param $url |
92
|
|
|
* |
93
|
|
|
* @return array |
94
|
|
|
*/ |
95
|
|
|
protected function parameters($url) |
96
|
|
|
{ |
97
|
|
|
return [ |
98
|
|
|
'access_token' => $this->access_token, |
99
|
|
|
'format' => self::RESPONSE_FORMAT, |
100
|
|
|
'longUrl' => urlencode($url), |
101
|
|
|
]; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* get the bitly shorten URL. |
106
|
|
|
* |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
|
|
protected function url() |
110
|
|
|
{ |
111
|
|
|
return $this->domain.$this->endpoint; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* get a response object and return the short URL form the result. |
116
|
|
|
* |
117
|
|
|
* @param $response_object |
118
|
|
|
* |
119
|
|
|
* @return mixed |
120
|
|
|
* |
121
|
|
|
* @throws \Vinelab\UrlShortener\Validators\ResponseErrorException |
122
|
|
|
*/ |
123
|
|
|
private function parse($response_object) |
124
|
|
|
{ |
125
|
|
|
// will throw an exception if not valid |
126
|
|
|
$this->validateResponseCode($response_object->status_code); |
127
|
|
|
|
128
|
|
|
// return only the short generated url |
129
|
|
|
return $response_object->data->url; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|