Weather::getWeek()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
nc 2
nop 0
dl 0
loc 17
c 1
b 0
f 0
cc 3
rs 9.9332
1
<?php
2
namespace idla\Weather;
3
4
class Weather
5
{
6
    private $raw = null;
7
    private $forcast = null;
8
    private $history = null;
9
10
    public function init(string $lon, string $lat)
11
    {
12
        $url = "http://www.student.bth.se/~idla18/dbwebb-kurser/ramverk1/me/redovisa/htdocs/weatherAPI";
13
14
        $data = array("lat" => $lat, "lon" => $lon);
15
16
        $options = array(
17
            'http' => array(
18
                'header'  => "Content-type: application/json\r\n",
19
                'method'  => 'POST',
20
                'content' => json_encode($data)
21
            )
22
        );
23
        $context  = stream_context_create($options);
24
        $this->raw = file_get_contents($url, false, $context);
25
        
26
        $this->forcast = json_decode($this->raw)->forcast;
27
        $this->history = json_decode($this->raw)->history;
28
    }
29
30
    public function getRaw()
31
    {
32
        return $this->raw;
33
    }
34
35
    public function getWeek()
36
    {
37
        if ($this->forcast != null) {
38
            $week = $this->forcast->daily;
39
40
            foreach ($week as $day) {
41
                $currentWeek[] = [
42
                    "day" => date('D', $day->dt),
43
                    "date" => date('d/m', $day->dt),
44
                    "minTemp" => $day->temp->min,
45
                    "maxTemp" => $day->temp->max,
46
                    "weather" => $day->weather[0]->description,
47
                ];
48
            }
49
50
        }
51
        return $currentWeek ?? null;
52
    }
53
54
    public function getHistory()
55
    {
56
        if ($this->history != null) {
57
            $history = $this->history;
58
59
            foreach ($history as $day) {
60
                $maxTemp = 0;
61
                $minTemp = 0;
62
                $description = null;
63
                $dayDate = null;
64
                $date = null;
65
66
                foreach ($day as $hour) {
67
                    $dayDate = date('D', $hour->dt);
68
                    $date = date('d/m', $hour->dt);
69
70
                    if ($maxTemp < $hour->temp) {
71
                        $maxTemp = $hour->temp;
72
                    }
73
    
74
                    if ($minTemp == null) {
75
                        $minTemp = $hour->temp;
76
                    } elseif ($minTemp > $hour->temp) {
77
                        $minTemp = $hour->temp;
78
                    }
79
                    $description = $hour->weather[0]->description;
80
                }
81
82
                $historydays[] = [
83
                    "day" => $dayDate,
84
                    "date" => $date,
85
                    "maxTemp" => $maxTemp,
86
                    "minTemp" => $minTemp,
87
                    "description" => $description,
88
                ];
89
            }
90
91
        }
92
        return $historydays ?? null;
93
    }
94
}
95