CurlModel::curl()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
nc 2
nop 2
dl 0
loc 21
c 1
b 0
f 0
cc 2
ccs 8
cts 8
cp 1
crap 2
rs 10
1
<?php
2
namespace Blixter\Utilities;
3
4
/**
5
 *
6
 * Model for Curl
7
 *
8
 * @SuppressWarnings(PHPMD.ShortVariable)
9
 */
10
class CurlModel
11
{
12
    /**
13
     * Curl to given url and return an json-response.
14
     *
15
     * @param string $url as an url to curl
16
     * @param bool $json decode response to json if true, Default is false.
17
     *
18
     * @return $response
0 ignored issues
show
Documentation Bug introduced by
The doc comment $response at position 0 could not be parsed: Unknown type name '$response' at position 0 in $response.
Loading history...
19
     */
20 9
    public function curl($url, $json = false)
21
    {
22
        // init curl handler and set url
23 9
        $ch = curl_init($url);
24
25
        // Return the response, if fail, print the response
26 9
        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

26
        curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_RETURNTRANSFER, true);
Loading history...
27
28
        // Store the returned data
29 9
        $response = 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

29
        $response = curl_exec(/** @scrutinizer ignore-type */ $ch);
Loading history...
30
31
        // Close the curl handler
32 9
        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

32
        curl_close(/** @scrutinizer ignore-type */ $ch);
Loading history...
33
34
        // If $json === true
35 9
        if ($json) {
36
            // Decode to JSON
37 9
            $response = json_decode($response, true);
38
        }
39
40 9
        return $response;
41
    }
42
43
    /**
44
     * Curl to given urls and return an response.
45
     *
46
     * @param array $urls as an array with urls to curl
47
     * @param bool $json decode response to json if true, Default is false.
48
     *
49
     * @return array $response with the responses
50
     */
51 4
    public function multiCurl($urls, $json = false)
52
    {
53
        $options = [
54 4
            CURLOPT_RETURNTRANSFER => true,
55
        ];
56
57
        // Add the curl handlers.
58
        // Init the multi curl
59 4
        $mh = curl_multi_init();
60 4
        $chAll = [];
61 4
        foreach ((array) $urls as $url) {
62 4
            $ch = curl_init($url);
63 4
            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

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

64
            curl_multi_add_handle($mh, /** @scrutinizer ignore-type */ $ch);
Loading history...
65 4
            $chAll[] = $ch;
66
        }
67
68
        // Execute the multi curls
69 4
        $running = null;
70
        do {
71 4
            curl_multi_exec($mh, $running);
72 4
        } while ($running);
73
74
        // Close the handles
75 4
        foreach ($chAll as $ch) {
76 4
            curl_multi_remove_handle($mh, $ch);
77
        }
78 4
        curl_multi_close($mh);
79
80
        // The requests are done and the result stored in $response
81 4
        $response = [];
82 4
        foreach ($chAll as $ch) {
83 4
            $data = curl_multi_getcontent($ch);
84 4
            if ($json) {
85
                // Decode to JSON
86 3
                $response[] = json_decode($data, true);
87
88
            } else {
89 4
                $response[] = $data;
90
            }
91
        }
92 4
        return $response;
93
    }
94
}
95