Completed
Pull Request — master (#134)
by Christian
12:22
created

TestHttpClient::currentWeather()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
/**
3
 * OpenWeatherMap-PHP-API — A php api to parse weather data from http://www.OpenWeatherMap.org .
4
 *
5
 * @license MIT
6
 *
7
 * Please see the LICENSE file distributed with this source code for further
8
 * information regarding copyright and licensing.
9
 *
10
 * Please visit the following links to read about the usage policies and the license of
11
 * OpenWeatherMap before using this class:
12
 *
13
 * @see http://www.OpenWeatherMap.org
14
 * @see http://www.OpenWeatherMap.org/terms
15
 * @see http://openweathermap.org/appid
16
 */
17
18
namespace Cmfcmf\OpenWeatherMap\Tests;
19
20
use Psr\Http\Client\ClientInterface;
21
use Psr\Http\Message\RequestInterface;
22
use Psr\Http\Message\ResponseInterface;
23
use Http\Factory\Guzzle\ResponseFactory;
24
use GuzzleHttp\Psr7;
25
26
class TestHttpClient implements ClientInterface
27
{
28
    /**
29
     * @var ResponseFactory
30
     */
31
    private $responseFactory;
32
33
    /**
34
     * @var int
35
     */
36
    private $errorWithStatusCode;
37
38
    public function __construct()
39
    {
40
        $this->responseFactory = new ResponseFactory();
41
        $this->errorWithStatusCode = null;
42
    }
43
44
    public function sendRequest(RequestInterface $request): ResponseInterface
45
    {
46
        $url = $request->getUri();
47
        $format = strpos($url, 'json') !== false ? 'json' : 'xml';
48
        $content = "";
0 ignored issues
show
Unused Code introduced by
$content is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

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.

Loading history...
49
        if (strpos($url, 'forecast') !== false) {
50
            $content = $this->forecast($format);
51
        } elseif (strpos($url, 'group') !== false) {
52
            $content = $this->group($format);
53
        } else {
54
            $content = $this->currentWeather($format);
55
        }
56
57
        $response = $this->responseFactory->createResponse($this->errorWithStatusCode !== null ? $this->errorWithStatusCode : 200);
58
        $this->errorWithStatusCode = null;
59
        return $response->withBody(Psr7\stream_for($content));
60
    }
61
62
    public function returnErrorForNextRequest($code)
63
    {
64
        $this->errorWithStatusCode = $code;
65
    }
66
67
    private function currentWeather($format)
68
    {
69
        if ($format == 'xml') {
70
            return FakeData::CURRENT_WEATHER_XML;
71
        }
72
    }
73
74
    private function forecast($format)
75
    {
76
        if ($format == 'xml') {
77
            return FakeData::forecastXML();
78
        }
79
    }
80
81
    private function group($format)
82
    {
83
        if ($format == 'json') {
84
            return FakeData::WEATHER_GROUP_JSON;
85
        }
86
    }
87
}
88