Completed
Branch master (d2edef)
by Kristoffer
13:52 queued 43s
created

Curl   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Test Coverage

Coverage 94.29%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 37
c 2
b 1
f 0
dl 0
loc 79
ccs 33
cts 35
cp 0.9429
rs 10
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
A single() 0 23 2
B multi() 0 38 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 2
        if ($ch) {
0 ignored issues
show
introduced by
$ch is of type false|resource, thus it always evaluated to false.
Loading history...
29
            // Set options
30 2
            curl_setopt_array($ch, $options);
31
            // Execute
32 2
            $data = curl_exec($ch);
33
            // Closing
34 2
            curl_close($ch);
35 2
            $res = json_decode($data, true);
36
            
37 2
            return $res;
38
        }
39
        return null;
0 ignored issues
show
Bug Best Practice introduced by
The expression return null returns the type null which is incompatible with the type-hinted return array.
Loading history...
40
    }
41
42
    /**
43
     * Function that takes multiple api urls and returns the result as a decoded json.
44
     *
45
     * @param array $urls
46
     *
47
     * @return array $result
48
     */
49 2
    public function multi(Array $urls) : array
50
    {
51
        // Setup options
52
        $options = [
53 2
            CURLOPT_RETURNTRANSFER => true,
54 2
            CURLOPT_HEADER => 0,
55
        ];
56
        // Add all curl handlers and remember them
57
        // Initiate the multi curl handler
58 2
        $mh = curl_multi_init();
59 2
        if ($mh) {
0 ignored issues
show
introduced by
$mh is of type resource, thus it always evaluated to false.
Loading history...
60 2
            $chAll = [];
61 2
            foreach ($urls as $url) {
62 2
                $ch = curl_init($url);
63 2
                curl_setopt_array($ch, $options);
64 2
                curl_multi_add_handle($mh, $ch);
65 2
                $chAll[] = $ch;
66
            }
67
            // Execute all queries simultaneously,
68
            // and continue when all are complete
69 2
            $running = null;
70
            do {
71 2
                curl_multi_exec($mh, $running);
72 2
            } while ($running);
73
            // Close the handles
74 2
            foreach ($chAll as $ch) {
75 2
                curl_multi_remove_handle($mh, $ch);
76
            }
77 2
            curl_multi_close($mh);
78
            // All of our requests are done, we can now access the results
79 2
            $response = [];
80 2
            foreach ($chAll as $ch) {
81 2
                $data = curl_multi_getcontent($ch);
82 2
                $response[] = json_decode($data, true);
83
            }
84 2
            return $response;
85
        }
86
        return null;
0 ignored issues
show
Bug Best Practice introduced by
The expression return null returns the type null which is incompatible with the type-hinted return array.
Loading history...
87
    }
88
}
89