Issues (10)

src/Weather/WeatherModel.php (1 issue)

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 10
    public function __construct()
27
    {
28 10
        $this->test = false;
29 10
    }
30
31
    /**
32
     * setup the basics: key, baseUrl, test, url to curl one, urls to curl multi
33
     */
34 10
    public function setConfig($param) : void
35
    {
36 10
        foreach ($param as $key => $value) {
37 10
            $this->$key = $value;
38
        }
39 10
    }
40
41
    /**
42
     * inject the weathermodel with the $di->service curls
43
     */
44 8
    public function setCurl($curl)
45
    {
46 8
        $this->curl = $curl;
47 8
    }
48
49
    /**
50
     * set url to fetch
51
     *
52
     * @var lat latitude
53
     * @var lon longitude
54
     *
55
     * @return void
56
     */
57 3
    public function getApiUrl($lat, $lon) : string
58
    {
59 3
        if ($this->test) {
60 2
            $this->url = $this->baseUrl;
61
        } else {
62 1
            $this->url = $this->baseUrl . $this->key . "/{$lat},{$lon}" . $this->exclude;
63
        }
64 3
        return $this->url;
65
    }
66
67
    /**
68
     * return urls for multi cyrl
69
     *
70
     * @return array
71
     */
72 2
    public function getApiUrls($lat, $lon, $days) : array
73
    {
74 2
        $urls = $this->urls;
75
76 2
        if ($this->test) {
77 1
            $this->urls = [ 0 => $this->baseUrl ];
78
        } else {
79 1
            for ($i=0; $i < $days; $i++) {
80 1
                $date = time() - ($i * 24 * 60 * 60); // echo(date('D j M Y', $date));
81 1
                $url = $this->baseUrl . $this->key . "/{$lat},{$lon},{$date}" . $this->excludeMulti;
82 1
                $urls[] = $url;
83
            };
84 1
            $this->urls = $urls;
85
        }
86 2
        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 4
    public function getGeo($location)
96
    {
97 4
        $curl = $this->curl;
98 4
        if ($this->test) {
99 4
            $url = $this->geoUrl;
100
        } else {
101
            $url = $this->geoUrl . "&q={$location}";
102
        }
103
104 4
        $result = $curl->curlOne($url);
105
         
106
        $geo = [
107 4
            "geo" => new Geo($result)
108
        ];
109 4
        $this->setConfig($geo);
110 4
        return $geo["geo"];
111
    }
112
113
114
115
116
117
    /**
118
     * Call for data.
119
     * uses $di service "curl"
120
     */
121 2
    public function getWeek($lat, $lon) : array
122
    {
123 2
        $curl = $this->curl;
124 2
        $week = array();
125 2
        $url = $this->getApiUrl($lat, $lon);
126 2
        $result = $curl->curlOne($url);
127
128 2
        if (array_key_exists('error', $result)) {
129
            $data = [
130
                "contentTitle" => "Oops...",
131
                "result" => $result
132
            ];
133
            return $data;
134
        }
135
136 2
        $this->Forecast = new Forecast($result);
137 2
        $week[0] = $this->Forecast->getToday();
138
139 2
        for ($i=1; $i < 7; $i++) {
140 2
            $week[$i] = $this->Forecast->getDay($i);
141
        }
142
        $data = [
143 2
            "contentTitle" => "Forecast for today and next 7 days",
144 2
            "result" => $week,
145
        ];
146 2
        return $data;
147
    }
148
149
    /**
150
     * Call for data.
151
     * uses $di service "curl"
152
     */
153 1
    public function getPast($lat, $lon) : array
154
    {
155 1
        $curl = $this->curl;
156 1
        $data = array();
157 1
        $urls = $this->getApiUrls($lat, $lon, 30);
158
159 1
        $multiresult = $curl->curlMulti($urls);
160
161 1
        foreach ($multiresult as $result) {
162 1
            if (array_key_exists('error', $result)) {
163
                $data = [
164
                    "contentTitle" => "Oops...",
165
                    "result" => $result
166
                ];
167
                return $data;
168
            } else {
169 1
                $this->Forecast = new Forecast($result);
170 1
                $data[] = $this->Forecast->getToday();
171
            }
172
        }
173
174
        $past = [
175 1
            "contentTitle" => "The weather today and the past 30 days",
176 1
            "today" => $data[0],
177 1
            "result" => $data
178
        ];
179 1
        return $past;
180
    }
181
182
    /**
183
     * Return all
184
     */
185 2
    public function getAll($radio) : array
186
    {
187 2
        $lat = $this->geo->getLat();
188 2
        $lon = $this->geo->getLon();
189
190 2
        if ($radio == "week") {
191 2
            $forecast = $this->getWeek($lat, $lon);
192 1
        } else if ($radio == "past") {
193 1
            $forecast = $this->getPast($lat, $lon);
194
        }
195
        $data = [
196 2
            "title" => "Weather forecast",
197 2
            "position" => $this->geo->getDisplayName(),
198 2
            "lat" => $lat,
199 2
            "lon" => $lon,
200 2
            "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 2
        return $data;
203
    }
204
}
205