Passed
Push — main ( b79556...5cf45d )
by Johan
02:30
created

WeatherRequest::setAPI()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

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
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Lefty\Weather;
4
5
use Anax\Commons\ContainerInjectableInterface;
6
use Anax\Commons\ContainerInjectableTrait;
7
8
// use Anax\Route\Exception\ForbiddenException;
9
// use Anax\Route\Exception\NotFoundException;
10
// use Anax\Route\Exception\InternalErrorException;
11
12
/**
13
*
14
*
15
*/
16
17
18
class WeatherRequest implements ContainerInjectableInterface
19
{
20
    use ContainerInjectableTrait;
21
22
    private $curl = "";
23
    private $apiKey = "";
24
    private $geoLocation = [];
25
26 6
    public function setAPI(string $key)
27
    {
28 6
        $this->apiKey = $this->di->get("keystore")->getKey($key);
29 6
    }
30
31 7
    public function checkWeather(object $geoLocation)
32
    {
33
        // var_dump($geoLocation->geoLocationOK());
34
        // var_dump($geoLocation->getGeoLocation()["latitude"]);
35 7
        if ($geoLocation->geoLocationOK() === true) {
36
37 1
            $lon = $geoLocation->getGeoLocation()->longitude;
38 1
            $lat = $geoLocation->getGeoLocation()->latitude;
39 1
            $this->geoInitCurl();
40 1
            $this->geoSetOptCurl($lat, $lon);
41 1
            $this->geoExecuteCurl();
42 1
            $this->geoCloseCurl();
43
        }
44 7
    }
45
46 7
    public function checkWeatherMulti(object $geoLocation)
47
    {
48 7
        if ($geoLocation->geoLocationOK() === true) {
49
            # code...
50 1
            $lon = $geoLocation->getGeoLocation()->longitude;
51 1
            $lat = $geoLocation->getGeoLocation()->latitude;
52
            
53 1
            $multiRequests = [];
54
            
55 1
            for ($i = 0; $i < 5; $i++) {
56 1
                $unixTime = time() - ($i * 24 * 60 * 60);
57
                
58 1
                $multiRequests[] = 'https://api.openweathermap.org/data/2.5/onecall/timemachine?lat=' . $lat . '&lon=' . $lon . '&dt=' . $unixTime . '&units=metric&appid=' . $this->apiKey;
59
            }
60
            
61 1
            $multiHandle = curl_multi_init();
62 1
            $curlArray = array();
63
            
64 1
            foreach ($multiRequests as $i => $url) {
65 1
                $curlArray[$i] = curl_init($url);
66 1
                curl_setopt($curlArray[$i], CURLOPT_RETURNTRANSFER, true);
67 1
                curl_multi_add_handle($multiHandle, $curlArray[$i]);
68
            }
69
            
70 1
            $running = null;
71
            do {
72 1
                usleep(10000);
73 1
                curl_multi_exec($multiHandle, $running);
74 1
            } while ($running > 0);
75
            
76 1
            $res = array();
77 1
            foreach ($multiRequests as $i => $url) {
78 1
                $res[$i] = json_decode(curl_multi_getcontent($curlArray[$i]));
79
            }
80
            
81 1
            foreach ($multiRequests as $i => $url) {
82 1
                curl_multi_remove_handle($multiHandle, $curlArray[$i]);
83
            }
84 1
            curl_multi_close($multiHandle);
85 1
            return $res;
86
        }
87 6
    }
88
89 1
    private function geoInitCurl()
90
    {
91 1
        $this->curl = curl_init();
92 1
    }
93
94 1
    private function geoSetOptCurl($latitude, $longitude)
95
    {
96
97 1
        curl_setopt($this->curl, CURLOPT_URL, "https://api.openweathermap.org/data/2.5/onecall?lat=" . $latitude . "&lon=" . $longitude . "&units=metric&exclude=hourly,minutely&appid=" . $this->apiKey);
0 ignored issues
show
Bug introduced by
$this->curl of type string is incompatible with the type resource expected by parameter $ch of curl_setopt(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

97
        curl_setopt(/** @scrutinizer ignore-type */ $this->curl, CURLOPT_URL, "https://api.openweathermap.org/data/2.5/onecall?lat=" . $latitude . "&lon=" . $longitude . "&units=metric&exclude=hourly,minutely&appid=" . $this->apiKey);
Loading history...
98 1
        curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1);
99 1
    }
100
101 1
    private function geoExecuteCurl()
102
    {
103 1
        $this->geoLocation = json_decode(curl_exec($this->curl));
0 ignored issues
show
Bug introduced by
$this->curl of type string is incompatible with the type resource expected by parameter $ch of curl_exec(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

103
        $this->geoLocation = json_decode(curl_exec(/** @scrutinizer ignore-type */ $this->curl));
Loading history...
104 1
    }
105
106 1
    private function geoCloseCurl()
107
    {
108 1
        curl_close($this->curl);
0 ignored issues
show
Bug introduced by
$this->curl of type string is incompatible with the type resource expected by parameter $ch of curl_close(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

108
        curl_close(/** @scrutinizer ignore-type */ $this->curl);
Loading history...
109 1
    }
110
111 7
    public function getWeather()
112
    {
113 7
        return $this->geoLocation;
114
    }
115
}
116