Weather::multiCurl()   A
last analyzed

Complexity

Conditions 5
Paths 8

Size

Total Lines 33
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 21
nc 8
nop 1
dl 0
loc 33
c 2
b 1
f 0
cc 5
ccs 0
cts 20
cp 0
crap 30
rs 9.2728
1
<?php
2
3
namespace Anax\Models;
4
5
use Anax\Config\access;
0 ignored issues
show
Bug introduced by
The type Anax\Config\access was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
7
/**
8
 * Class Weather
9
*/
10
class Weather
11
{
12
    private $keys;
13
14
    public function setKeys($config)
15
    {
16
        $this->keys = $config;
17
    }
18
19
    /**
20
    * Curl from url
21
    */
22
23
    public function curl($url)
24
    {
25
        $curl = curl_init($url);
26
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
0 ignored issues
show
Bug introduced by
It seems like $curl 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

26
        curl_setopt(/** @scrutinizer ignore-type */ $curl, CURLOPT_RETURNTRANSFER, true);
Loading history...
27
        $data = curl_exec($curl);
0 ignored issues
show
Bug introduced by
It seems like $curl 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

27
        $data = curl_exec(/** @scrutinizer ignore-type */ $curl);
Loading history...
28
        curl_close($curl);
0 ignored issues
show
Bug introduced by
It seems like $curl 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

28
        curl_close(/** @scrutinizer ignore-type */ $curl);
Loading history...
29
30
        return $data;
31
    }
32
33
    public function multiCurl($urls)
34
    {
35
        $opts = [
36
            CURLOPT_RETURNTRANSFER => true,
37
        ];
38
        $cAll = [];
39
40
        $mh = curl_multi_init();
41
42
        foreach ($urls as $url) {
43
            $ch = curl_init("$url");
44
            curl_setopt_array($ch, $opts);
0 ignored issues
show
Bug introduced by
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

44
            curl_setopt_array(/** @scrutinizer ignore-type */ $ch, $opts);
Loading history...
45
            curl_multi_add_handle($mh, $ch);
0 ignored issues
show
Bug introduced by
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

45
            curl_multi_add_handle($mh, /** @scrutinizer ignore-type */ $ch);
Loading history...
46
            $cAll[] = $ch;
47
        }
48
49
        $running = null;
50
        do {
51
            curl_multi_exec($mh, $running);
52
        } while ($running);
53
54
        foreach ($cAll as $ch) {
55
            curl_multi_remove_handle($mh, $ch);
56
        }
57
        curl_multi_close($mh);
58
59
        $response = [];
60
        foreach ($cAll as $ch) {
61
            $data = curl_multi_getcontent($ch);
62
            array_push($response, $data);
63
        }
64
65
        return $response;
66
    }
67
68
    public function getWeather($lat, $long, $geo)
69
    {
70
        $urls;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $urls seems to be never defined.
Loading history...
71
        $accessToken = $this->keys["darkSky"];
72
73
        if ($geo == "future") {
74
            $url = 'https://api.darksky.net/forecast/' . $accessToken . "/" . $lat . "," . $long . "?units=si";
75
            $data = json_decode($this->curl($url));
76
        } else {
77
            $days = 5;
78
            for ($i = 0; $i < $days; $i++) {
79
                $timestamp = "-" . $i . " day";
80
                $time = strtotime($timestamp, time());
81
                $urls[$i] = 'https://api.darksky.net/forecast/' . $accessToken . "/" . $lat . "," . $long . "," . $time . "?units=si";
82
            }
83
            $curlResponse = $this->multiCurl($urls);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $urls does not seem to be defined for all execution paths leading up to this point.
Loading history...
84
            for ($i = 0; $i < $days; $i++) {
85
                $data[$i] = json_decode($curlResponse[$i]);
86
            }
87
        }
88
        $mapUrl = "https://nominatim.openstreetmap.org/reverse?lat=$lat&lon=$long&[email protected]&format=json";
89
        $mapData = json_decode($this->curl($mapUrl));
90
        $weatherData = [
91
            "data" => $data,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $data does not seem to be defined for all execution paths leading up to this point.
Loading history...
92
            "mapData" => $mapData
93
        ];
94
        return $weatherData;
95
    }
96
}
97