Passed
Push — master ( 86479e...2843a8 )
by Marcus
02:54
created

WeatherController::historyActionGet()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 32
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3.0884

Importance

Changes 0
Metric Value
cc 3
eloc 15
nc 3
nop 4
dl 0
loc 32
ccs 11
cts 14
cp 0.7856
crap 3.0884
rs 9.7666
c 0
b 0
f 0
1
<?php
2
3
namespace Mahw17\Weather;
4
5
use Anax\Commons\ContainerInjectableInterface;
6
use Anax\Commons\ContainerInjectableTrait;
7
8
/**
9
 * Controller to handle user login.
10
 */
11
class WeatherController implements ContainerInjectableInterface
12
{
13
    use ContainerInjectableTrait;
14
15
16
    // Create and configure new db-object
17
18
    /**
19
     * Display the about page.
20
     *
21
     * @return object
22
     */
23 1
    public function indexActionGet() : object
24
    {
25
26
        // Load framework services
27 1
        $page = $this->di->get("page");
28 1
        $session = $this->di->get("session");
29
30 1
        $ipObj = $this->di->get("weather")->ipObj;
31
32
        // Set navbar active
33 1
        $session->set('navbar', 'weather');
34
35
        // Collect data
36 1
        $currentIp = $ipObj->getIpAddress();
37
38
        $data = [
39 1
            "title" => "Väder | ramverk1",
40 1
            "intro_mount"   => 'Väder',
41 1
            "intro_path"    => 'formulär',
42 1
            "currentIp"     => $currentIp
43
        ];
44
45
        // Add and render views
46 1
        $page->add("mahw17/intro/subintro", $data, "subintro");
47 1
        $page->add("mahw17/weather/form", $data, "main");
48
49 1
        return $page->render($data);
50
    }
51
52
    /**
53
     * Retrive posted form.
54
     *
55
     */
56 3
    public function indexActionPost($test = false)
57
    {
58
        // Load framework services
59 3
        $page = $this->di->get("page");
60 3
        $request = $this->di->get("request");
61 3
        $response = $this->di->get("response");
62 3
        $session = $this->di->get("session");
63 3
        $weather = $this->di->get("weather");
64
65
        //
66 3
        $ipValidation = $weather->ipObj;
67
68
        // Validate coord (from posted)
69 3
        $coord = $request->getPost('coord', null);
70 3
        $coord = $weather->validateCoord($coord);
71
72
        // If none valide coords by post check posted ip-address for coords
73 3
        if (!$coord) {
74 1
            $ipAddress = $request->getPost('ip', null);
75 1
            $coord = $ipValidation->validateCoord($ipAddress);
76
        }
77
78
        // If neither coords by post or thru the ip is valid
79 3
        if (!$coord) {
80
            // Error message!
81 1
            return $response->redirect("error/weather");
82
            // return false;
83
        }
84
85
        // Type of data (forecast/history)
86 2
        $weaterType = $request->getPost('weather', null);
87
88
        // Fetch API-resultset
89 2
        $weatherInfo = null;
90 2
        $day = time();
91 2
        if ($weaterType === "forecast") {
92 1
            $weatherInfo = $weather->weatherForecast($coord);
93
        } else {
94 1
            $weatherInfo = $weather->weatherHistory($day, $coord);
95
        }
96
97
        // Set navbar active
98 2
        $session->set('navbar', 'weather');
99
100
        // Collect data
101
        $data = [
102 2
            "title"         => "Väder | ramverk1",
103 2
            "intro_mount"   => 'Prognos',
104 2
            "intro_path"    => 'resultat',
105 2
            "weatherType"   => $weaterType,
106 2
            "weatherInfo"   => $weatherInfo
107
        ];
108
109
        // Add and render views
110 2
        $page->add("mahw17/intro/subintro", $data, "subintro");
111 2
        $page->add("mahw17/weather/result", $data, "main");
112
113 2
        return $test ? $data : $page->render($data);
114
    }
115
116
    /**
117
     * This is the index method action, it handles:
118
     * GET METHOD mountpoint
119
     * GET METHOD mountpoint/
120
     * GET METHOD mountpoint/index
121
     *
122
     * @return array
123
     */
124 1
    public function forecastActionGet($source = 'ip', $param1 = '194.103.20.10', $param2 = null) : array
125
    {
126
        // Load framework services
127 1
        $weather = $this->di->get("weather");
128
129
130
        // Collect data
131
132
        // Input as IP-address => convert to coordinates
133 1
        if (strtolower($source) === "ip") {
134
            // Load framework services
135 1
            $ipValidation = $weather->ipObj;
136 1
            $results = $ipValidation->validateIp($param1);
137
138 1
            if ($results['valid']) {
139
                $info = $ipValidation->ipInfo($param1);
140
                $param1 = $info->latitude;
141
                $param2 = $info->longitude;
142
            }
143
        }
144
145
        $coordinates = [
146 1
            "lat" => $param1,
147 1
            "lon" => $param2
148
        ];
149
150
151 1
        $results = $weather->weatherForecast($coordinates);
152
153
        // Deal with the action and return a response.
154
        $json = [
155 1
            'data' => $results
156
        ];
157 1
        return [$json];
158
    }
159
160
    /**
161
     * This is the index method action, it handles:
162
     * GET METHOD mountpoint
163
     * GET METHOD mountpoint/
164
     * GET METHOD mountpoint/index
165
     *
166
     * @return array
167
     */
168 1
    public function historyActionGet($day = 1542818124, $source = 'ip', $param1 = '194.103.20.10', $param2 = null) : array
169
    {
170
        // Load framework services
171 1
        $weather = $this->di->get("weather");
172
173
        // Collect data
174
        // Input as IP-address => convert to coordinates
175 1
        if (strtolower($source) === "ip") {
176
            // Load framework services
177 1
            $ipValidation = $weather->ipObj;
178 1
            $results = $ipValidation->validateIp($param1);
179
180 1
            if ($results['valid']) {
181
                $info = $ipValidation->ipInfo($param1);
182
                $param1 = $info->latitude;
183
                $param2 = $info->longitude;
184
            }
185
        }
186
187
        $coordinates = [
188 1
            "lat" => $param1,
189 1
            "lon" => $param2
190
        ];
191
192
193 1
        $results = $weather->weatherHistory($day, $coordinates);
194
195
        // Deal with the action and return a response.
196
        $json = [
197 1
            'data' => $results
198
        ];
199 1
        return [$json];
200
    }
201
}
202