Completed
Push — master ( acc482...035a10 )
by Jérémy
03:04
created

YahooWeatherAPI::getYql()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/*
4
 * (c) Jean-Baptiste Audebert <[email protected]>
5
 * (c) Jérémy Marodon         <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Th3Mouk\YahooWeatherAPI;
12
13
use Goutte\Client as GoutteClient;
14
use Th3Mouk\YahooWeatherAPI\Query\Query;
15
16
class YahooWeatherAPI implements YahooWeatherAPIInterface
17
{
18
    /**
19
     * @var Client Goutte client
20
     */
21
    protected $client;
22
23
    /**
24
     * @var array last response from API
25
     */
26
    protected $lastResponse;
27
28
    /**
29
     * @var string last woeid explicitly called
30
     */
31
    protected $woeid;
32
33
    /**
34
     * @var string last city name explicitly called
35
     */
36
    protected $city;
37
38
    /**
39
     * @var string the last url called
40
     */
41
    protected $yql;
42
43
    public function __construct()
44
    {
45
        $this->client = new GoutteClient();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Goutte\Client() of type object<Goutte\Client> is incompatible with the declared type object<Th3Mouk\YahooWeatherAPI\Client> of property $client.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 View Code Duplication
    public function callApiWoeid($woeid = null, $unit = 'c')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
52
    {
53
        if ($woeid === null && $this->woeid === null) {
54
            throw new \Exception('Please provide a woeid code', 400);
55
        }
56
57
        if ($woeid !== null) {
58
            $this->woeid = $woeid;
59
        }
60
61
        $this->yql = Query::URL_BASE.urlencode(sprintf(Query::WOEID_QUERY, $this->woeid, $unit));
62
63
        return $this->callApi();
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69 View Code Duplication
    public function callApiCityName($city = null, $unit = 'c')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
70
    {
71
        if ($city === null && $this->city === null) {
72
            throw new \Exception('Please provide a city\'s name', 400);
73
        }
74
75
        if ($city !== null) {
76
            $this->city = $city;
77
        }
78
79
        $this->yql = Query::URL_BASE.urlencode(sprintf(Query::CITY_NAME_QUERY, $this->city, $unit));
80
81
        return $this->callApi();
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    public function callApi($yql = null)
88
    {
89
        if ($yql === null && $this->yql) {
90
            throw new \Exception('Please provide a YQL request', 400);
91
        }
92
93
        if ($yql !== null) {
94
            $this->yql = $yql;
95
        }
96
97
        try {
98
            $response = $this->client->getClient()->get($yql)->json();
99
            if (!isset($response['query']['results']['channel']['item']['condition'])) {
100
                $this->lastResponse = false;
0 ignored issues
show
Documentation Bug introduced by
It seems like false of type false is incompatible with the declared type array of property $lastResponse.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
101
            } else {
102
                $this->lastResponse = $response['query']['results']['channel'];
103
            }
104
        } catch (\Exception $e) {
105
            $this->lastResponse = false;
106
        }
107
108
        return $this->lastResponse;
109
    }
110
111
    /**
112
     * Get lastResponse.
113
     *
114
     * @param bool $toJson choose format for the return value (array or json)
115
     *
116
     * @return array|string
117
     */
118
    public function getLastResponse($toJson = false)
119
    {
120
        return ($toJson) ? json_encode($this->lastResponse) : $this->lastResponse;
121
    }
122
123
    /**
124
     * Set lastResponse.
125
     *
126
     * @param array $data data from json_encode
127
     */
128
    public function setLastResponse($data)
129
    {
130
        $this->lastResponse = $data;
131
    }
132
133
    /**
134
     * Get current temperature.
135
     *
136
     * @param bool $withUnit return or not the unit
137
     *
138
     * @return string
139
     */
140
    public function getTemperature($withUnit = false)
141
    {
142
        if (!$this->lastResponse || !isset($this->lastResponse['item']['condition']['temp'])) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->lastResponse of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
143
            return '';
144
        }
145
        $return = $this->lastResponse['item']['condition']['temp'];
146
        if ($withUnit) {
147
            $return .= ' '.$this->lastResponse['units']['temperature'];
148
        }
149
150
        return $return;
151
    }
152
153
    /**
154
     * Get Location.
155
     *
156
     * @return string
157
     */
158 View Code Duplication
    public function getLocation()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
159
    {
160
        if (!$this->lastResponse || !isset($this->lastResponse['location']['city'])) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->lastResponse of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
161
            return '';
162
        }
163
164
        return $this->lastResponse['location']['city'];
165
    }
166
167
    /**
168
     * get Forecast.
169
     *
170
     * @return array
171
     */
172 View Code Duplication
    public function getForecast()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
173
    {
174
        if (!$this->lastResponse || !isset($this->lastResponse['item']['forecast'])) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->lastResponse of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
175
            return array();
176
        }
177
178
        return $this->lastResponse['item']['forecast'];
179
    }
180
181
    /**
182
     * get Wind.
183
     *
184
     * @param bool $withUnit return or not the unit
185
     *
186
     * @return array
187
     */
188
    public function getWind($withUnit = false)
189
    {
190
        if (!$this->lastResponse || !isset($this->lastResponse['wind']['speed'])) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->lastResponse of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
191
            return array();
192
        }
193
194
        $response = array(
195
            'chill' => $this->lastResponse['item']['wind']['chill'],
196
            'direction' => $this->lastResponse['item']['wind']['direction'],
197
            'speed' => $this->lastResponse['item']['wind']['speed'],
198
        );
199
200
        if ($withUnit) {
201
            $response['speed'] .= ' '.$this->lastResponse['units']['speed'];
202
        }
203
204
        return $response;
205
    }
206
207
    /**
208
     * @param GoutteClient $client
209
     */
210
    public function setClient(GoutteClient $client)
211
    {
212
        $this->client = $client;
0 ignored issues
show
Documentation Bug introduced by
It seems like $client of type object<Goutte\Client> is incompatible with the declared type object<Th3Mouk\YahooWeatherAPI\Client> of property $client.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
213
    }
214
215
    /**
216
     * @return string
217
     */
218
    public function getWoeid()
219
    {
220
        return $this->woeid;
221
    }
222
223
    /**
224
     * @return string
225
     */
226
    public function getCity()
227
    {
228
        return $this->city;
229
    }
230
231
    /**
232
     * @return string
233
     */
234
    public function getYql()
235
    {
236
        return $this->yql;
237
    }
238
}
239