WeatherReportController   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 185
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 75
c 1
b 0
f 0
dl 0
loc 185
ccs 72
cts 72
cp 1
rs 10
wmc 11

5 Methods

Rating   Name   Duplication   Size   Complexity  
A geoActionGet() 0 36 3
A initialize() 0 8 1
A ipActionGet() 0 45 3
A indexAction() 0 21 3
A catchAll() 0 11 1
1
<?php
2
3
namespace Nihl\WeatherReport;
4
5
use Anax\Commons\ContainerInjectableInterface;
6
use Anax\Commons\ContainerInjectableTrait;
7
8
/**
9
 * Weather report
10
 *
11
 */
12
class WeatherReportController implements ContainerInjectableInterface
13
{
14
    use ContainerInjectableTrait;
15
16
17
    /**
18
     * @var object $weatherreport   instance of WeatherReport class
19
     */
20
    private $weatherreport;
21
    private $ipvalidator;
22
    private $request;
23
    private $geotag;
24
    private $page;
25
26
    /**
27
     * Initiate IPValidator
28
     *
29
     * @return void
30
     */
31 10
    public function initialize() : void
32
    {
33
        // Use to initialise member variables.
34 10
        $this->weatherreport = $this->di->get("weatherreport");
35 10
        $this->ipvalidator = $this->di->get("ipvalidator");
36 10
        $this->request = $this->di->get("request");
37 10
        $this->geotag = $this->di->get("geotag");
38 10
        $this->page = $this->di->get("page");
39 10
    }
40
41
42
    /**
43
     * Index action get
44
     *
45
     * @return object
46
     */
47 3
    public function indexAction() : object
48
    {
49 3
        $title = "Weather Report";
50
51
        // If get is set
52 3
        $search = $this->request->getGet("search", null);
53
54
        // Render index page
55 3
        $this->page->add("nihl/weatherreport/index", ["search" => $search]);
56
57 3
        if ($search == "ip") {
58 1
            $userIP = $this->ipvalidator->getUserIP($this->request->getServer());
59 1
            $this->page->add("nihl/weatherreport/ipform", ["ip" => $userIP]);
60
        }
61
62 3
        if ($search == "geo") {
63 1
            $this->page->add("nihl/weatherreport/geoform", []);
64
        }
65
66 3
        return $this->page->render([
67 3
            "title" => $title
68
        ]);
69
    }
70
71
72
73
    /**
74
     * Route for IP-based weather search
75
     *
76
     * @return object
77
     */
78 3
    public function ipActionGet() : object
79
    {
80 3
        $title = "Weather Report";
81
82
        // Retrieve GET-data
83 3
        $ip = $this->request->getGet("ip", null);
84 3
        $tp = $this->request->getGet("timeperiod", null);
85
86 3
        $data = [];
87
88
        // Fetch data about IP address from IP-stack
89 3
        $geo = $this->geotag->getIPData($ip);
90 3
        $lat = $geo["latitude"] ?? null;
91 3
        $lon = $geo["longitude"] ?? null;
92
93
        // Get current weather
94 3
        $data["current"] = $this->weatherreport->getCurrentWeatherByCoord($lat, $lon);
95
96 3
        if ($data["current"]["cod"] != 200) {
97 1
            $title .= " | Not Found";
98 1
            $this->page->add("nihl/weatherreport/result", [
99 1
                "message" => $data["current"]["message"]
100
            ]);
101 1
            $this->page->add("nihl/weatherreport/ipform", ["ip" => $ip]);
102
103 1
            return $this->page->render([ "title" => $title ]);
104
        }
105
106 2
        $this->page->add("nihl/weatherreport/current", $data);
107
108
        // Get weather history or forecast based in timeperiod variable in GET
109 2
        if ($tp == "history") {
110 1
            $data["weather"] = $this->weatherreport->getWeatherHistory($lat, $lon);
111 1
            $this->page->add("nihl/weatherreport/history", $data);
112
        } else {
113 1
            $data["weather"] = $this->weatherreport->getWeatherForecast($lat, $lon);
114 1
            $this->page->add("nihl/weatherreport/forecast", $data);
115
        }
116
117
        // Add map to geotag
118 2
        $geo["map"] = $this->geotag->renderMap($lat, $lon);
119 2
        $this->page->add("nihl/ip-validator/geotag", $geo);
120
121 2
        return $this->page->render([
122 2
            "title" => $title
123
        ]);
124
    }
125
126
    /**
127
     * Route for coordinates-based weather search
128
     *
129
     * @return object
130
     */
131 3
    public function geoActionGet() : object
132
    {
133 3
        $title = "Weather Report";
134 3
        $data = [];
135
136
        // Retrieve GET-data
137 3
        $lat = $this->request->getGet("lat", null);
138 3
        $lon = $this->request->getGet("lon", null);
139 3
        $tp = $this->request->getGet("timeperiod", null);
140
141
        // Get current weather
142 3
        $data["current"] = $this->weatherreport->getCurrentWeatherByCoord($lat, $lon);
143
144 3
        if ($data["current"]["cod"] != 200) {
145 1
            $title .= " | Not Found";
146 1
            $this->page->add("nihl/weatherreport/result", [
147 1
                "message" => $data["current"]["message"]
148
            ]);
149 1
            $this->page->add("nihl/weatherreport/geoform", []);
150
151 1
            return $this->page->render([ "title" => $title ]);
152
        }
153
154 2
        $this->page->add("nihl/weatherreport/current", $data);
155
156
        // Get weather history or forecast based in timeperiod variable in GET
157 2
        if ($tp == "history") {
158 1
            $data["weather"] = $this->weatherreport->getWeatherHistory($lat, $lon);
159 1
            $this->page->add("nihl/weatherreport/history", $data);
160
        } else {
161 1
            $data["weather"] = $this->weatherreport->getWeatherForecast($lat, $lon);
162 1
            $this->page->add("nihl/weatherreport/forecast", $data);
163
        }
164
165 2
        return $this->page->render([
166 2
            "title" => $title
167
        ]);
168
    }
169
170
171
172
    /**
173
     * Adding an optional catchAll() method will catch all actions sent to the
174
     * router. You can then reply with an actual response or return void to
175
     * allow for the router to move on to next handler.
176
     * A catchAll() handles the following, if a specific action method is not
177
     * created:
178
     * ANY METHOD mountpoint/**
179
     *
180
     * @param array $args as a variadic parameter.
181
     *
182
     * @return mixed
183
     *
184
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
185
     */
186 1
    public function catchAll(...$args)
0 ignored issues
show
Unused Code introduced by
The parameter $args is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

186
    public function catchAll(/** @scrutinizer ignore-unused */ ...$args)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
187
    {
188 1
        $title = "IP-Validator | Route not found";
189 1
        $this->page = $this->di->get("page");
190 1
        $path = $this->di->get("request")->getRoute();
191 1
        $this->page->add("nihl/ip-validator/error", [
192 1
            "path" => $path
193
        ]);
194
195 1
        return $this->page->render([
196 1
            "title" => $title
197
        ]);
198
    }
199
}
200