Issues (11)

src/DIModel/DICheck.php (7 issues)

1
<?php
2
3
namespace Seb\DIModel;
4
5
class DICheck
6
{
7 4
    public function __construct(array $apiKeys)
8
    {
9 4
        $this->apiKeys = $apiKeys;
0 ignored issues
show
Bug Best Practice introduced by
The property apiKeys does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
10 4
    }
11
12 4
    public function checkCords($ipIn) : array
13
    {
14 4
        $key = $this->apiKeys["ipStack"];
15 4
        $ch = curl_init('http://api.ipstack.com/'.$ipIn.'?access_key='.$key.'');
16 4
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
0 ignored issues
show
It seems like $ch can also be of type false; however, parameter $ch of curl_setopt() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

16
        curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_RETURNTRANSFER, true);
Loading history...
17 4
        $json = curl_exec($ch);
0 ignored issues
show
It seems like $ch can also be of type false; however, parameter $ch of curl_exec() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

17
        $json = curl_exec(/** @scrutinizer ignore-type */ $ch);
Loading history...
18 4
        curl_close($ch);
0 ignored issues
show
It seems like $ch can also be of type false; however, parameter $ch of curl_close() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

18
        curl_close(/** @scrutinizer ignore-type */ $ch);
Loading history...
19 4
        $res = json_decode($json, true);
20
21 4
        $lon = null;
22 4
        $lat = null;
23 4
        $city = null;
24 4
        $country = null;
25
        
26 4
        if ($ipIn) {
27 4
            $lat = $res['latitude'] ?? null;
28 4
            $lon = $res['longitude'] ?? null;
29 4
            $city = $res['city'] ?? null;
30 4
            $country = $res['country_name'] ?? null;
31 4
            $lat = round($lat, 3);
32 4
            $lon = round($lon, 3);
33
        }
34
35 4
        return [$lon, $lat, $city, $country];
36
    }
37
38 4
    public function validateCords($lat, $lon) : bool
39
    {
40 4
        if ($lat == null || $lon == null) {
41 4
            return false;
42
        }
43
44 2
        $latNum = intval($lat);
45 2
        $lonNum = intval($lon);
46
        
47 2
        if ($latNum >= -90 && $latNum <= 90) {
48 2
            if ($lonNum >= -180 && $lonNum <= 180) {
49 2
                return true;
50
            }
51
        }
52 2
        return false;
53
    }
54
55 2
    public function getHistWeatherUrls($lat, $lon) : array
56
    {
57 2
        $key = $this->apiKeys["openWeatherApiKey"];
58 2
        $urls = [];
59 2
        $oneDayInSec = 60*60*24;
60 2
        $timestamp = time();
61
62 2
        for ($i=0; $i < 5; $i++) { 
63 2
            $timestamp -= $oneDayInSec;
64 2
            $url = "https://api.openweathermap.org/data/2.5/onecall/timemachine?lat=".$lat."&lon=".$lon."&dt=".$timestamp."&appid=".$key."&units=metric&lang=se";
65 2
            array_push($urls, $url);
66
        }
67
68 2
        return $urls;
69
    }
70
71 2
    public function getForecastWeatherUrl($lat, $lon) : array
72
    {
73 2
        $key = $this->apiKeys["openWeatherApiKey"];
74 2
        $url = [];
75
76 2
        $urlStr = "https://api.openweathermap.org/data/2.5/onecall?lat=".$lat."&lon=".$lon."&exclude=minutely,hourly,current&appid=".$key."&units=metric&lang=se";
77 2
        array_push($url, $urlStr);
78 2
        return $url;
79
    }
80
81 2
    public function curlMulti($urls) : array
82
    {
83
        $curlOpt = [
84 2
            CURLOPT_HEADER => false,
85 2
            CURLOPT_RETURNTRANSFER => true
86
        ];
87
88 2
        $multi = curl_multi_init();
89 2
        $chArray = [];
90
91 2
        foreach ($urls as $key => $url) {
92 2
            $ch = curl_init($url);
93 2
            curl_setopt_array($ch, $curlOpt);
0 ignored issues
show
It seems like $ch can also be of type false; however, parameter $ch of curl_setopt_array() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

93
            curl_setopt_array(/** @scrutinizer ignore-type */ $ch, $curlOpt);
Loading history...
94
95 2
            $chArray[$key] = $ch;
96
97 2
            curl_multi_add_handle($multi, $ch);
0 ignored issues
show
It seems like $ch can also be of type false; however, parameter $ch of curl_multi_add_handle() does only seem to accept resource, maybe add an additional type check? ( 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_multi_add_handle($multi, /** @scrutinizer ignore-type */ $ch);
Loading history...
98
        }
99
100 2
        $active = null;
101
        do {
102 2
            $mrc = curl_multi_exec($multi, $active);
103 2
        } while ($mrc == CURLM_CALL_MULTI_PERFORM);
104
105 2
        while ($active && $mrc == CURLM_OK) {
106 2
            while (curl_multi_exec($multi, $active) == CURLM_CALL_MULTI_PERFORM);
107
        }
108
109 2
        foreach ($chArray as $ch) {
110 2
            curl_multi_remove_handle($multi, $ch);
111
        }
112 2
        curl_multi_close($multi);
113
114 2
        $res = [];
115 2
        foreach ($chArray as $key => $ch) {
116 2
            $strRes = curl_multi_getcontent($ch);
117 2
            $json = json_decode($strRes, JSON_UNESCAPED_UNICODE);
0 ignored issues
show
Seb\DIModel\JSON_UNESCAPED_UNICODE of type integer is incompatible with the type boolean expected by parameter $assoc of json_decode(). ( Ignorable by Annotation )

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

117
            $json = json_decode($strRes, /** @scrutinizer ignore-type */ JSON_UNESCAPED_UNICODE);
Loading history...
118 2
            $res[$key] = $json;
119
        }
120
121 2
        return $res;
122
    }
123
124 2
    public function map($lat, $lon) : string
125
    {
126 2
        $map = '<iframe width="700" height="500" src="https://www.openstreetmap.org/export/embed.html?bbox=';
127 2
        $map = $map.($lon - 10).'%2C'.($lat - 10).'%2C'.($lon + 10).'%2C'.($lat + 10);
128 2
        $map = $map.'&amp;layer=mapnik&amp;marker='.$lat."%2C".$lon.'"></iframe>';
129 2
        return $map;
130
    }
131
}
132