Completed
Branch master (0df73e)
by Kristoffer
21:04 queued 18:40
created

Curl::multi()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 35
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 5

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 5
eloc 22
c 2
b 0
f 0
nc 8
nop 1
dl 0
loc 35
ccs 21
cts 21
cp 1
crap 5
rs 9.2568
1
<?php
2
3
namespace Linder\Model;
4
5
/**
6
 * A model class retrievieng data from an external server.
7
 */
8
class Curl
9
{
10
    /**
11
     * Function that takes an api url and returns the result as a decoded json.
12
     *
13
     * @param string $url
14
     *
15
     * @return array $result
16
     */
17 2
    public function single(String $url) : array
18
    {
19
20
        // Setup options
21
        $options = [
22 2
            CURLOPT_RETURNTRANSFER => true,
23 2
            CURLOPT_HEADER => 0,
24 2
            CURLOPT_URL => $url
25
        ];
26
        //  Initiate curl handler
27 2
        $ch = curl_init();
28
        // Set options
29 2
        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

29
        curl_setopt_array(/** @scrutinizer ignore-type */ $ch, $options);
Loading history...
30
        // Execute
31 2
        $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

31
        $data = curl_exec(/** @scrutinizer ignore-type */ $ch);
Loading history...
32
        // Closing
33 2
        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

33
        curl_close(/** @scrutinizer ignore-type */ $ch);
Loading history...
34 2
        $res = json_decode($data, true);
35
        
36 2
        return $res;
37
38
    }
39
40
    /**
41
     * Function that takes multiple api urls and returns the result as a decoded json.
42
     *
43
     * @param array $urls
44
     *
45
     * @return array $result
46
     */
47 2
    public function multi(Array $urls) : array
48
    {
49
        // Setup options
50
        $options = [
51 2
            CURLOPT_RETURNTRANSFER => true,
52 2
            CURLOPT_HEADER => 0,
53
        ];
54
        // Add all curl handlers and remember them
55
        // Initiate the multi curl handler
56 2
        $mh = curl_multi_init();
57 2
        $chAll = [];
58 2
        foreach ($urls as $url) {
59 2
            $ch = curl_init($url);
60 2
            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

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

61
            curl_multi_add_handle($mh, /** @scrutinizer ignore-type */ $ch);
Loading history...
62 2
            $chAll[] = $ch;
63
        }
64
        // Execute all queries simultaneously,
65
        // and continue when all are complete
66 2
        $running = null;
67
        do {
68 2
            curl_multi_exec($mh, $running);
69 2
        } while ($running);
70
        // Close the handles
71 2
        foreach ($chAll as $ch) {
72 2
            curl_multi_remove_handle($mh, $ch);
73
        }
74 2
        curl_multi_close($mh);
75
        // All of our requests are done, we can now access the results
76 2
        $response = [];
77 2
        foreach ($chAll as $ch) {
78 2
            $data = curl_multi_getcontent($ch);
79 2
            $response[] = json_decode($data, true);
80
        }
81 2
        return $response;
82
    }
83
}
84