GeoWeatherController::catchAll()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
nc 1
nop 1
dl 0
loc 5
c 1
b 0
f 0
cc 1
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
3
namespace Anax\Controller;
4
5
use Anax\Models;
6
use Anax\Commons\ContainerInjectableInterface;
7
use Anax\Commons\ContainerInjectableTrait;
8
9
/**
10
 *
11
 * @SuppressWarnings(PHPMD.TooManyPublicMethods)
12
 */
13
class GeoWeatherController implements ContainerInjectableInterface
14
{
15
    use ContainerInjectableTrait;
16
17
    private $darksky;
18
    private $ipValidator;
19
20 11
    public function initialize()
21
    {
22 11
        $this->darksky = $this->di->get("apiWeather");
23 11
        $this->ipValidator = $this->di->get("apiIpValidator");
24 11
    }
25
26 1
    public function indexActionGet()
27
    {
28 1
        $title = "Weather forecast";
29 1
        $page = $this->di->get("page");
30 1
        $request = $this->di->get("request");
31
32
        $data = [
33 1
            "title" => $title,
34 1
            "ip" => $request->getServer("REMOTE_ADDR", ""),
35 1
            "content" => ""
36
        ];
37 1
        $page->add("anax/weather/header", $data);
38 1
        $page->add("anax/weather/index", $data);
39 1
        $page->add("anax/weather/footer", $data);
40
41 1
        return $page->render([
42 1
            "title" => $title,
43
        ]);
44
    }
45
46 5
    public function indexActionPost()
47
    {
48 5
        $title = "Weather forecast";
49 5
        $page = $this->di->get("page");
50 5
        $request = $this->di->get("request");
51 5
        $location = $request->getPost("geo");
52 5
        $config = $request->getPost("weather");
53
54 5
        $data = $this->ipValidator->validateIp($location);
55 5
        $lat = "";
56 5
        $long = "";
57
58 5
        if ($data["valid"] == true) {
59 2
            $lat = $data["stack"]->{"latitude"};
60 2
            $long = $data["stack"]->{"longitude"};
61 2
            $weatherData = $this->darksky->getWeather($lat, $long, $config);
62
63 2
            $exists = true;
64
        } else {
65 3
            $splitLocation = explode(",", $location);
66 3
            if (count($splitLocation) == 2) {
67 2
                $lat = trim($splitLocation[0]);
68 2
                $long = trim($splitLocation[1]);
69
            }
70 3
            if (count($splitLocation) == 2 && is_numeric($lat) && is_numeric($long)) {
71 2
                if ($lat <= 90 && $lat >= -90 && $long >= -180 && $long <= 180) {
72 1
                    $weatherData = $this->darksky->getWeather($lat, $long, $config);
73 1
                    $exists = true;
74
                } else {
75 1
                    $weatherData = "";
76 2
                    $exists = false;
77
                }
78
            } else {
79 1
                $weatherData = "";
80 1
                $exists = false;
81
            }
82
        }
83
84 5
        if ($exists == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
85 3
            $mapNode = "var latitude = $lat; var longitude = $long;";
86
        } else {
87 2
            $mapNode = "";
88
        }
89
90
        $data = [
91 5
            "title" => $title,
92 5
            "weatherData" => $weatherData,
93 5
            "config" => $config,
94 5
            "exists" => $exists,
95 5
            "ip" => $location,
96 5
            "mapNode" => $mapNode
97
        ];
98 5
        $page->add("anax/weather/header", $data);
99 5
        $page->add("anax/weather/show", $data);
100 5
        $page->add("anax/weather/footer", $data);
101
102 5
        return $page->render([
103 5
            "title" => $title,
104
        ]);
105
    }
106
107
    /**
108
     * Action for REST API
109
     * GET mountpoint geo/json
110
     *
111
     * @return object
112
     */
113 1
    public function jsonAction(): object
114
    {
115 1
        $title = "Weather forecast in JSON";
116 1
        $request = $this->di->get("request");
117 1
        $page = $this->di->get("page");
118
119
        $data = [
120 1
            "title" => $title,
121 1
            "ip" => $request->getServer("REMOTE_ADDR", ""),
122 1
            "content" => ""
123
        ];
124
125 1
        $page->add("anax/weather/header", $data);
126 1
        $page->add("anax/weather/index", $data);
127 1
        $page->add("anax/weather/footer", $data);
128
129 1
        return $page->render($data);
130
    }
131
132
     /**
133
     * POST mountpoint ip/json
134
     *
135
     * @return object
136
     */
137 4
    public function jsonActionPOST()
138
    {
139 4
        $request = $this->di->get("request");
140 4
        $location = $request->getPost("geo");
141 4
        $config = $request->getPost("weather");
142
143 4
        $data = $this->ipValidator->validateIp($location);
144 4
        $lat = "";
145 4
        $long = "";
146
147 4
        if ($data["valid"] == true) {
148 1
            $lat = $data["stack"]->{"latitude"};
149 1
            $long = $data["stack"]->{"longitude"};
150 1
            $weatherData = $this->darksky->getWeather($lat, $long, $config);
151
152 1
            $exists = true;
0 ignored issues
show
Unused Code introduced by
The assignment to $exists is dead and can be removed.
Loading history...
153
        } else {
154 3
            $splitLocation = explode(",", $location);
155 3
            if (count($splitLocation) == 2) {
156 2
                $lat = trim($splitLocation[0]);
157 2
                $long = trim($splitLocation[1]);
158
            }
159 3
            if (count($splitLocation) == 2 && is_numeric($lat) && is_numeric($long)) {
160 2
                if ($lat <= 90 && $lat >= -90 && $long >= -180 && $long <= 180) {
161 1
                    $weatherData = $this->darksky->getWeather($lat, $long, $config);
162 1
                    $exists = true;
163
                } else {
164 1
                    $weatherData = "";
165 2
                    $exists = false;
166
                }
167
            } else {
168 1
                $weatherData = "";
169 1
                $exists = false;
170
            }
171
        }
172
173 4
        return json_encode($weatherData);
0 ignored issues
show
Bug Best Practice introduced by
The expression return json_encode($weatherData) returns the type string which is incompatible with the documented return type object.
Loading history...
174
    }
175
176
177
    /**
178
     * Adding an optional catchAll() method will catch all actions sent to the
179
     * router. You can then reply with an actual response or return void to
180
     * allow for the router to move on to next handler.
181
     * A catchAll() handles the following, if a specific action method is not
182
     * created:
183
     * ANY METHOD mountpoint/**
184
     *
185
     * @param array $args as a variadic parameter.
186
     *
187
     * @return mixed
188
     *
189
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
190
     */
191
    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

191
    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...
192
    {
193
        // Deal with the request and send an actual response, or not.
194
        //return __METHOD__ . ", \$db is {$this->db}, got '" . count($args) . "' arguments: " . implode(", ", $args);
195
        return;
196
    }
197
}
198