Completed
Pull Request — master (#77)
by Alex
02:09
created

CurrentWeather   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 2
c 3
b 0
f 1
lcom 0
cbo 6
dl 0
loc 87
ccs 0
cts 19
cp 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 22 2
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;
19
20
use Cmfcmf\OpenWeatherMap;
21
use Cmfcmf\OpenWeatherMap\Util\City;
22
use Cmfcmf\OpenWeatherMap\Util\Sun;
23
use Cmfcmf\OpenWeatherMap\Util\Temperature;
24
use Cmfcmf\OpenWeatherMap\Util\Unit;
25
use Cmfcmf\OpenWeatherMap\Util\Weather as WeatherObj;
26
use Cmfcmf\OpenWeatherMap\Util\Wind;
27
28
/**
29
 * Weather class used to hold the current weather data.
30
 */
31
class CurrentWeather
32
{
33
    /**
34
     * The city object.
35
     *
36
     * @var Util\City
37
     */
38
    public $city;
39
40
    /**
41
     * The temperature object.
42
     *
43
     * @var Util\Temperature
44
     */
45
    public $temperature;
46
47
    /**
48
     * @var Util\Unit
49
     */
50
    public $humidity;
51
52
    /**
53
     * @var Util\Unit
54
     */
55
    public $pressure;
56
57
    /**
58
     * @var Util\Wind
59
     */
60
    public $wind;
61
62
    /**
63
     * @var Util\Unit
64
     */
65
    public $clouds;
66
67
    /**
68
     * @var Util\Unit
69
     */
70
    public $precipitation;
71
72
    /**
73
     * @var Util\Sun
74
     */
75
    public $sun;
76
77
    /**
78
     * @var Util\Weather
79
     */
80
    public $weather;
81
82
    /**
83
     * @var \DateTime
84
     */
85
    public $lastUpdate;
86
87
    /**
88
     * Create a new weather object.
89
     *
90
     * @param \SimpleXMLElement $xml
91
     * @param string            $units
92
     *
93
     * @internal
94
     */
95
    public function __construct(\SimpleXMLElement $xml, $units)
96
    {
97
        $this->city = new City($xml->city['id'], $xml->city['name'], $xml->city->coord['lon'], $xml->city->coord['lat'], $xml->city->country);
98
        $this->temperature = new Temperature(new Unit($xml->temperature['value'], $xml->temperature['unit']), new Unit($xml->temperature['min'], $xml->temperature['unit']), new Unit($xml->temperature['max'], $xml->temperature['unit']));
99
        $this->humidity = new Unit($xml->humidity['value'], $xml->humidity['unit']);
100
        $this->pressure = new Unit($xml->pressure['value'], $xml->pressure['unit']);
101
102
        // This is kind of a hack, because the units are missing in the xml document.
103
        if ($units == 'metric') {
104
            $windSpeedUnit = 'm/s';
105
        } else {
106
            $windSpeedUnit = 'mph';
107
        }
108
        $this->wind = new Wind(new Unit($xml->wind->speed['value'], $windSpeedUnit, $xml->wind->speed['name']), new Unit($xml->wind->direction['value'], $xml->wind->direction['code'], $xml->wind->direction['name']));
109
110
        $this->clouds = new Unit($xml->clouds['value'], null, $xml->clouds['name']);
111
        $this->precipitation = new Unit($xml->precipitation['value'], $xml->precipitation['unit'], $xml->precipitation['mode']);
112
        $utctz = new \DateTimeZone('UTC');
113
        $this->sun = new Sun(new \DateTime($xml->city->sun['rise'], $utctz), new \DateTime($xml->city->sun['set'], $utctz));
114
        $this->weather = new WeatherObj($xml->weather['number'], $xml->weather['value'], $xml->weather['icon']);
115
        $this->lastUpdate = new \DateTime($xml->lastupdate['value'], $utctz);
116
    }
117
}
118