Test Setup Failed
Push — master ( 79f2cd...4dddaf )
by Tomie
01:51
created

WeatherModel::setCurl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
namespace Jenel\Weather;
3
4
/**
5
 * Return content for weatherController
6
 * uses $di->get("curl")
7
 */
8
9
class WeatherModel
10
{
11
    /** declare variables */
12
    private $Forecast;
13
    private $curl;
14
    private $baseUrl;
15
    private $geoUrl;
16
    private $exclude;
17
    private $excludeMulti;
18
    private $test;
19
    private $url;
20
    private $urls;
21
    private $geo;
22
    protected $key;
23
24
25
26
    public function __construct()
27
    {
28
        $this->test = false;
29
    }
30
31
    /**
32
     * setup the basics: key, baseUrl, test, url to curl one, urls to curl multi
33
     */
34
    public function setConfig($param) : void
35
    {
36
        foreach ($param as $key => $value) {
37
            $this->$key = $value;
38
        }
39
    }
40
41
    /**
42
     * inject the weathermodel with the $di->service curls
43
     */
44
    public function setCurl($curl)
45
    {
46
        $this->curl = $curl;
47
    }
48
49
    /**
50
     * set url to fetch
51
     *
52
     * @var lat latitude
53
     * @var lon longitude
54
     *
55
     * @return void
56
     */
57
    public function getApiUrl($lat, $lon) : string
58
    {
59
        if ($this->test) {
60
            $this->url = $this->baseUrl;
61
        } else {
62
            $this->url = $this->baseUrl . $this->key . "/{$lat},{$lon}" . $this->exclude;
63
        }
64
        return $this->url;
65
    }
66
67
    /**
68
     * return urls for multi cyrl
69
     *
70
     * @return array
71
     */
72
    public function getApiUrls($lat, $lon, $days) : array
73
    {
74
        $urls = $this->urls;
75
76
        if ($this->test) {
77
            $this->urls = [ 0 => $this->baseUrl ];
78
        } else {
79
            for ($i=0; $i < $days; $i++) {
80
                $date = time() - ($i * 24 * 60 * 60); // echo(date('D j M Y', $date));
81
                $url = $this->baseUrl . $this->key . "/{$lat},{$lon},{$date}" . $this->excludeMulti;
82
                $urls[] = $url;
83
            };
84
            $this->urls = $urls;
85
        }
86
        return $this->urls;
87
    }
88
89
    /**
90
     * Return latitude and longitude from a location string.
91
     * @var location  string of location to get geografic position from
92
     *
93
     * @return geo  array with lat and long
94
     */
95
    public function getGeo($location)
96
    {
97
        $curl = $this->curl;
98
        if ($this->test) {
99
            $url = $this->geoUrl;
100
        } else {
101
            $url = $this->geoUrl . "&q={$location}";
102
        }
103
104
        $result = $curl->curlOne($url);
105
         
106
        $geo = [
107
            "geo" => new Geo($result)
108
        ];
109
        $this->setConfig($geo);
110
        return $geo["geo"];
111
    }
112
113
114
115
116
117
    /**
118
     * Call for data.
119
     * uses $di service "curl"
120
     */
121
    public function getWeek($lat, $lon) : array
122
    {
123
        $curl = $this->curl;
124
        $week = array();
125
        $url = $this->getApiUrl($lat, $lon);
126
        $result = $curl->curlOne($url);
127
128
        if (array_key_exists('error', $result)) {
129
            $data = [
130
                "contentTitle" => "Oops...",
131
                "result" => $result
132
            ];
133
            return $data;
134
        }
135
136
        $this->Forecast = new Forecast($result);
137
        $week[0] = $this->Forecast->getToday();
138
139
        for ($i=1; $i < 7; $i++) {
140
            $week[$i] = $this->Forecast->getDay($i);
141
        }
142
        $data = [
143
            "contentTitle" => "Forecast for today and next 7 days",
144
            "result" => $week,
145
        ];
146
        return $data;
147
    }
148
149
    /**
150
     * Call for data.
151
     * uses $di service "curl"
152
     */
153
    public function getPast($lat, $lon) : array
154
    {
155
        $curl = $this->curl;
156
        $data = array();
157
        $urls = $this->getApiUrls($lat, $lon, 30);
158
159
        $multiresult = $curl->curlMulti($urls);
160
161
        foreach ($multiresult as $result) {
162
            if (array_key_exists('error', $result)) {
163
                $data = [
164
                    "contentTitle" => "Oops...",
165
                    "result" => $result
166
                ];
167
                return $data;
168
            } else {
169
                $this->Forecast = new Forecast($result);
170
                $data[] = $this->Forecast->getToday();
171
            }
172
        }
173
174
        $past = [
175
            "contentTitle" => "The weather today and the past 30 days",
176
            "today" => $data[0],
177
            "result" => $data
178
        ];
179
        return $past;
180
    }
181
182
    /**
183
     * Return all
184
     */
185
    public function getAll($radio) : array
186
    {
187
        $lat = $this->geo->getLat();
188
        $lon = $this->geo->getLon();
189
190
        if ($radio == "week") {
191
            $forecast = $this->getWeek($lat, $lon);
192
        } else if ($radio == "past") {
193
            $forecast = $this->getPast($lat, $lon);
194
        }
195
        $data = [
196
            "title" => "Weather forecast",
197
            "position" => $this->geo->getDisplayName(),
198
            "lat" => $lat,
199
            "lon" => $lon,
200
            "result" => $forecast,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $forecast does not seem to be defined for all execution paths leading up to this point.
Loading history...
201
        ];
202
        return $data;
203
    }
204
}
205