Issues (42)

src/Controller/KrisWeatherController.php (2 issues)

1
<?php
2
3
namespace Kris3XIQ\Controller;
4
5
use Anax\Commons\ContainerInjectableInterface;
6
use Anax\Commons\ContainerInjectableTrait;
7
use Kris3XIQ\Service\APIService;
0 ignored issues
show
The type Kris3XIQ\Service\APIService was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Kris3XIQ\Weather\Weather;
9
10
// use Anax\Route\Exception\ForbiddenException;
11
// use Anax\Route\Exception\NotFoundException;
12
// use Anax\Route\Exception\InternalErrorException;
13
14
/**
15
 * A sample controller to show how a controller class can be implemented.
16
 * The controller will be injected with $di if implementing the interface
17
 * ContainerInjectableInterface, like this sample class does.
18
 * The controller is mounted on a particular route and can then handle all
19
 * requests for that mount point.
20
 *
21
 * @SuppressWarnings(PHPMD.TooManyPublicMethods)
22
 */
23
class KrisWeatherController implements ContainerInjectableInterface
24
{
25
    use ContainerInjectableTrait;
26
27
    /**
28
     * This is the index method action, it handles:
29
     * ANY METHOD mountpoint
30
     * ANY METHOD mountpoint/
31
     * ANY METHOD mountpoint/index
32
     *
33
     * @SuppressWarnings(PHPMD.UnusedLocalVariable)
34
     *
35
     * @return object
36
     */
37
    public function indexAction() : object
38
    {
39
        // Setting up variables.
40
        $title = "Weather service";
41
        $page = $this->di->get("page");
42
43
        // Get services to load and their API-keys.
44
        $service = $this->di->get("api-service");
45
        $service->setServiceToLoad("openweathermap");
46
        $owmApiKey = $service->getKeyToService();
47
        $service->setServiceToLoad("ipstack");
48
        $ipsApiKey = $service->getKeyToService("ipstack");
49
50
        // Setting up Weather
51
        $weather = new Weather();
52
        $weather->setKeyChain(array("openweathermap" => $owmApiKey, "ipstack" => $ipsApiKey));
53
        
54
        // Session variables
55
        $weatherCurrentJSON = $_SESSION["weatherCurrentJSON"] ?? null;
56
        $weatherHistoryJSON = $_SESSION["weatherHistoryJSON"] ?? null;
57
58
        if ($weatherHistoryJSON) {
59
            $pastOne = json_decode($weatherHistoryJSON["past_days"]["past_01"], true);
60
            $pastTwo = json_decode($weatherHistoryJSON["past_days"]["past_02"], true);
61
            $pastThree = json_decode($weatherHistoryJSON["past_days"]["past_03"], true);
62
            $pastFour = json_decode($weatherHistoryJSON["past_days"]["past_04"], true);
63
            $pastFive = json_decode($weatherHistoryJSON["past_days"]["past_05"], true);
64
        }
65
66
        $data = [
67
            "key" => $keyToService ?? null,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $keyToService seems to never exist and therefore isset should always be false.
Loading history...
68
            "weatherCurrentJSON" => $weatherCurrentJSON ?? null,
69
            "weatherHistoryJSON" => $weatherHistoryJSON ?? null,
70
            "pastOne" => $pastOne ?? null,
71
            "pastTwo" => $pastTwo ?? null,
72
            "pastThree" => $pastThree ?? null,
73
            "pastFour" => $pastFour ?? null,
74
            "pastFive" => $pastFive ?? null,
75
            "latitude" => $weatherCurrentJSON["coord"]["lat"] ?? null,
76
            "longitude" => $weatherCurrentJSON["coord"]["lon"] ?? null,
77
        ];
78
79
        $page->add("weather/weather-service", $data);
80
            
81
        return $page->render([
82
            "title" => $title,
83
        ]);
84
    }
85
86
    /**
87
     * This handles POST request
88
     *
89
     * @SuppressWarnings(PHPMD.UnusedLocalVariable)
90
     *
91
     * @return object
92
     */
93
    public function indexActionPost() : object
94
    {
95
        // Set variables
96
        $response = $this->di->get("response");
97
        $request = $this->di->get("request");
98
        $service = $this->di->get("api-service");
99
100
        // Grab post variables
101
        $input = $request->getPost("input");
102
103
        // If user didnt input anything, redirect back.
104
        if ($input == "") {
105
            $data = [
106
                "cod" => 404,
107
            ];
108
            $_SESSION["weatherCurrentJSON"] = $data;
109
            return $response->redirect("weather");
110
        }
111
112
        // Setting up Weather
113
        $weather = new Weather();
114
        $service->setServiceToLoad("openweathermap");
115
        $owmApiKey = $service->getKeyToService();
116
        $service->setServiceToLoad("ipstack");
117
        $ipsApiKey = $service->getKeyToService("ipstack");
118
        $weather->setKeyChain(array("openweathermap" => $owmApiKey, "ipstack" => $ipsApiKey));
119
        
120
        $filter = $weather->isIpAddress($input);
121
        if ($filter) {
122
            $loc = $weather->ipTrackWithGeolocation($input);
123
            $location = $loc["city"] . ", " . $loc["country_code"];
124
            $weatherCurrentJSON = $weather->getWeatherCurrentFromAPI($location);
125
            $weatherHistoryJSON = $weather->getWeatherHistoryFromAPI($loc["latitude"], $loc["longitude"]);
126
        } else {
127
            $weather->setApiKey($owmApiKey);
128
            $isVerified = $weather->verifyLocation($input);
129
            if ($isVerified != "404") {
130
                $weatherCurrentJSON = $weather->getWeatherCurrentFromAPI($input);
131
                $lon = $weatherCurrentJSON["coord"]["lon"];
132
                $lat = $weatherCurrentJSON["coord"]["lat"];
133
                $weatherHistoryJSON = $weather->getWeatherHistoryFromAPI($lat, $lon);
134
            } else {
135
                $data = [
136
                    "cod" => 404,
137
                ];
138
                $_SESSION["weatherCurrentJSON"] = $data;
139
                $_SESSION["weatherHistoryJSON"] = null;
140
                return $response->redirect("weather");
141
            }
142
        }
143
144
        // Set session variables
145
        $_SESSION["weatherCurrentJSON"] = $weatherCurrentJSON;
146
        $_SESSION["weatherHistoryJSON"] = $weatherHistoryJSON;
147
    
148
        // Return di->response object to redirect to ip-geo-locator.
149
        return $response->redirect("weather");
150
    }
151
}
152