Forecast   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 32
c 1
b 0
f 0
dl 0
loc 81
ccs 23
cts 23
cp 1
rs 10
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getDay() 0 14 1
A __construct() 0 3 1
A getIcon() 0 17 1
A getToday() 0 13 1
1
<?php
2
namespace Jenel\Weather;
3
4
/**
5
 * Return content for WeatherModel.
6
 */
7
8
class Forecast
9
{
10
    /**
11
     * variables
12
     */
13
    private $data;
14
15 2
    public function __construct($data)
16
    {
17 2
        $this->data =  $data;
18
        // var_dump($data);
19 2
    }
20
    /**
21
     * Return a HTML icon
22
     * @var icon string    icon name
23
     *
24
     * @return string   HTML compatible string
25
     */
26 2
    public function getIcon($icon) : string
27
    {
28
        $res = [
29 2
            "clear-day" => '<i class="fas fa-sun"> </i>',
30
            "clear-night" => '<i class="fas fa-moon"></i>',
31
            "rain" => '<i class="fas fa-cloud-rain"></i>',
32
            "snow" => '<i class="fas fa-snowflake"></i>',
33
            "sleet" => '<i class="fas fa-frown-open"></i>',
34
            "wind" => '<i class="fas fa-wind"></i>',
35
            "fog" => '<i class="fas fa-smog"></i>',
36
            "cloudy" => '<i class="fas fa-cloud"></i>',
37
            "partly-cloudy-day" => '<i class="fas fa-cloud-sun"></i>',
38
            "partly-cloudy-night" => '<i class="fas fa-cloud-moon"></i>',
39
        ];
40
41
        
42 2
        return $res[$icon] ?? '<i class="fas fa-user-astronaut"></i>';
43
    }
44
45
    /**
46
     * return an array with:
47
     * date of the forecast
48
     * summary for the day
49
     * temperature for the day
50
     * icon
51
     */
52 2
    public function getDay($index)
53
    {
54 2
        $tempMin = $this->data['daily']['data'][$index]['temperatureMin'];
55 2
        $tempMax = $this->data['daily']['data'][$index]['temperatureMax'];
56 2
        $temp = $tempMin + $tempMax / 2;
57
        $day = [
58 2
            "date" => date('D j M Y', intVal($this->data['daily']['data'][$index]['time'])),
59 2
            "sum" =>$this->data['daily']['data'][$index]['summary'],
60 2
            "temp" => round($temp, 1),
61 2
            "icon" => $this->getIcon($this->data['daily']['data'][$index]['icon']),
62 2
            "position" => $this->data['timezone']
63
        ];
64
65 2
        return $day;
66
    }
67
68
    /**
69
     * return current day forecast
70
     *
71
     * date of today
72
     * summary for the day
73
     * temperature right now
74
     * icon
75
     */
76 2
    public function getToday()
77
    {
78
        
79
        $today = [
80 2
            "date" => date('D j M Y', intVal($this->data['currently']['time'])),
81 2
            "sum" => $this->data['currently']['summary'],
82 2
            "temp" => round($this->data['currently']['temperature'], 1),
83 2
            "icon" => $this->getIcon($this->data['currently']['icon']),
84 2
            "position" => $this->data['timezone']
85
86
        ];
87
88 2
        return $today;
89
    }
90
}
91