|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the Teazee package. |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
* |
|
8
|
|
|
* @license MIT License |
|
9
|
|
|
*/ |
|
10
|
|
|
namespace Teazee\Provider; |
|
11
|
|
|
|
|
12
|
|
|
use Http\Client\HttpClient; |
|
13
|
|
|
use Http\Message\MessageFactory; |
|
14
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
15
|
|
|
use Psr\Http\Message\UriInterface; |
|
16
|
|
|
use RuntimeException; |
|
17
|
|
|
use Teazee\Exception\ServiceMissingException; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @author Michael Crumm <[email protected]> |
|
21
|
|
|
*/ |
|
22
|
|
|
abstract class AbstractHttpProvider extends AbstractProvider |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var HttpClient |
|
26
|
|
|
*/ |
|
27
|
|
|
private $client; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var MessageFactory |
|
31
|
|
|
*/ |
|
32
|
|
|
private $messageFactory; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* AbstractHttpProvider Constructor. |
|
36
|
|
|
* |
|
37
|
|
|
* @param HttpClient $client HttpClient makes HTTP requests. |
|
38
|
|
|
* @param MessageFactory $messageFactory MessageFactory creates Request objects. |
|
39
|
|
|
*/ |
|
40
|
|
|
public function __construct(HttpClient $client = null, MessageFactory $messageFactory = null) |
|
41
|
|
|
{ |
|
42
|
|
|
if (null === $client && !class_exists('Http\Discovery\HttpClientDiscovery')) { |
|
43
|
2 |
|
throw ServiceMissingException::noHttpClient(); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
23 |
|
$this->client = $client ?: \Http\Discovery\HttpClientDiscovery::find(); |
|
47
|
|
|
|
|
48
|
|
|
if (null === $messageFactory && !class_exists('Http\Discovery\MessageFactoryDiscovery')) { |
|
49
|
2 |
|
throw ServiceMissingException::noMessageFactory(); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
23 |
|
$this->messageFactory = $messageFactory ?: \Http\Discovery\MessageFactoryDiscovery::find(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Returns a ResponseInterface for the given URI/method. |
|
57
|
|
|
* |
|
58
|
|
|
* @param string|UriInterface $uri Request URI. |
|
59
|
|
|
* @param string $method HTTP method (Defaults to 'GET'). |
|
60
|
|
|
* |
|
61
|
|
|
* @return ResponseInterface |
|
62
|
|
|
*/ |
|
63
|
|
|
protected function getResponse($uri, $method = 'GET') |
|
64
|
|
|
{ |
|
65
|
9 |
|
$request = $this->messageFactory->createRequest($method, $uri); |
|
66
|
|
|
|
|
67
|
9 |
|
return $this->client->sendRequest($request); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param string|float $lat Coordinate latitude. |
|
72
|
|
|
* @param string|float $lng Coordinate longitude. |
|
73
|
|
|
* @param int|null $timestamp Seconds since Jan 1, 1970 UTC. |
|
74
|
|
|
* |
|
75
|
|
|
* @throws RuntimeException When the response body cannot be decoded. |
|
76
|
|
|
* |
|
77
|
|
|
* @return object |
|
78
|
|
|
*/ |
|
79
|
|
|
protected function getResult($lat, $lng, $timestamp = null) |
|
80
|
|
|
{ |
|
81
|
9 |
|
$query = $this->buildUri($lat, $lng, $timestamp); |
|
82
|
9 |
|
$response = $this->getResponse($query); |
|
83
|
|
|
|
|
84
|
9 |
|
$data = json_decode($response->getBody()->getContents()); |
|
85
|
|
|
|
|
86
|
|
|
if (JSON_ERROR_NONE !== json_last_error()) { |
|
87
|
|
|
throw new RuntimeException((string) json_last_error_msg()); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
9 |
|
return $data; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Returns the URI for the specified location and timestamp. |
|
95
|
|
|
* |
|
96
|
|
|
* @param string|float $lat Coordinate latitude. |
|
97
|
|
|
* @param string|float $lng Coordinate longitude. |
|
98
|
|
|
* @param int|null $timestamp Seconds since Jan 1, 1970 UTC. |
|
99
|
|
|
* |
|
100
|
|
|
* @return string|UriInterface |
|
101
|
|
|
*/ |
|
102
|
|
|
abstract protected function buildUri($lat, $lng, $timestamp = null); |
|
103
|
|
|
} |
|
104
|
|
|
|