1 | <?php |
||
17 | class HttpClientLocationCreator |
||
18 | { |
||
19 | 1 | use LoggerAwareTrait; |
|
20 | |||
21 | /** |
||
22 | * @var ClientInterface |
||
23 | */ |
||
24 | protected $client; |
||
25 | |||
26 | |||
27 | /** |
||
28 | * @var RequestFactoryInterface |
||
29 | */ |
||
30 | public $request_factory; |
||
31 | |||
32 | |||
33 | /** |
||
34 | * Geocoder API endpoint. |
||
35 | * |
||
36 | * @var string |
||
37 | */ |
||
38 | public $api; |
||
39 | |||
40 | |||
41 | /** |
||
42 | * IP adress query parameter |
||
43 | * |
||
44 | * @var string |
||
45 | */ |
||
46 | public $ip_var_name = "ip"; |
||
47 | |||
48 | |||
49 | /** |
||
50 | * @var callable |
||
51 | */ |
||
52 | public $response_decoder; |
||
53 | |||
54 | |||
55 | /** |
||
56 | * @var mixed |
||
57 | */ |
||
58 | public $default_location; |
||
59 | |||
60 | |||
61 | /** |
||
62 | * @var string |
||
63 | */ |
||
64 | protected $error_loglevel = LogLevel::ERROR; |
||
65 | |||
66 | |||
67 | /** |
||
68 | * @param string $api Geocoder API endpoint |
||
69 | * @param ClientInterface $client PSR-18 HTTP Client |
||
70 | * @param RequestFactoryInterface $request_factory PSR-17 Request factory |
||
71 | * @param callable|null $response_decoder Optional: PSR-7 Response decoder callable |
||
72 | */ |
||
73 | 2 | public function __construct( string $api, ClientInterface $client, RequestFactoryInterface $request_factory, callable $response_decoder = null, LoggerInterface $logger = null) |
|
81 | |||
82 | |||
83 | |||
84 | /** |
||
85 | * @param string $client_ip Client IP address |
||
86 | */ |
||
87 | 2 | public function __invoke( string $client_ip ) |
|
108 | |||
109 | |||
110 | |||
111 | /** |
||
112 | * @param string $client_ip Client IP address |
||
113 | * @return RequestInterface |
||
114 | */ |
||
115 | 4 | public function createRequest( string $client_ip ) : RequestInterface |
|
124 | |||
125 | |||
126 | |||
127 | /** |
||
128 | * @param ResponseInterface $response |
||
129 | * @return mixed |
||
130 | */ |
||
131 | 4 | public function decodeResponse(ResponseInterface $response) |
|
137 | |||
138 | |||
139 | |||
140 | /** |
||
141 | * Sets the default location to return on error |
||
142 | * |
||
143 | * @param mixed $location |
||
144 | */ |
||
145 | 2 | public function setDefaultLocation( $location ) : self |
|
150 | |||
151 | |||
152 | |||
153 | /** |
||
154 | * Sets the API endpoint |
||
155 | * |
||
156 | * @param string $api |
||
157 | */ |
||
158 | 4 | public function setApiEndpoint( string $api ) : self |
|
163 | |||
164 | |||
165 | |||
166 | /** |
||
167 | * Sets the HTTP Client to use. |
||
168 | * |
||
169 | * @param ClientInterface $client |
||
170 | */ |
||
171 | 6 | public function setClient( ClientInterface $client ) : self |
|
176 | |||
177 | |||
178 | /** |
||
179 | * Sets the PSR-17 Request factory |
||
180 | * |
||
181 | * @param RequestFactoryInterface $request_factory |
||
182 | */ |
||
183 | 4 | public function setRequestFactory( RequestFactoryInterface $request_factory ) : self |
|
188 | |||
189 | |||
190 | /** |
||
191 | * Sets the PSR-7 Response decoder callable. |
||
192 | * |
||
193 | * @param callable $response_decoder |
||
194 | */ |
||
195 | 4 | public function setResponseDecoder( callable $response_decoder = null ) : self |
|
200 | |||
201 | } |
||
202 |