Completed
Branch master (758b64)
by Elena
03:49 queued 01:36
created

CurlModel::fetchMultiData()   B

Complexity

Conditions 6
Paths 9

Size

Total Lines 35
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
cc 6
eloc 23
c 0
b 0
f 0
nc 9
nop 1
dl 0
loc 35
ccs 0
cts 23
cp 0
crap 42
rs 8.9297
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))) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
40
                $jsonCache = $this->singleFetch($link);
41
                $handle = fopen($cache, 'wb');
42
                if ($handle == false) {
43
                    return array();
44
                }
45
                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

45
                fwrite($handle, /** @scrutinizer ignore-type */ $jsonCache);
Loading history...
46
                fclose($handle);
47
            } else {
48 9
                $jsonCache = file_get_contents($cache);
49
            }
50 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

50
            return json_decode(/** @scrutinizer ignore-type */ $jsonCache, true);
Loading history...
51
        } else {
52
            return json_decode($this->singleFetch($link), true);
53
        }
54
    }
55
56
    public function singleFetch(string $link)
57
    {
58
        $curl = curl_init();
59
60
        if ($curl == false) {
61
            return array();
62
        }
63
        curl_setopt($curl, CURLOPT_URL, $link);
64
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
65
        $data = curl_exec($curl);
66
        curl_close($curl);
67
        return $data;
68
    }
69
70
    /**
71
     * Fetch multidata.
72
     *
73
     * @return array
74
     */
75
76 3
    public function getMultiData(array $links)
77
    {
78 3
        $outputArr = array();
79 3
        if ($this->testMode == true) {
80 3
            $forceRefresh = false;
81 3
            $refresh = 60 * 60 * 13;
82 3
            $url = $links[1];
83 3
            $tempUrl = preg_replace("/[,][0-9]*[?]/u", "", $url);
84 3
            $file = preg_replace("/[^[:alnum:][:space:]]/u", '', $tempUrl);
85 3
            $cache = ANAX_INSTALL_PATH . "/test/cache/weather/multi-$file.cache";
86 3
            if (!is_file(($cache))) {
87
                $handle = fopen($cache, 'wb');
88
                if ($handle == false) {
89
                    return array();
90
                }
91
                fclose($handle);
92
                $forceRefresh = true;
93
            }
94
95 3
            if ($forceRefresh == true || ((time() - filectime($cache)) > ($refresh) || 0 == filesize($cache))) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
96
                $outputArr = $this->fetchMultiData($links);
97
                $handle = fopen($cache, 'wb');
98
                if ($handle == false) {
99
                    return array();
100
                }
101
                fwrite($handle, json_encode($outputArr));
102
                fclose($handle);
103
            } else {
104 3
                $jsonCache = file_get_contents($cache);
105 3
                array_push($outputArr, json_decode($jsonCache[0], true));
106
            }
107
        } else {
108
            $outputArr = $this->fetchMultiData($links);
109
        }
110 3
        return $outputArr;
111
    }
112
    /*
113
    * Fetch multi data from API
114
    * @return array
115
    */
116
    private function fetchMultiData(array $links)
117
    {
118
        $multiCurl = array();
119
        $outputArr = array();
120
        //create the multiple cURL handle
121
        $multiHandler = curl_multi_init();
122
        foreach ($links as $link) {
123
            $curlHandler = curl_init();
124
            if ($curlHandler == false) {
125
                return array();
126
            }
127
            // set URL and other appropriate options
128
            curl_setopt($curlHandler, CURLOPT_URL, $link);
129
            curl_setopt($curlHandler, CURLOPT_HEADER, 0);
130
            curl_setopt($curlHandler, CURLOPT_RETURNTRANSFER, true);
131
            //add the two handles
132
            curl_multi_add_handle($multiHandler, $curlHandler);
133
            array_push($multiCurl, $curlHandler);
134
        }
135
        //execute the multi handle
136
        $active = null;
137
        do {
138
            curl_multi_exec($multiHandler, $active);
139
        } while ($active);
140
141
        //close the handles
142
        foreach ($multiCurl as $handler) {
143
            curl_multi_remove_handle($multiHandler, $handler);
144
        }
145
        curl_multi_close($multiHandler);
146
        foreach ($multiCurl as $handler) {
147
            $data = curl_multi_getcontent($handler);
148
            array_push($outputArr, json_decode($data, true));
149
        }
150
        return $outputArr;
151
    }
152
153
    /**
154
     * Set test mode.
155
     *
156
     */
157
158 20
    public function setTestMode(bool $mode)
159
    {
160 20
        $this->testMode = $mode;
161 20
    }
162
}
163