1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* OpenWeatherMap-PHP-API — A PHP API to parse weather data from https://OpenWeatherMap.org. |
5
|
|
|
* |
6
|
|
|
* @license MIT |
7
|
|
|
* |
8
|
|
|
* Please see the LICENSE file distributed with this source code for further |
9
|
|
|
* information regarding copyright and licensing. |
10
|
|
|
* |
11
|
|
|
* Please visit the following links to read about the usage policies and the license of |
12
|
|
|
* OpenWeatherMap data before using this library: |
13
|
|
|
* |
14
|
|
|
* @see https://OpenWeatherMap.org/price |
15
|
|
|
* @see https://OpenWeatherMap.org/terms |
16
|
|
|
* @see https://OpenWeatherMap.org/appid |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace Cmfcmf\OpenWeatherMap\Tests; |
20
|
|
|
|
21
|
|
|
use Psr\Http\Client\ClientInterface; |
22
|
|
|
use Psr\Http\Message\RequestInterface; |
23
|
|
|
use Psr\Http\Message\ResponseInterface; |
24
|
|
|
use Http\Factory\Guzzle\ResponseFactory; |
25
|
|
|
use GuzzleHttp\Psr7; |
26
|
|
|
|
27
|
|
|
class TestHttpClient implements ClientInterface |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var ResponseFactory |
31
|
|
|
*/ |
32
|
|
|
private $responseFactory; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var int |
36
|
|
|
*/ |
37
|
|
|
private $errorWithStatusCode; |
38
|
|
|
|
39
|
|
|
public function __construct() |
40
|
|
|
{ |
41
|
|
|
$this->responseFactory = new ResponseFactory(); |
42
|
|
|
$this->errorWithStatusCode = null; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function sendRequest(RequestInterface $request): ResponseInterface |
46
|
|
|
{ |
47
|
|
|
$url = $request->getUri(); |
48
|
|
|
$format = strpos($url, 'json') !== false ? 'json' : 'xml'; |
49
|
|
|
$content = ""; |
|
|
|
|
50
|
|
|
if (strpos($url, 'forecast') !== false) { |
51
|
|
|
$content = $this->forecast($format); |
52
|
|
|
} elseif (strpos($url, 'group') !== false) { |
53
|
|
|
$content = $this->group($format); |
54
|
|
|
} else { |
55
|
|
|
$content = $this->currentWeather($format); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$response = $this->responseFactory->createResponse($this->errorWithStatusCode !== null ? $this->errorWithStatusCode : 200); |
59
|
|
|
$this->errorWithStatusCode = null; |
60
|
|
|
return $response->withBody(Psr7\stream_for($content)); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function returnErrorForNextRequest($code) |
64
|
|
|
{ |
65
|
|
|
$this->errorWithStatusCode = $code; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
private function currentWeather($format) |
69
|
|
|
{ |
70
|
|
|
if ($format == 'xml') { |
71
|
|
|
return FakeData::CURRENT_WEATHER_XML; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
private function forecast($format) |
76
|
|
|
{ |
77
|
|
|
if ($format == 'xml') { |
78
|
|
|
return FakeData::forecastXML(); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
private function group($format) |
83
|
|
|
{ |
84
|
|
|
if ($format == 'json') { |
85
|
|
|
return FakeData::WEATHER_GROUP_JSON; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.