1
|
|
|
<?php |
2
|
|
|
namespace Dwr\OpenWeather; |
3
|
|
|
|
4
|
|
|
use Dwr\OpenWeather\Factory\RequestFactory; |
5
|
|
|
use Dwr\OpenWeather\Factory\ResponseFactory; |
6
|
|
|
use Dwr\OpenWeather\Response\ResponseInterface; |
7
|
|
|
use GuzzleHttp\Client; |
8
|
|
|
use GuzzleHttp\Psr7\Response; |
9
|
|
|
|
10
|
|
|
class OpenWeather implements OpenWeatherInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var Configuration |
14
|
|
|
*/ |
15
|
|
|
private $config; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var Client |
19
|
|
|
*/ |
20
|
|
|
private $client; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
private $type; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
private $supportedType = ['Weather', 'Forecast']; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* OpenWeather constructor. |
34
|
|
|
* @param string $type |
35
|
|
|
* @param Configuration $config |
36
|
|
|
*/ |
37
|
|
|
public function __construct($type, Configuration $config) |
38
|
|
|
{ |
39
|
|
|
if (! $this->isType($type)) { |
40
|
|
|
throw new \InvalidArgumentException( |
41
|
|
|
'Unknown OpenWeather type. Supported types are: ' . implode(', ', $this->supportedType) |
42
|
|
|
); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$this->type = $type; |
46
|
|
|
$this->config = $config; |
47
|
|
|
$this->client = $config->getHttpClient(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param string $type |
52
|
|
|
* @return bool |
53
|
|
|
*/ |
54
|
|
|
private function isType($type) |
55
|
|
|
{ |
56
|
|
|
if (isset($type) |
57
|
|
|
&& ! empty($type) |
58
|
|
|
&& in_array($type, $this->supportedType) |
59
|
|
|
) { |
60
|
|
|
return true; |
61
|
|
|
} |
62
|
|
|
return false; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return array |
67
|
|
|
*/ |
68
|
|
|
public function getSupportedType() |
69
|
|
|
{ |
70
|
|
|
return $this->supportedType; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param string $cityName |
75
|
|
|
* @return ResponseInterface |
76
|
|
|
*/ |
77
|
|
|
public function getByCityName($cityName) |
78
|
|
|
{ |
79
|
|
|
return $this->request(['query' => ['q' => $cityName]]); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param string $cityId |
84
|
|
|
* @return ResponseInterface |
85
|
|
|
*/ |
86
|
|
|
public function getByCityId($cityId) |
87
|
|
|
{ |
88
|
|
|
return $this->request(['query' => ['id' => (int)$cityId]]); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param int $lat |
93
|
|
|
* @param int $lon |
94
|
|
|
* @return ResponseInterface |
95
|
|
|
*/ |
96
|
|
|
public function getByGeographicCoordinates($lon, $lat) |
97
|
|
|
{ |
98
|
|
|
return $this->request(['query' => ['lon' => $lon, 'lat' => $lat]]); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param array $parameters |
103
|
|
|
* @return ResponseInterface |
104
|
|
|
*/ |
105
|
|
|
private function request(array $parameters) |
106
|
|
|
{ |
107
|
|
|
$parameters['query']['appid'] = $this->config->apiKey(); |
108
|
|
|
$data = $this->client->get($this->buildUri($this->type), $parameters); |
109
|
|
|
|
110
|
|
|
return $this->response($this->type, $data); |
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param $requestType |
115
|
|
|
* @return string |
116
|
|
|
*/ |
117
|
|
|
private function buildUri($requestType) |
118
|
|
|
{ |
119
|
|
|
$request = new RequestFactory($requestType); |
120
|
|
|
return $this->config->version() . $request->create()->getUri(); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param $responseType |
125
|
|
|
* @param Response $data |
126
|
|
|
* @return ResponseInterface |
127
|
|
|
*/ |
128
|
|
|
private function response($responseType, Response $data) |
129
|
|
|
{ |
130
|
|
|
$response = new ResponseFactory($responseType); |
131
|
|
|
return $response->create($data->getBody()); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.