WeatherIpController::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
1
<?php
2
namespace Aisa\Weather;
3
use Anax\Commons\ContainerInjectableInterface;
4
use Anax\Commons\ContainerInjectableTrait;
5
// use Anax\Route\Exception\ForbiddenException;
6
// use Anax\Route\Exception\NotFoundException;
7
// use Anax\Route\Exception\InternalErrorException;
8
/**
9
 * A sample controller to show how a controller class can be implemented.
10
 * The controller will be injected with $di if implementing the interface
11
 * ContainerInjectableInterface, like this sample class does.
12
 * The controller is mounted on a particular route and can then handle all
13
 * requests for that mount point.
14
 *
15
 * @SuppressWarnings(PHPMD.TooManyPublicMethods)
16
 */
17
class WeatherIpController implements ContainerInjectableInterface
18
{
19
    use ContainerInjectableTrait;
20
21
22
    protected $apiKey;
23
    protected $darkKey;
24
25 1
    public function __construct()
26
    {
27 1
        $apiKeys = require ANAX_INSTALL_PATH . "/config/apiKeys.php";
28 1
        $this->apiKey = $apiKeys["ipstack"];
29 1
        $this->darkKey = $apiKeys["darksky"];
30
31
32 1
    }
33
    /**
34
     * @var string $db a sample member variable that gets initialised
35
     */
36
    private $ipAddress;
37
    private $time;
38
    private $object;
39
    private $WeatherRequest;
40
    /**
41
     * Display the view
42
     *
43
     * @return object
44
     */
45 1
    public function indexAction() : object
46
    {
47 1
        $title = "Check IP";
48 1
        $page = $this->di->get("page");
49 1
        $request = $this->di->get("request");
50 1
        $this->WeatherRequest = $this->di->get("WeatherRequest");
51 1
        $this->ipAddress = $request->getGet("ip");
52 1
        $this->time = $request->getGet("time");
53 1
        $currentIp = $this->ipAddress;
0 ignored issues
show
Unused Code introduced by
The assignment to $currentIp is dead and can be removed.
Loading history...
54 1
        $this->object = new WeatherIp();
55 1
        $currentIp = $this->object->validateIp($this->ipAddress);
56 1
        $accessKey  = $this->apiKey;
57 1
        $details = $this->WeatherRequest->curlJson('http://api.ipstack.com/'.$currentIp.'?access_key='.$accessKey);
58 1
        $weather = $this->multiCurlJson($details);
59 1
        $data["details"] = $details;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.
Loading history...
60 1
        $data["weather"] = $weather;
61 1
        $data["currentIp"] = $currentIp;
62 1
        $page->add("index", $data);
63 1
        return $page->render([
64 1
            "title" => $title,
65
        ]);
66
    }
67
68 1
    public function multiCurlJson($details)
69
    {
70 1
        $accessKey  = $this->darkKey;
71 1
        $multiRequests = [];
72
        #future weather
73 1
        if ($this->time === "future") {
74
            for ($i=0; $i < 7; $i++) {
75
                $unixTime = time() + ($i * 24 * 60 * 60);
76
                $multiRequests[] = 'https://api.darksky.net/forecast/'.$accessKey .'/'.$details['latitude'].','.$details['longitude'].','.$unixTime.'?exclude=minutely,hourly,daily,flags&units=si';
77
            }
78
        }
79
        #previous weather
80 1
        if ($this->time === "past") {
81
            for ($i=0; $i < 30; $i++) {
82
                $unixTime = time() - ($i * 24 * 60 * 60);
83
                $multiRequests[] = 'https://api.darksky.net/forecast/'.$accessKey .'/'.$details['latitude'].','.$details['longitude'].','.$unixTime.'?exclude=minutely,hourly,daily,flags&units=si';
84
            }
85
        }
86 1
        $weather = $this->WeatherRequest->multiRequest($multiRequests);
87 1
        foreach ($weather as $key => $value) {
88
            $weather[$key] = json_decode(stripslashes($value), true);
89
        }
90 1
        return $weather;
91
    }
92
}
93