Curl::getMultiData()   A
last analyzed

Complexity

Conditions 6
Paths 8

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 17
c 1
b 0
f 0
nc 8
nop 1
dl 0
loc 28
ccs 0
cts 17
cp 0
crap 42
rs 9.0777
1
<?php
2
3
/**
4
 * Curl model
5
 */
6
7
namespace Hepa19\Curl;
8
9
/**
10
 * Get data via public API
11
 *
12
 */
13
class Curl
14
{
15
    /**
16
     * Function that uses curl and returns response
17
     *
18
     * @param string $url API URL to use in curl
19
     *
20
     * @return array $res Result in JSON
21
     */
22
23
    public function getData(String $url)
24
    {
25
        $ch = curl_init($url);
26
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
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, 1);
Loading history...
27
        $res = 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

27
        $res = curl_exec(/** @scrutinizer ignore-type */ $ch);
Loading history...
28
        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

28
        curl_close(/** @scrutinizer ignore-type */ $ch);
Loading history...
29
        $res = json_decode($res, true);
30
31
        return $res;
32
    }
33
34
35
36
    /**
37
     * Function that uses multiple curls and returns response
38
     *
39
     * @param array $urls API URLs to use in curls
40
     *
41
     * @return array $res Result in JSON
42
     */
43
    public function getMultiData(Array $urls)
44
    {
45
        $mh = curl_multi_init();
46
47
        $chArray = [];
48
49
        foreach ($urls as $url) {
50
            $ch = curl_init($url);
51
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
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

51
            curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_RETURNTRANSFER, 1);
Loading history...
52
            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

52
            curl_multi_add_handle($mh, /** @scrutinizer ignore-type */ $ch);
Loading history...
53
            array_push($chArray, $ch);
54
        }
55
56
        do {
57
            $status = curl_multi_exec($mh, $active);
58
            if ($active) {
59
                curl_multi_select($mh);
60
            }
61
        } while ($active && $status == CURLM_OK);
62
63
        $res = [];
64
65
        foreach ($chArray as $ch) {
66
            $response = curl_multi_getcontent($ch);
67
            $res[] = json_decode($response, true);
68
        }
69
70
        return $res;
71
    }
72
}
73