WeatherReportAPIController::catchAll()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 11
ccs 8
cts 8
cp 1
crap 1
rs 10
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 WeatherReportAPIController 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 11
    public function initialize() : void
32
    {
33
        // Use to initialise member variables.
34 11
        $this->weatherreport = $this->di->get("weatherreport");
35 11
        $this->ipvalidator = $this->di->get("ipvalidator");
36 11
        $this->request = $this->di->get("request");
37 11
        $this->geotag = $this->di->get("geotag");
38 11
        $this->page = $this->di->get("page");
39 11
    }
40
41
    /**
42
     * This is the index method action, it handles:
43
     * GET METHOD mountpoint
44
     * GET METHOD mountpoint/
45
     * GET METHOD mountpoint/index
46
     *
47
     * @return array
48
     */
49 1
    public function indexAction()
50
    {
51 1
        $title = "Weather report REST API";
52
53
        // Get users IP-address
54 1
        $userIP = $this->ipvalidator->getUserIP($this->request->getServer());
55
56 1
        $this->page->add("nihl/weatherreport/api/index", ["ip" => $userIP]);
57
58 1
        return $this->page->render([
59 1
            "title" => $title
60
        ]);
61
    }
62
63
    /**
64
     * This is the ip post method action, it handles:
65
     * POST mountpoint/ip
66
     *
67
     * @return array
68
     */
69 4
    public function ipActionPost()
70
    {
71
        $error = [
72 4
            "code" => null,
73
            "message" => null
74
        ];
75
76 4
        $json = [];
77
78 4
        $ip = $this->request->getPost("ip", null);
79 4
        if (!$ip) {
80 1
            $error["code"] = 400;
81 1
            $error["message"] = "No IP was received.";
82 1
            return [$error];
83
        }
84
85
        // Get IP information
86 3
        $json["geo"] = $this->geotag->getIPData($ip);
87
88 3
        if ($json["geo"]["type"] != "ipv4") {
89 1
            $error["code"] = 400;
90 1
            $error["message"] = "IP can't be connected to a geographic position";
91 1
            return [$error];
92
        }
93
94 2
        $lat = $json["geo"]["latitude"];
95 2
        $lon = $json["geo"]["longitude"];
96
97 2
        $json["map"] = $this->geotag->getMap($lat, $lon);
98
99 2
        $json["weather"] = $this->weatherreport->getCurrentWeatherByCoord($lat, $lon);
100
101
102 2
        if ($json["weather"]["cod"] == 200) {
103 2
            $timeperiod = $this->request->getPost("timeperiod", null);
104
105 2
            if ($timeperiod == "history") {
106 1
                $json["weather"]["history"] = $this->weatherreport->getWeatherHistory($lat, $lon);
107
            } else {
108 1
                $json["weather"]["forecast"] = $this->weatherreport->getWeatherForecast($lat, $lon);
109
            }
110
        }
111
112 2
        return [$json];
113
    }
114
115
    /**
116
     * This is the geo post method action, it handles:
117
     * POST mountpoint/geo
118
     *
119
     * @return array
120
     */
121 5
    public function geoActionPost()
122
    {
123
        $error = [
124 5
            "code" => null,
125
            "message" => null
126
        ];
127
128 5
        $json = [];
129
130 5
        $lat = $this->request->getPost("lat", null);
131 5
        $lon = $this->request->getPost("lon", null);
132
133
        // Get IP
134 5
        if (!$lat || !$lon) {
135 1
            $error["code"] = 400;
136 1
            $error["message"] = "Latitude or longitude missing in request";
137 1
            return [$error];
138
        }
139
140 4
        $json["map"] = $this->geotag->getMap($lat, $lon);
141
142 4
        $json["weather"] = $this->weatherreport->getCurrentWeatherByCoord($lat, $lon);
143
144
145 4
        if ($json["weather"]["cod"] == 200) {
146 2
            $timeperiod = $this->request->getPost("timeperiod", null);
147
148 2
            if ($timeperiod == "history") {
149 1
                $json["weather"]["history"] = $this->weatherreport->getWeatherHistory($lat, $lon);
150
            } else {
151 1
                $json["weather"]["forecast"] = $this->weatherreport->getWeatherForecast($lat, $lon);
152
            }
153
        }
154 4
        return [$json];
155
    }
156
157
    /**
158
     * Adding an optional catchAll() method will catch all actions sent to the
159
     * router. You can then reply with an actual response or return void to
160
     * allow for the router to move on to next handler.
161
     * A catchAll() handles the following, if a specific action method is not
162
     * created:
163
     * ANY METHOD mountpoint/**
164
     *
165
     * @param array $args as a variadic parameter.
166
     *
167
     * @return mixed
168
     *
169
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
170
     */
171 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

171
    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...
172
    {
173 1
        $title = "WeatherReport | Route not found";
174 1
        $this->page = $this->di->get("page");
175 1
        $path = $this->di->get("request")->getRoute();
176 1
        $this->page->add("nihl/weatherreport/error", [
177 1
            "path" => $path
178
        ]);
179
180 1
        return $this->page->render([
181 1
            "title" => $title
182
        ]);
183
    }
184
}
185