Completed
Push — master ( 039fcd...735c44 )
by Kristoffer
02:06
created

Curl   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 33
c 1
b 0
f 0
dl 0
loc 73
ccs 31
cts 31
cp 1
rs 10
wmc 6
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
        if $ch {
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_VARIABLE, expecting '(' on line 28 at column 11
Loading history...
29 2
            // Set options
30
            curl_setopt_array($ch, $options);
31 2
            // Execute
32
            $data = curl_exec($ch);
33 2
            // Closing
34 2
            curl_close($ch);
35
            $res = json_decode($data, true);
36 2
            
37
            return $res;
38
        }
39
        return null;
40
    }
41
42
    /**
43
     * Function that takes multiple api urls and returns the result as a decoded json.
44
     *
45
     * @param array $urls
46 2
     *
47
     * @return array $result
48
     */
49
    public function multi(Array $urls) : array
50 2
    {
51 2
        // Setup options
52
        $options = [
53
            CURLOPT_RETURNTRANSFER => true,
54
            CURLOPT_HEADER => 0,
55 2
        ];
56 2
        // Add all curl handlers and remember them
57 2
        // Initiate the multi curl handler
58 2
        $mh = curl_multi_init();
59 2
        if $mh {
60 2
            $chAll = [];
61 2
            foreach ($urls as $url) {
62
                $ch = curl_init($url);
63
                curl_setopt_array($ch, $options);
64
                curl_multi_add_handle($mh, $ch);
65 2
                $chAll[] = $ch;
66
            }
67 2
            // Execute all queries simultaneously,
68 2
            // and continue when all are complete
69
            $running = null;
70 2
            do {
71 2
                curl_multi_exec($mh, $running);
72
            } while ($running);
73 2
            // Close the handles
74
            foreach ($chAll as $ch) {
75 2
                curl_multi_remove_handle($mh, $ch);
76 2
            }
77 2
            curl_multi_close($mh);
78 2
            // All of our requests are done, we can now access the results
79
            $response = [];
80 2
            foreach ($chAll as $ch) {
81
                $data = curl_multi_getcontent($ch);
82
                $response[] = json_decode($data, true);
83
            }
84
            return $response;
85
        }
86
        return null;
87
    }
88
}
89