DarkSky::setConfig()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Linder\Model;
4
5
use Linder\Model\Curl;
6
7
/**
8
 * A model class retrievieng data from an external server.
9
 *
10
 * @SuppressWarnings(PHPMD.ShortVariable)
11
 */
12
class DarkSky
13
{
14
    private $config;
15
    private $curl;
16
17
    /**
18
     * Constructor, allow for $di to be injected.
19
     *
20
     * @param array $config container
21
     */
22 2
    public function __construct(Array $config)
23
    {
24 2
        $this->config = $config;
25 2
        $this->curl = new Curl();
26 2
    }
27
28
    /**
29
     * Set config
30
     */
31 2
    public function setConfig(Array $config)
32
    {
33 2
        $this->config = $config;
34 2
    }
35
36
    /**
37
     * Function that takes an coordinate and gets upcomming weather.
38
     *
39
     * @param string $latlon
40
     *
41
     * @return array $result
42
     */
43 2
    public function getWeatherComing(String $latlon) : array
44
    {
45 2
        $res = [];
46 2
        $res[0] = $this->curl->single($this->config["url"] . $latlon . $this->config["single"]);
47 2
        $size = count($res[0]["daily"]["data"]);
48 2
        for ($i = 0; $i < $size; $i++) {
49 2
            $time = $res[0]["daily"]["data"][$i]["time"];
50 2
            $res[0]["daily"]["data"][$i]["date"] = date("Y-m-d", $time);
51
        }
52 2
        return $res;
53
    }
54
55
56
    /**
57
     * Function that takes an coordinate and get past 30 days weather
58
     *
59
     * @param string $latlon
60
     *
61
     * @return array $result
62
     */
63 2
    public function getWeatherPast(String $latlon) : array
64
    {
65 2
        $curr = time();
66 2
        $urls = [];
67 2
        for ($i = 0; $i < 30; $i++) {
68
            // take away a day from current time
69 2
            $curr -= 86400;
70 2
            $urls[] = $this->config["url"] . $latlon . "," . $curr . $this->config["multi"];
71
        }
72 2
        $res = $this->curl->multi($urls);
73 2
        $size = count($res);
74 2
        for ($i = 0; $i < $size; $i++) {
75 2
            $time = $res[$i]["daily"]["data"][0]["time"];
76 2
            $res[$i]["daily"]["data"][0]["date"] = date("Y-m-d", $time);
77
        }
78 2
        return $res;
79
    }
80
}
81