Issues (32)

src/Weather/WeatherController.php (4 issues)

Labels
Severity
1
<?php
2
3
namespace Anax\Weather;
4
5
use Anax\Commons\ContainerInjectableInterface;
6
use Anax\Commons\ContainerInjectableTrait;
7
use Anax\Curl\Curl as Curl;
8
9
// use Anax\Route\Exception\ForbiddenException;
10
// use Anax\Route\Exception\NotFoundException;
11
// use Anax\Route\Exception\InternalErrorException;
12
13
/**
14
 * A sample controller to show how a controller class can be implemented.
15
 * The controller will be injected with $di if implementing the interface
16
 * ContainerInjectableInterface, like this sample class does.
17
 * The controller is mounted on a particular route and can then handle all
18
 * requests for that mount point.
19
 *
20
 * @SuppressWarnings(PHPMD.TooManyPublicMethods)
21
 */
22
class WeatherController implements ContainerInjectableInterface
23
{
24
    use ContainerInjectableTrait;
25
26
27 1
    public function indexAction() : object
28
    {
29
30 1
        $title = "Weather";
31
32 1
        $address = $_SERVER["REMOTE_ADDR"] ?? "No IP detected.";
33
34
35
36 1
        $page = $this->di->get("page");
37 1
        $session = $this->di->session;
0 ignored issues
show
Accessing session on the interface Psr\Container\ContainerInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
38
39 1
        $res = $session->get("res", null);
40 1
        $ipv = $session->get("ip", null);
41 1
        $geo = $session->get("geo", null);
42 1
        $apikey = $session->get("apikey", null);
43 1
        $weatherWeek = $session->get("weatherWeek", null);
44 1
        $weatherPast = $session->get("weatherPast", null);
45 1
        $latitude = $session->get("latitude", null);
46 1
        $longitude = $session->get("longitude", null);
47
48 1
        $session->set("res", null);
49 1
        $session->set("ip", null);
50 1
        $session->set("weatherWeek", null);
51 1
        $session->set("weatherPast", null);
52 1
        $session->set("geo", null);
53 1
        $session->set("latitude", null);
54 1
        $session->set("longitude", null);
55 1
        $session->set("apikey", null);
56
57
58
59
        $data = [
60 1
            "res" => $res,
61 1
            "ip" => $ipv,
62 1
            "address" => $address,
63 1
            "weatherWeek" => $weatherWeek,
64 1
            "weatherPast" => $weatherPast,
65 1
            "geo" => $geo,
66 1
            "latitude" => $latitude,
67 1
            "longitude" => $longitude,
68 1
            "apikey" => $apikey,
69
        ];
70
71 1
        $page->add("weather/verify", $data);
72
73 1
        return $page->render([
74 1
            "title" => $title,
75
        ]);
76
    }
77
78 1
    public function indexActionPost()
79
    {
80 1
        $request = $this->di->request;
0 ignored issues
show
Accessing request on the interface Psr\Container\ContainerInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
81 1
        $response = $this->di->response;
0 ignored issues
show
Accessing response on the interface Psr\Container\ContainerInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
82 1
        $session = $this->di->session;
0 ignored issues
show
Accessing session on the interface Psr\Container\ContainerInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
83
84 1
        $doVerify = $request->getPost("verify", null);
85 1
        $address = $request->getPost("ip", null);
86 1
        $latitude = $request->getPost("latitude", null);
87 1
        $longitude = $request->getPost("longitude", null);
88
89 1
        $check = $this->di->get("validator");
90
91 1
        $weather = new Curl();
92
93
94 1
        if ($doVerify && $address) {
95 1
            $result = $check->checkIpv($address);
96
97 1
            $geotag = $check->geoTag($address);
98
99
100 1
            $fetchWeather = $weather->weather($geotag->latitude, $geotag->longitude);
101 1
            $fetchPastWeather = $weather->pastWeather($geotag->latitude, $geotag->longitude);
102
103 1
            $data = $fetchWeather->daily;
104
105 1
            $pastData = $fetchPastWeather;
106
107 1
            $session->set("res", $result);
108 1
            $session->set("ip", $address);
109 1
            $session->set("weatherWeek", $data);
110 1
            $session->set("weatherPast", $pastData);
111 1
            $session->set("latitude", $geotag->latitude);
112 1
            $session->set("longitude", $geotag->longitude);
113 1
            $session->set("apikey", $weather->getApiKey());
114
        }
115
116 1
        if ($doVerify && $latitude) {
117 1
            $verifyCoord = $check->verifyGeo($latitude, $longitude);
118
119 1
            if ($verifyCoord) {
120 1
                $fetchWeather = $weather->weather($latitude, $longitude);
121 1
                $fetchPastWeather = $weather->pastWeather($latitude, $longitude);
122
123 1
                $data = $fetchWeather->daily;
124 1
                $pastData = $fetchPastWeather;
125
126 1
                $valid = "The coordinates $latitude, $longitude are valid.";
127
128 1
                $session->set("geo", $valid);
129 1
                $session->set("weatherWeek", $data);
130 1
                $session->set("weatherPast", $pastData);
131 1
                $session->set("latitude", $latitude);
132 1
                $session->set("longitude", $longitude);
133 1
                $session->set("apikey", $weather->getApiKey());
134
            } else {
135
                $session->set("geo", "The coordinates are invalid. Try again.");
136
            }
137
        }
138 1
        return $response->redirect("weather");
139
    }
140
}
141