Requester   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Test Coverage

Coverage 54.84%

Importance

Changes 0
Metric Value
eloc 32
dl 0
loc 79
ccs 17
cts 31
cp 0.5484
rs 10
c 0
b 0
f 0
wmc 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A curlJson() 0 12 1
B multiRequest() 0 50 9
1
<?php
2
3
namespace Edward\Requester;
4
5
use Anax\Commons\ContainerInjectableInterface;
6
use Anax\Commons\ContainerInjectableTrait;
7
8
// use Anax\Route\Exception\ForbiddenException;
9
// use Anax\Route\Exception\NotFoundException;
10
// use Anax\Route\Exception\InternalErrorException;
11
12
/**
13
 * A sample controller to show how a controller class can be implemented.
14
 * The controller will be injected with $di if implementing the interface
15
 * ContainerInjectableInterface, like this sample class does.
16
 * The controller is mounted on a particular route and can then handle all
17
 * requests for that mount point.
18
 *
19
 * @SuppressWarnings(PHPMD.TooManyPublicMethods)
20
 */
21
class Requester implements ContainerInjectableInterface
22
{
23
    use ContainerInjectableTrait;
24
25
26
    /**
27
     * Curl URL and return JSON
28
     *
29
     * @return array
30
     */
31 2
    public function curlJson($url)
32
    {
33
        // Initialize CURL:
34 2
        $initization = curl_init($url);
35 2
        curl_setopt($initization, CURLOPT_RETURNTRANSFER, true);
36
37
        // Store the data:
38 2
        $json = curl_exec($initization);
39 2
        curl_close($initization);
40
41
        // Decode JSON response:
42 2
        return json_decode($json, true);
43
    }
44
45
    /**
46
     * Curl multi
47
     *
48
     * @return array
49
     */
50 2
    public function multiRequest($data, $options = array())
51
    {
52
        // array of curl handles
53 2
        $curly = array();
54
        // data to be returned
55 2
        $result = array();
56
57
        // multi handle
58 2
        $mh = curl_multi_init();
59
60
        // loop through $data and create curl handles
61
        // then add them to the multi-handle
62 2
        foreach ($data as $id => $d) {
63
            $curly[$id] = curl_init();
64
65
            $url = (is_array($d) && !empty($d['url'])) ? $d['url'] : $d;
66
            curl_setopt($curly[$id], CURLOPT_URL, $url);
67
            curl_setopt($curly[$id], CURLOPT_HEADER, 0);
68
            curl_setopt($curly[$id], CURLOPT_RETURNTRANSFER, 1);
69
70
            // post?
71
            if (is_array($d)) {
72
                if (!empty($d['post'])) {
73
                    curl_setopt($curly[$id], CURLOPT_POST, 1);
74
                    curl_setopt($curly[$id], CURLOPT_POSTFIELDS, $d['post']);
75
                }
76
            }
77
78
            // extra options?
79
            if (!empty($options)) {
80
                curl_setopt_array($curly[$id], $options);
81
            }
82
83
            curl_multi_add_handle($mh, $curly[$id]);
84
        }
85
86
        // execute the handles
87 2
        $running = null;
88
        do {
89 2
            curl_multi_exec($mh, $running);
90 2
        } while ($running > 0);
91
        // get content and remove handles
92 2
        foreach ($curly as $id => $c) {
93
            $result[$id] = curl_multi_getcontent($c);
94
            curl_multi_remove_handle($mh, $c);
95
        }
96
        // all done
97 2
        curl_multi_close($mh);
98
99 2
        return $result;
100
    }
101
}
102