Issues (16)

src/Weather/WeatherIpJsonController.php (2 issues)

1
<?php
2
3
namespace Edward\Weather;
4
5
use Anax\Commons\ContainerInjectableInterface;
6
use Anax\Commons\ContainerInjectableTrait;
7
8
// use Anax\Route\Exception\ForbiddenException;
9
// use Anax\Route\Exception\NotFoundException;
10
// use Anax\Route\Exception\InternalErrorException;
11
12
/**
13
 * A sample controller to show how a controller class can be implemented.
14
 * The controller will be injected with $di if implementing the interface
15
 * ContainerInjectableInterface, like this sample class does.
16
 * The controller is mounted on a particular route and can then handle all
17
 * requests for that mount point.
18
 *
19
 * @SuppressWarnings(PHPMD.TooManyPublicMethods)
20
 */
21
class WeatherIpJsonController extends WeatherIpController implements ContainerInjectableInterface
22
{
23
    use ContainerInjectableTrait;
24
25
    /**
26
     * @var string $db a sample member variable that gets initialised
27
     */
28
    private $ipAddress;
29
    private $time;
30
    private $object;
31
    private $requester;
32
33
34
    /**
35
     * Display the view
36
     *
37
     * @return object
38
     */
39 1
    public function indexAction() : object
40
    {
41 1
        $title = "Check IP (JSON)";
42
43 1
        $page = $this->di->get("page");
44 1
        $request = $this->di->get("request");
45 1
        $this->requester = $this->di->get("requester");
46
47 1
        $this->ipAddress = $request->getGet("ip");
48 1
        $this->time = $request->getGet("time");
49
50 1
        $currentIp = $this->ipAddress;
0 ignored issues
show
The assignment to $currentIp is dead and can be removed.
Loading history...
51 1
        $this->object = new WeatherIp();
52
53 1
        $currentIp = $this->object->validateIp($this->ipAddress);
54
55
56 1
        $accessKey  = '49a95e2b98f16776978bbf2d3097c542';
57 1
        $details = $this->requester->curlJson('http://api.ipstack.com/'.$currentIp.'?access_key='.$accessKey);
58 1
        $weather = $this->multiCurlJson($details);
59
60 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...
61 1
        $data["weather"] = $weather;
62 1
        $data["currentIp"] = $currentIp;
63
64 1
        $page->add("anax/v2/weather/json", $data);
65
66 1
        return $page->render([
67 1
            "title" => $title,
68
        ]);
69
    }
70
71
72 1
    public function multiCurlJson($details)
73
    {
74 1
        $accessKey  = '29b7a65dbbc991295815b55e7a37f93b';
75
76 1
        $multiRequests = [];
77
        #future weather
78 1
        if ($this->time === "future") {
79
            for ($i=0; $i < 7; $i++) {
80
                $unixTime = time() + ($i * 24 * 60 * 60);
81
                $multiRequests[] = 'https://api.darksky.net/forecast/'.$accessKey .'/'.$details['latitude'].','.$details['longitude'].','.$unixTime.'?exclude=minutely,hourly,daily,flags';
82
            }
83
        }
84
85
        #previous weather
86 1
        if ($this->time === "past") {
87
            for ($i=0; $i < 30; $i++) {
88
                $unixTime = time() - ($i * 24 * 60 * 60);
89
                $multiRequests[] = 'https://api.darksky.net/forecast/'.$accessKey .'/'.$details['latitude'].','.$details['longitude'].','.$unixTime.'?exclude=minutely,hourly,daily,flags';
90
            }
91
        }
92
93
94 1
        $weather = $this->requester->multiRequest($multiRequests);
95
96 1
        foreach ($weather as $key => $value) {
97
            $weather[$key] = json_decode(stripslashes($value), true);
98
        }
99
100 1
        return $weather;
101
    }
102
}
103