Weather   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 10
eloc 25
dl 0
loc 107
c 0
b 0
f 0
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setGuzzleOptions() 0 3 1
A getWeather() 0 35 5
A __construct() 0 3 1
A getLiveWeather() 0 3 1
A getHttpClient() 0 3 1
A getForecastsWeather() 0 3 1
1
<?php
2
3
namespace Run6\Weather;
4
5
use GuzzleHttp\Client;
6
use Run6\Weather\Exception\HttpException;
7
use Run6\Weather\Exception\InvalidArgumentException;
8
9
class Weather
10
{
11
    protected $key; // APIkey
12
13
    protected $guzzleOptions = []; // guzzle的配置
14
15
    public function __construct($key)
16
    {
17
        $this->key = $key;
18
    }
19
20
    /**
21
     * @param $city
22
     * @param string $type
23
     * @param string $format
24
     *
25
     * @return false|string
26
     *
27
     * @throws HttpException
28
     * @throws InvalidArgumentException
29
     */
30
    public function getWeather($city, $type = 'base', $format = 'json')
31
    {
32
        $url = 'https://restapi.amap.com/v3/weather/weatherInfo'; // 接口url地址
33
34
        // 1.对 `$format` 和 `$type` 参数进行检查,不在范围内的抛出异常
35
        if (!in_array(strtolower($format), ['xml', 'json'])) {
36
            throw new InvalidArgumentException('Invalid response format: '.$format);
37
        }
38
39
        if (!in_array(strtolower($type), ['base', 'all'])) {
40
            throw new InvalidArgumentException('Invalid type value(base/all): '.$type);
41
        }
42
43
        // 2.构建查询参数,对空值过滤
44
        $query = array_filter([
45
            'key' => $this->key,
46
            'city' => $city,
47
            'output' => strtolower($format),
48
            'extensions' => strtolower($type),
49
        ]);
50
51
        try {
52
            // 3. 调用 `getHttpClient` 方法获取 `HTTP` 请求实例,并调用 `get`方法
53
            // 请求 `API` 接口
54
            $response = $this->getHttpClient()->get($url, [
55
                'query' => $query,
56
            ])->getBody()->getContents();
57
58
            // 4. 返回值根据 `$format` 返回不同的格式
59
            // 当 `$format` 为 `json` 时,返回数组,否则为 `XML`。
60
            return 'json' === $format ? json_decode($response, true) : $response;
61
        } catch (\Exception $e) {
62
            // 5. 当调用出现异常时捕获并抛出,消息为捕获到的异常消息,
63
            // 并将调用异常作为 $previousException 传入。
64
            throw new HttpException($e->getMessage(), $e->getCode(), $e);
65
        }
66
    }
67
68
    /**
69
     * 获取实时天气信息.
70
     *
71
     * @param $city
72
     * @param string $format
73
     *
74
     * @return false|string
75
     *
76
     * @throws HttpException
77
     * @throws InvalidArgumentException
78
     */
79
    public function getLiveWeather($city, $format = 'json')
80
    {
81
        return $this->getWeather($city, 'base', $format);
82
    }
83
84
    /**
85
     * 获取天气预报.
86
     *
87
     * @param $city
88
     * @param string $format
89
     *
90
     * @return false|string
91
     *
92
     * @throws HttpException
93
     * @throws InvalidArgumentException
94
     */
95
    public function getForecastsWeather($city, $format = 'json')
96
    {
97
        return $this->getWeather($city, 'all', $format);
98
    }
99
100
    /**
101
     * 返回guzzle的HTTP实例.
102
     *
103
     * @return Client
104
     */
105
    public function getHttpClient()
106
    {
107
        return new Client($this->guzzleOptions);
108
    }
109
110
    /**
111
     * @param array $guzzleOptions
112
     */
113
    public function setGuzzleOptions(array $guzzleOptions)
114
    {
115
        $this->guzzleOptions = $guzzleOptions;
116
    }
117
}
118