MultiCURL::multicurla()   B
last analyzed

Complexity

Conditions 8
Paths 48

Size

Total Lines 58
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 28
CRAP Score 8.0025

Importance

Changes 0
Metric Value
eloc 32
dl 0
loc 58
ccs 28
cts 29
cp 0.9655
rs 8.1635
c 0
b 0
f 0
cc 8
nc 48
nop 2
crap 8.0025

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace KW\Models;
4
5
class MultiCURL
6
{
7
    /**
8
    * @var array $objekt som api:et returnerar
9
    */
10
11
12 2
    public function __construct($di)
13
    {
14 2
        $this->di = $di;
0 ignored issues
show
Bug Best Practice introduced by
The property di does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
15 2
        $this->darkSkyKey = $this->di->get("apikeys")["config"]["darkSky"];
0 ignored issues
show
Bug Best Practice introduced by
The property darkSkyKey does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
16 2
        $this->baseUrl = "https://api.darksky.net/forecast/";
0 ignored issues
show
Bug Best Practice introduced by
The property baseUrl does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
17 2
        $this->apiSettings = "?lang=sv&units=si";
0 ignored issues
show
Bug Best Practice introduced by
The property apiSettings does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
18 2
    }
19
20
21 2
    public function multicurla($latitude, $longitude)
22
    {
23
        $result = array(
24 2
            "daily" => array(
25
                "data" => array()
26
            )
27
        );
28 2
        $chh = [];
29 2
        $datum = time();
30
31
        // create all cURL resources
32 2
        for ($i=0; $i < 30; $i++) {
33 2
            $chh[$i] = curl_init();
34
        }
35
36
        // set URL and other appropriate options
37 2
        for ($i=0; $i < 30; $i++) {
38 2
            $aktuellsekund = $datum - 30*86400 + $i*86400;
39 2
            $url = ($this->baseUrl . $this->darkSkyKey . "/" . $latitude . "," . $longitude . "," . $aktuellsekund . $this->apiSettings);
40
            // create all cURL resources
41 2
            curl_setopt($chh[$i], CURLOPT_URL, $url);
42 2
            curl_setopt($chh[$i], CURLOPT_HEADER, 0);
43 2
            curl_setopt($chh[$i], CURLOPT_RETURNTRANSFER, 1);
44
        }
45
46
        //create the multiple cURL handle
47 2
        $mhh = curl_multi_init();
48
49
        //add the handles
50 2
        for ($i=0; $i < 30; $i++) {
51 2
            curl_multi_add_handle($mhh, $chh[$i]);
52
        }
53
54
        // execute the handles
55 2
        $running = null;
56
        do {
57 2
            curl_multi_exec($mhh, $running);
58 2
        } while ($running > 0);
59
60
61 2
        foreach ($chh as $id => $c) {
62 2
            $svar[$id] = curl_multi_getcontent($c);
63 2
            $svar[$id] = json_decode($svar[$id]);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $svar seems to be defined later in this foreach loop on line 62. Are you sure it is defined here?
Loading history...
64 2
            curl_multi_remove_handle($mhh, $c);
65
        }
66
67 2
        curl_multi_close($mhh);
68
69 2
        for ($i=0; $i < 30; $i++) {
70 2
            if (isset($svar[$i]->daily->data[0])) {
71
                $result['daily']['data'][$i] = $svar[$i]->daily->data[0];
72
            } else {
73 2
                $result['daily']['data'][$i] = "Historisk väderdata över aktuell plats och tid verkar ej vara tillgänglig";
74
                //exit("Historisk väderdata över aktuell plats och tid verkar ej vara tillgänglig");
75
            }
76
        }
77 2
        $result = json_decode(json_encode($result));
78 2
        return $result;
79
    }
80
}
81