Completed
Push — master ( 749d6b...11b362 )
by jesper
02:12
created

CurlModel::getCurl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 14
ccs 0
cts 7
cp 0
crap 2
rs 10
1
<?php
2
namespace Anax\Curl;
3
4
/**
5
 * A model class retrievieng data from an external server.
6
 */
7
class CurlModel
8
{
9
10
    /**
11
     * Get curl from given url
12
     *
13
     * @return array
14
     */
15
    public function getCurl(string $url) : array
16
    {
17
        //  Initiate curl handler
18
        $ch = curl_init();
19
        // Will return the response, if false it print the response
20
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
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

20
        curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_RETURNTRANSFER, true);
Loading history...
21
        // Set the url
22
        curl_setopt($ch, CURLOPT_URL, $url);
23
        // Execute
24
        $data = 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

24
        $data = curl_exec(/** @scrutinizer ignore-type */ $ch);
Loading history...
25
        // Closing
26
        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

26
        curl_close(/** @scrutinizer ignore-type */ $ch);
Loading history...
27
28
        return json_decode($data, true) ?? [];
29
    }
30
31
32
    /**
33
     * Get curl using multicurl
34
     *
35
     * @return array
36
     */
37
    public function getMultiCurl(array $urls) : array
38
    {
39
        $options = [
40
            CURLOPT_RETURNTRANSFER => true,
41
        ];
42
        // Add all curl handlers and remember them
43
        // Initiate the multi curl handler
44
        $mh = curl_multi_init();
45
        $chAll = [];
46
        foreach ($urls as $url) {
47
            $ch = curl_init("$url");
48
            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

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

49
            curl_multi_add_handle($mh, /** @scrutinizer ignore-type */ $ch);
Loading history...
50
            $chAll[] = $ch;
51
        }
52
        // Execute all queries simultaneously,
53
        // and continue when all are complete
54
        $running = null;
55
        do {
56
            curl_multi_exec($mh, $running);
57
        } while ($running);
58
        // Close the handles
59
        foreach ($chAll as $ch) {
60
            curl_multi_remove_handle($mh, $ch);
61
        }
62
        curl_multi_close($mh);
63
        // All of our requests are done, we can now access the results
64
        $response = [];
65
        foreach ($chAll as $ch) {
66
            $data = curl_multi_getcontent($ch);
67
            $response[] = json_decode($data, true);
68
        }
69
        return $response;
70
    }
71
}
72