1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Sunlight-php |
4
|
|
|
* |
5
|
|
|
* PHP Version 7.0.1 |
6
|
|
|
* |
7
|
|
|
* @category Sunlight_Foundation_API |
8
|
|
|
* @package ContactMyReps |
9
|
|
|
* @author edfialk <[email protected]> |
10
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
11
|
|
|
* @link https://github.com/contactmyreps/sunlight-php |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace ContactMyReps\Sunlight\api; |
15
|
|
|
|
16
|
|
|
use GuzzleHttp\Client; |
17
|
|
|
use GuzzleHttp\Exception\RequestException; |
18
|
|
|
use Psr\Http\Message\ResponseInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Sunlight Foundation Base API |
22
|
|
|
* |
23
|
|
|
* @category Sunlight_Foundation_API |
24
|
|
|
* @package ContactMyReps |
25
|
|
|
* @author edfialk <[email protected]> |
26
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
27
|
|
|
* @link https://github.com/contactmyreps/sunlight-php |
28
|
|
|
*/ |
29
|
|
|
abstract class BaseAPI |
30
|
|
|
{ |
31
|
|
|
|
32
|
|
|
public $options = []; |
33
|
|
|
|
34
|
|
|
private static $instance; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Create api |
38
|
|
|
* |
39
|
|
|
* @param array $options local api options |
40
|
|
|
*/ |
41
|
|
|
public function __construct($options = []) |
42
|
|
|
{ |
43
|
|
|
$this->options = array_merge($this->options, $options); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Set or retrieve option value |
48
|
|
|
* |
49
|
|
|
* @param string or array $key the option key to set/retrieve, or an array of options to set |
50
|
|
|
* @param object $value value to store |
51
|
|
|
* |
52
|
|
|
* @return object if key is string and value is null, return options[key] value |
53
|
|
|
*/ |
54
|
|
View Code Duplication |
public function option($key, $value = null) |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
if (is_array($key)) { |
57
|
|
|
$this->options = array_merge($this->options, $key); |
58
|
|
|
return; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if (null === $value) { |
62
|
|
|
return $this->options[$key] ?? null; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$this->options[$key] = $value; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Submit asynchronous get request |
70
|
|
|
* |
71
|
|
|
* @param string $url request url |
72
|
|
|
* @param array $fields response fields |
73
|
|
|
* |
74
|
|
|
* @return GuzzleHttp\Promise Request promise |
75
|
|
|
*/ |
76
|
|
|
public function getAsync($url, $fields = []) |
77
|
|
|
{ |
78
|
|
|
$url = $this->addFieldsToUrl($fields, $url); |
79
|
|
|
|
80
|
|
|
return $this->getClient()->getAsync($url)->then( |
81
|
|
|
function (ResponseInterface $response) { |
82
|
|
|
return $this->format($response); |
|
|
|
|
83
|
|
|
}, |
84
|
|
|
function (RequestException $e) { |
85
|
|
|
echo "INSIDE BASE ASYNC EXCEPTION\n"; |
86
|
|
|
return $e; |
87
|
|
|
} |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Submit get request |
93
|
|
|
* |
94
|
|
|
* @param string $url request url |
95
|
|
|
* @param array $fields response fields |
96
|
|
|
* |
97
|
|
|
* @return GuzzleHttp\Promise or json or array response |
98
|
|
|
*/ |
99
|
|
|
public function get($url, $fields = []) |
100
|
|
|
{ |
101
|
|
|
$url = $this->addFieldsToUrl($fields, $url); |
102
|
|
|
|
103
|
|
|
if ($this->option('async') === true) { |
104
|
|
|
return $this->getAsync($url); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$response = $this->getClient()->get($url); |
108
|
|
|
return $this->format($response); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Convert guzzle response to data |
113
|
|
|
* |
114
|
|
|
* @param Response $response GuzzleHttp\Response |
115
|
|
|
* |
116
|
|
|
* @return string or array data |
117
|
|
|
*/ |
118
|
|
|
public function format($response) |
119
|
|
|
{ |
120
|
|
|
$data = (string) $response->getBody(); |
121
|
|
|
|
122
|
|
|
if ($this->option('json') === true) { |
123
|
|
|
return $data; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return json_decode($data); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Get API instance |
131
|
|
|
* |
132
|
|
|
* @param string $key API class name |
133
|
|
|
* @param array $options API options |
134
|
|
|
* |
135
|
|
|
* @return object API instance |
136
|
|
|
*/ |
137
|
|
|
public static function getInstance($key, $options) |
138
|
|
|
{ |
139
|
|
|
$class = get_called_class(); |
140
|
|
|
if (!isset(self::$instance[$class])) { |
141
|
|
|
self::$instance[$class] = new static($options); |
142
|
|
|
self::$instance[$class]->setClient($key); |
143
|
|
|
} |
144
|
|
|
return self::$instance[$class]; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Copy display fields to url string |
149
|
|
|
* |
150
|
|
|
* @param array $fields response fields |
151
|
|
|
* @param string $url request url |
152
|
|
|
* |
153
|
|
|
* @return string url with ?fields=field1,field2 |
154
|
|
|
*/ |
155
|
|
|
public function addFieldsToUrl($fields, $url) |
156
|
|
|
{ |
157
|
|
|
if (count($fields) == 0) { |
158
|
|
|
return $url; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
$url .= strpos($url, "?") !== false ? "&" : "?"; |
162
|
|
|
$url .= "fields=".implode(",", $fields); |
163
|
|
|
return $url; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Create API Guzzle Client |
168
|
|
|
* |
169
|
|
|
* @param string $key Sunlight Foundation API key |
170
|
|
|
* |
171
|
|
|
* @return null |
172
|
|
|
*/ |
173
|
|
|
abstract protected function setClient($key); |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Get API Guzzle Client |
177
|
|
|
* |
178
|
|
|
* @return GuzzleHttp\Client API Client |
179
|
|
|
*/ |
180
|
|
|
abstract protected function getClient(); |
181
|
|
|
} |
182
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.