JsonController::weatherActionPost()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2.032

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 11
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 19
ccs 8
cts 10
cp 0.8
crap 2.032
rs 9.9
1
<?php
2
3
namespace Lii\Controller;
4
5
use Anax\Commons\ContainerInjectableInterface;
6
use Anax\Commons\ContainerInjectableTrait;
7
use Lii\Model\IPValidator;
8
use Lii\Model\WeatherReport;
9
10
// use Anax\Route\Exception\ForbiddenException;
11
// use Anax\Route\Exception\NotFoundException;
12
// use Anax\Route\Exception\InternalErrorException;
13
14
/**
15
 * A sample JSON 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
class JsonController implements ContainerInjectableInterface
22
{
23
    use ContainerInjectableTrait;
24
25
    /**
26
     * @var string $db a sample member variable that gets initialised
27
     */
28
    public $db = "not active";
29
    public $validator = null;
30
    public $report = null;
31
32
    /**
33
     * The initialize method is optional and will always be called before the
34
     * target method/action. This is a convienient method where you could
35
     * setup internal properties that are commonly used by several methods.
36
     *
37
     * @return void
38
     */
39
    public function initialize() : void
40
    {
41
        // Use to initialise member variables.
42
        $this->db = "active";
43
        $service = $this->di->get("apikeys");
44
45
        $this->validator = new IPValidator($service->getIpKey());
46
        $this->report = new WeatherReport($service->getWeatherKey());
47
    }
48
49
    /**
50
     * This is the index method action, it handles:
51
     * GET METHOD mountpoint
52
     * GET METHOD mountpoint/
53
     * GET METHOD mountpoint/index
54
     *
55
     * @return array
56
     */
57 1
    public function indexActionGet() : array
58
    {
59
        // Deal with the action and return a response.
60
        $json = [
61 1
            "message" => __METHOD__ . ", \$db is {$this->db}",
62
        ];
63 1
        return [$json];
64
    }
65
66
    /**
67
     * This is the index method action, it handles:
68
     * ANY METHOD mountpoint
69
     * ANY METHOD mountpoint/
70
     * ANY METHOD mountpoint/index
71
     *
72
     * @return object
73
     */
74 1
    public function infoAction() : object
75
    {
76 1
        $request = $this->di->get("request");
77 1
        $userIP = $request->getServer("HTTP_X_FORWARDED_FOR", "x.x.x.x");
78
79 1
        $page = $this->di->get("page");
80
        $data = [
81 1
            "userIP" => $userIP,
82
        ];
83
84 1
        $page->add("Lii/api-info", $data);
85
86 1
        return $page->render([
87 1
            "title" => __METHOD__,
88
        ]);
89
    }
90
91
    /**
92
     * This is the validate method action, it handles:
93
     * POST METHOD mountpoint/validate
94
     *
95
     * @return array
96
     */
97 1
    public function validateActionPost() : array
98
    {
99 1
        $request = $this->di->get("request");
100 1
        $inputIP = $request->getPost("ip");
101
102
//         $validator = new IPValidator();
103 1
        $json = $this->validator->validateIPJSON($inputIP);
104
105 1
        return $json;
106
    }
107
108
    /**
109
     * This is the validate method action, it handles:
110
     * GET METHOD mountpoint/validate
111
     *
112
     * @return array
113
     */
114 1
    public function validateActionGet() : array
115
    {
116 1
        $request = $this->di->get("request");
117 1
        $inputIP = $request->getGet("ip");
118
119
//         $validator = new IPValidator();
120 1
        $json = $this->validator->validateIPJSON($inputIP);
121
122 1
        return $json;
123
    }
124
125
    /**
126
     * This is the validate method action, it handles:
127
     * GET METHOD mountpoint/location
128
     *
129
     * @return array
130
     */
131 2
    public function locationActionGet() : array
132
    {
133 2
        $request = $this->di->get("request");
134 2
        $inputIP = $request->getGet("ip");
135
136
//         $validator = new IPValidator();
137 2
        $json = $this->validator->locateIPJSON($inputIP);
138
139 2
        return $json;
140
    }
141
    /**
142
     * This is the validate method action, it handles:
143
     * POST METHOD mountpoint/location
144
     *
145
     * @return array
146
     */
147 1
    public function locationActionPost() : array
148
    {
149 1
        $request = $this->di->get("request");
150 1
        $inputIP = $request->getPost("ip");
151
152
//         $validator = new IPValidator();
153 1
        $json = $this->validator->locateIPJSON($inputIP);
154
155 1
        return $json;
156
    }
157
    /**
158
     * This is the validate method action, it handles:
159
     * POST METHOD mountpoint/weather
160
     *
161
     * @return array
162
     */
163 1
    public function weatherActionPost() : array
164
    {
165 1
        $request = $this->di->get("request");
166 1
        $inputIP = $request->getPost("ip");
167
168
//         $validator = new IPValidator();
169 1
        $location = $this->validator->locateIP($inputIP);
170 1
        $json = [];
171
172 1
        if ($location == 'Not valid IP-address.') {
173
            $json = [
174
                "error" => "Not valid IP-address.",
175
            ];
176
            $json = [$json];
177
        } else {
178 1
            $json = $this->report->getJsonWeather($this->report->getHistoricDates(), $location["latitude"], $location["longitude"]);
179
        }
180
181 1
        return $json;
182
    }
183
}
184