Passed
Push — main ( 13bce2...e1bd00 )
by Åsa
02:19
created

WeatherService::curlWeatherApi()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 2.0014

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 16
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 20
ccs 13
cts 14
cp 0.9286
crap 2.0014
rs 9.7333
1
<?php
2
3
namespace Asti\Weather;
4
5
use Asti\Weather\CurlService;
6
use Asti\Weather\ApiDataProcessing;
7
8
class WeatherService
9
{
10
11
12
    /*
13
     *
14
     * curls api, get_file_contents
15
     * sends response to model
16
     *
17
     */
18
19
    private $key = null;
20
    private $url = null;
21
22 2
    public function setKey($key)
23
    {
24 2
        $this->key = $key;
25 2
    }
26
27 2
    public function setUrl($url) : void
28
    {
29 2
        $this->url = $url;
30 2
    }
31
32 2
    public function getKey() : string
33
    {
34 2
        return $this->key;
35
    }
36
37 2
    public function getUrl() : string
38
    {
39 2
        return $this->url;
40
    }
41
42
43
44 2
    public function curlWeatherApi($lon, $lat) : array
45
    {
46 2
        $curl = new CurlService();
47 2
        $data = new ApiDataProcessing();
48 2
        $res = $curl->getDataThroughCurl($this->getUrl() . "?" . "lat=" . $lat . "&lon=" . $lon . "&units=metric" . "&lang=sv" . "&appid=" . $this->getKey());
49 2
        if (isset($res["cod"])) {
50
            return [
51
                "Error" => "Platsangivelse är fel"
52
            ];
53
        } else {
54
            $json = [
55 2
                "CurrentTemp" => substr($res["current"]["temp"], 0, 5),
56 2
                "CurrentFeelsLike" => substr($res["current"]["feels_like"], 0, 5),
57 2
                "CurrentWeather" => $res["current"]["weather"][0]["description"],
58 2
                "DailyDates" => $data->loopThroughDate($res["daily"]),
59 2
                "DailyTemperatures" => $data->loopThroughTemp($res["daily"], "temp", "day"),
60 2
                "DailyFeelsLike" => $data->loopThroughTemp($res["daily"], "feels_like", "day"),
61 2
                "DailyDescriptions" => $data->loopThroughDesc($res["daily"], "weather", "description")
62
            ];
63 2
            return [$json];
64
        }
65
    }
66
67 2
    public function curlOldWeatherApi($lon, $lat) : array
68
    {
69 2
        $curl = new CurlService();
70 2
        $dateArray = [];
71 2
        $currentTime = time();
72 2
        for ($x = 0; $x <= 4; $x++) {
73 2
            array_push($dateArray, $currentTime -= 86400);
74
        }
75 2
        $res = $curl->getMultipleCurls($this->getUrl() . "/timemachine?" . "lat=" . $lat . "&lon=" . $lon . "&units=metric" . "&lang=sv" . "&dt=", $dateArray,  "&appid=" . $this->getKey());
76 2
        if (isset($res["cod"])) {
77
            return [
78
                "Error" => "Väder kan inte ges för positionen. Försök igen"
79
            ];
80
        }
81
        $json =  [
82 2
            "Date1" => date("Y-m-d", $res[0]["current"]["dt"]),
83 2
            "CurrentTemp1" => substr($res[0]["current"]["temp"], 0, 5),
84 2
            "CurrentFeelsLike1" => substr($res[0]["current"]["feels_like"], 0, 5),
85 2
            "CurrentWeather1" => $res[0]["current"]["weather"][0]["main"],
86 2
            "Date2" => date("Y-m-d", $res[1]["current"]["dt"]),
87 2
            "CurrentTemp2" => substr($res[1]["current"]["temp"], 0, 5),
88 2
            "CurrentFeelsLike2" =>  substr($res[1]["current"]["feels_like"], 0, 5),
89 2
            "CurrentWeather2" => $res[1]["current"]["weather"][0]["main"],
90 2
            "Date3" => date("Y-m-d", $res[2]["current"]["dt"]),
91 2
            "CurrentTemp3" => substr($res[2]["current"]["temp"], 0, 5),
92 2
            "CurrentFeelsLike3" =>  substr($res[2]["current"]["feels_like"], 0, 5),
93 2
            "CurrentWeather3" => $res[2]["current"]["weather"][0]["main"],
94 2
            "Date4" => date("Y-m-d", $res[3]["current"]["dt"]),
95 2
            "CurrentTemp4" => substr($res[3]["current"]["temp"], 0, 5),
96 2
            "CurrentFeelsLike4" =>  substr($res[3]["current"]["feels_like"], 0, 5),
97 2
            "CurrentWeather4" => $res[3]["current"]["weather"][0]["main"],
98 2
            "Date5" => date("Y-m-d", $res[4]["current"]["dt"]),
99 2
            "CurrentTemp5" => substr($res[4]["current"]["temp"], 0, 5),
100 2
            "CurrentFeelsLike5" =>  substr($res[4]["current"]["feels_like"], 0, 5),
101 2
            "CurrentWeather5" => $res[4]["current"]["weather"][0]["main"]
102
        ];
103 2
        return [$json];
104
    }
105
}
106