Curl::sCurl()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 12
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 17
ccs 0
cts 13
cp 0
crap 2
rs 9.8666
1
<?php
2
3
namespace Jiad\Modules;
4
5
use Anax\Session\SessionInterface;
6
7
/**
8
 *
9
 */
10
class Curl
11
{
12
    /**
13
     * Make a single curl request to DarkSky's API.
14
     *
15
     * @param string $search object with IP or City to be searched for weather information.
16
     *
17
     * @return self
18
     */
19
    public function sCurl(array $search) : object
20
    {
21
        $key = require(ANAX_INSTALL_PATH . "/config/api_key.php");
22
        $api_key = $key["darksky"];
23
        $lat = $search["lat"];
24
        $long = $search["long"];
25
26
        $url = "https://api.darksky.net/forecast/$api_key/$lat,$long";
27
        $ch = curl_init();
28
29
        curl_setopt($ch, CURLOPT_URL, $url);
0 ignored issues
show
Bug introduced by
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

29
        curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_URL, $url);
Loading history...
30
        curl_setopt($ch, CURLOPT_HEADER, 0);
31
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
32
33
        $info = curl_exec($ch);
0 ignored issues
show
Bug introduced by
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

33
        $info = curl_exec(/** @scrutinizer ignore-type */ $ch);
Loading history...
34
        curl_close($ch);
0 ignored issues
show
Bug introduced by
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

34
        curl_close(/** @scrutinizer ignore-type */ $ch);
Loading history...
35
        return json_decode($info);
36
    }
37
38
    /**
39
     * Make multiple curl requests to Darksky's API and requesting weather for 30 days back.
40
     *
41
     * @param string $search object with IP or City to be searched for weather information.
42
     *
43
     * @return self
44
     */
45
    public function mCurl(array $search) : array
46
    {
47
        $key = require(ANAX_INSTALL_PATH . "/config/api_key.php");
48
        $api_key = $key["darksky"];
49
        $lat = $search["lat"];
50
        $long = $search["long"];
51
52
        $url = "https://api.darksky.net/forecast/$api_key/$lat,$long";
53
54
        $options = [
55
            CURLOPT_RETURNTRANSFER => true,
56
        ];
57
58
        $mh = curl_multi_init();
59
        $chAll = [];
60
61
        $time = time();
62
        
63
        $i = 1;
64
        while ($i <= 30) {
65
            $ch = curl_init("$url,$time");
66
            curl_setopt_array($ch, $options);
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

66
            curl_setopt_array(/** @scrutinizer ignore-type */ $ch, $options);
Loading history...
67
            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

67
            curl_multi_add_handle($mh, /** @scrutinizer ignore-type */ $ch);
Loading history...
68
            $chAll[] = $ch;
69
            $time = strtotime('-1 day', $time);
70
            $i++;
71
        }
72
        
73
        // Execute all queries simultaneously,
74
        // and continue when all are complete
75
        $running = null;
76
        do {
77
            curl_multi_exec($mh, $running);
78
        } while ($running);
79
80
        // Close the handles
81
        foreach ($chAll as $ch) {
82
            curl_multi_remove_handle($mh, $ch);
83
        }
84
        curl_multi_close($mh);
85
86
        // All of our requests are done, we can now access the results
87
        $response = [];
88
        foreach ($chAll as $ch) {
89
            $data = curl_multi_getcontent($ch);
90
            $response[] = json_decode($data, true);
91
        }
92
93
        return json_decode(json_encode($response), false);
94
    }
95
}
96