CallUrl   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
eloc 46
dl 0
loc 108
ccs 45
cts 45
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A fetch() 0 14 1
A buildUrl() 0 12 3
B fetchConcurrently() 0 47 6
1
<?php
2
/**
3
 * Showing off a standard class with methods and properties.
4
 */
5
namespace Erjh17\CallUrl;
6
7
use Anax\Commons\ContainerInjectableInterface;
8
use Anax\Commons\ContainerInjectableTrait;
9
10
/**
11
 * CallUrl
12
 */
13
class CallUrl implements ContainerInjectableInterface
14
{
15
    use ContainerInjectableTrait;
16
17
    /**
18
     * Fetches information from a foreign API.
19
     *
20
     * @param string $urlHost   The base url for the API website.
21
     * @param array  $query     The query string for the API call.
22
     *
23
     * @return array The result data from the API call.
24
     */
25 15
    public function fetch($urlHost, $query)
26
    {
27 15
        $queryString = "?" . http_build_query($query);
28 15
        $apiGet = curl_init($urlHost.$queryString);
29
30 15
        curl_setopt($apiGet, CURLOPT_RETURNTRANSFER, true);
31
32 15
        $json = curl_exec($apiGet);
33
34 15
        curl_close($apiGet);
35
36 15
        $apiResult = json_decode($json, true);
37
38 15
        return $apiResult;
39
    }
40
41
42
    /**
43
     * [buildUrl description]
44
     *
45
     * @param  string $url     [description]
46
     * @param  array  $params  [description]
47
     * @param  array  $queries [description]
48
     *
49
     * @return string          [description]
50
     */
51 3
    public function buildUrl($url, $params, $queries)
52
    {
53 3
        $returnUrl = $url;
54 3
        if (!empty($params)) {
55 3
            $returnUrl .= implode("/", $params);
56
        }
57
58 3
        if (!empty($queries)) {
59 3
            $returnUrl .= "?" . http_build_query($queries);
60
        }
61
62 3
        return $returnUrl;
63
    }
64
65
    /**
66
     * Fetch multiple
67
     *
68
     * @param array $urls    [description]
69
     * @param array $params  [description]
70
     * @param array $queries [description]
71
     *
72
     * @return array          [description]
73
     */
74 4
    public function fetchConcurrently($urls, $params, $queries)
75
    {
76 4
        $cache = $this->di->get("cache");
77 4
        $hour = date('YMDH');
78 4
        $latLonDay = explode(",", $params[0][1]);
79 4
        $lat = $latLonDay[0];
80 4
        $lon = $latLonDay[1];
81 4
        $cookieName = $lat . $lon . $hour;
82 4
        $weatherCache = $cache->get($cookieName);
83
84 4
        if ($weatherCache) {
85 1
            return $weatherCache;
86
        } else {
87 3
            $nodes = array();
88 3
            $results = array();
89
90 3
            $urlCount = count($urls);
91
92 3
            for ($i=0; $i < $urlCount; $i++) {
93 3
                $url = $this->buildUrl($urls[$i], $params[$i], $queries[$i]);
94 3
                array_push($nodes, $url);
95
            }
96
97
            // $json = json_decode(file_get_contents(__DIR__ . '/getjson.json'), true);
98
            // return $json;
99 3
            $nodeCount = count($nodes);
100
101 3
            $curlArray = array();
102 3
            $master = curl_multi_init();
103
104 3
            for ($i = 0; $i < $nodeCount; $i++) {
105 3
                $url = $nodes[$i];
106 3
                $curlArray[$i] = curl_init($url);
107 3
                curl_setopt($curlArray[$i], CURLOPT_RETURNTRANSFER, true);
108 3
                curl_multi_add_handle($master, $curlArray[$i]);
109
            }
110
111
            do {
112 3
                curl_multi_exec($master, $running);
113 3
            } while ($running > 0);
114
115 3
            for ($i = 0; $i < $nodeCount; $i++) {
116 3
                $results[] = json_decode(curl_multi_getcontent($curlArray[$i]), true);
117
            }
118 3
            $cache->set($cookieName, $results);
119
120 3
            return $results;
121
        }
122
    }
123
}
124