Completed
Push — master ( 758b64...550d43 )
by Elena
02:18
created

CurlModel::getMultiData()   B

Complexity

Conditions 8
Paths 8

Size

Total Lines 35
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 14.2906

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 8
eloc 27
c 2
b 0
f 0
nc 8
nop 1
dl 0
loc 35
ccs 14
cts 26
cp 0.5385
crap 14.2906
rs 8.4444
1
<?php
2
3
/**
4
 * CurlModel.
5
 */
6
7
namespace Anax\CurlModel;
8
9
use PhpParser\Node\Expr\Cast\Array_;
10
11
/**
12
 * Showing off a standard class with methods and properties.
13
 */
14
class CurlModel
15
{
16
    protected $testMode;
17
18
    /**
19
     * Fetch data.
20
     *
21
     * @return array
22
     */
23
24 9
    public function getData(string $link)
25
    {
26 9
        if ($this->testMode == true) {
27 9
            $file = preg_replace("/[^[:alnum:][:space:]]/u", '', $link);
28 9
            $cache = ANAX_INSTALL_PATH . "/test/cache/weather/$file.cahce";
29 9
            $forceRefresh = false;
30 9
            $refresh = 60 * 60 * 13;
31 9
            if (!is_file(($cache))) {
32
                $handle = fopen($cache, 'wb');
33
                if ($handle === false) {
34
                    return array();
35
                }
36
                fclose($handle);
37
                $forceRefresh = true;
38
            }
39 9
            if ($forceRefresh === true || ((time() - filectime($cache)) > ($refresh) || 0 == filesize($cache))) {
40
                $jsonCache = $this->singleFetch($link);
41
                $handle = fopen($cache, 'wb');
42
                if ($handle === false) {
43
                    return array();
44
                }
45
                if (gettype($jsonCache) == array()) {
46
                    $jsonCache = json_encode($jsonCache);
47
                }
48
                fwrite($handle, $jsonCache);
0 ignored issues
show
Bug introduced by
It seems like $jsonCache can also be of type array; however, parameter $string of fwrite() does only seem to accept string, 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

48
                fwrite($handle, /** @scrutinizer ignore-type */ $jsonCache);
Loading history...
49
                fclose($handle);
50
            } else {
51 9
                $jsonCache = file_get_contents($cache);
52
            }
53 9
            return json_decode($jsonCache, true);
0 ignored issues
show
Bug introduced by
It seems like $jsonCache can also be of type array; however, parameter $json of json_decode() does only seem to accept string, 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

53
            return json_decode(/** @scrutinizer ignore-type */ $jsonCache, true);
Loading history...
54
        } else {
55
            return json_decode($this->singleFetch($link), true);
56
        }
57
    }
58
59
    public function singleFetch(string $link)
60
    {
61
        $curl = curl_init();
62
63
        if ($curl == false) {
64
            return array();
65
        }
66
        curl_setopt($curl, CURLOPT_URL, $link);
67
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
68
        $data = curl_exec($curl);
69
        curl_close($curl);
70
        return $data;
71
    }
72
73
    /**
74
     * Fetch multidata.
75
     *
76
     * @return array
77
     */
78
79 3
    public function getMultiData(array $links)
80
    {
81 3
        $outputArr = array();
82 3
        if ($this->testMode == true) {
83 3
            $forceRefresh = false;
84 3
            $refresh = 60 * 60 * 13;
85 3
            $url = $links[1];
86 3
            $tempUrl = preg_replace("/[,][0-9]*[?]/u", "", $url);
87 3
            $file = preg_replace("/[^[:alnum:][:space:]]/u", '', $tempUrl);
88 3
            $cache = ANAX_INSTALL_PATH . "/test/cache/weather/multi-$file.cache";
89 3
            if (!is_file(($cache))) {
90
                $handle = fopen($cache, 'wb');
91
                if ($handle === false) {
92
                    return array();
93
                }
94
                fclose($handle);
95
                $forceRefresh = true;
96
            }
97
98 3
            if ($forceRefresh === true || ((time() - filectime($cache)) > ($refresh) || 0 == filesize($cache))) {
99
                $outputArr = $this->fetchMultiData($links);
100
                $handle = fopen($cache, 'wb');
101
                if ($handle === false) {
102
                    return array();
103
                }
104
                fwrite($handle, json_encode($outputArr));
105
                fclose($handle);
106
            } else {
107 3
                $jsonCache = file_get_contents($cache);
108 3
                array_push($outputArr, json_decode($jsonCache[0], true));
109
            }
110
        } else {
111
            $outputArr = $this->fetchMultiData($links);
112
        }
113 3
        return $outputArr;
114
    }
115
    /*
116
    * Fetch multi data from API
117
    * @return array
118
    */
119
    private function fetchMultiData(array $links)
120
    {
121
        $multiCurl = array();
122
        $outputArr = array();
123
        //create the multiple cURL handle
124
        $multiHandler = curl_multi_init();
125
        foreach ($links as $link) {
126
            $curlHandler = curl_init();
127
            if ($curlHandler === false) {
128
                return array();
129
            }
130
            // set URL and other appropriate options
131
            curl_setopt($curlHandler, CURLOPT_URL, $link);
132
            curl_setopt($curlHandler, CURLOPT_HEADER, 0);
133
            curl_setopt($curlHandler, CURLOPT_RETURNTRANSFER, true);
134
            //add the two handles
135
            curl_multi_add_handle($multiHandler, $curlHandler);
136
            array_push($multiCurl, $curlHandler);
137
        }
138
        //execute the multi handle
139
        $active = null;
140
        do {
141
            curl_multi_exec($multiHandler, $active);
142
        } while ($active);
143
144
        //close the handles
145
        foreach ($multiCurl as $handler) {
146
            curl_multi_remove_handle($multiHandler, $handler);
147
        }
148
        curl_multi_close($multiHandler);
149
        foreach ($multiCurl as $handler) {
150
            $data = curl_multi_getcontent($handler);
151
            array_push($outputArr, json_decode($data, true));
152
        }
153
        return $outputArr;
154
    }
155
156
    /**
157
     * Set test mode.
158
     *
159
     */
160
161 20
    public function setTestMode(bool $mode)
162
    {
163 20
        $this->testMode = $mode;
164 20
    }
165
}
166