Passed
Push — master ( 550d43...d44f33 )
by Elena
02:09
created

CurlModel::getMultiData()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 30
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 9.3554

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 7
eloc 23
c 2
b 0
f 0
nc 6
nop 1
dl 0
loc 30
ccs 14
cts 22
cp 0.6364
crap 9.3554
rs 8.6186
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
                file_put_contents($cache, $jsonCache);
42
            } else {
43 9
                $jsonCache = file_get_contents($cache);
44
            }
45 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

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