Passed
Push — main ( 3d8e8a...e61083 )
by Aron
02:37
created

WeatherController::generateData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 15
c 1
b 0
f 0
nc 2
nop 4
dl 0
loc 19
ccs 16
cts 16
cp 1
crap 2
rs 9.7666
1
<?php
2
3
namespace Anax\Controller;
4
5
namespace artes\Weather;
6
7
// use Anax\Commons\AppInjectableInterface;
8
// use Anax\Commons\AppInjectableTrait;
9
use Anax\Commons\ContainerInjectableInterface;
10
use Anax\Commons\ContainerInjectableTrait;
11
use Anax\Route\Exception\NotFoundException;
12
use artes\IP\IPGeotag;
13
use artes\IP\RealIP;
14
15
/**
16
 * A sample controller to show how a controller class can be implemented.
17
 * The controller will be injected with $app if implementing the interface
18
 * AppInjectableInterface, like this sample class does.
19
 * The controller is mounted on a particular route and can then handle all
20
 * requests for that mount point.
21
 *
22
 * @SuppressWarnings(PHPMD)
23
 */
24
class WeatherController implements ContainerInjectableInterface
25
{
26
    use ContainerInjectableTrait;
27
28
    /**
29
     * This is the index method action, it handles:
30
     * ANY METHOD mountpoint
31
     * ANY METHOD mountpoint/
32
     * ANY METHOD mountpoint/index
33
     *
34
     * @return object
35
     */
36 1
    public function indexActionGet() : object
37
    {
38 1
        $page = $this->di->get("page");
39 1
        $session = $this->di->get("session");
40 1
        $realip = new RealIP();
41 1
        $ipaddress = $realip->getRealIpAddr();
42
        $data = [
43 1
            "warning" => $session->get("warning"),
44 1
            "ip" => $ipaddress
45
        ];
46 1
        $session->destroy();
47
48 1
        $page->add(
49 1
            "weather/index",
50
            $data
51
        );
52
53 1
        return $page->render([
54 1
            "title" => "Weather",
55
        ]);
56
    }
57
58
    /**
59
     * This is the index method action, it handles:
60
     * ANY METHOD mountpoint
61
     * ANY METHOD mountpoint/
62
     * ANY METHOD mountpoint/index
63
     *
64
     * @return object
65
     */
66 1
    public function indexActionPost() : object
67
    {
68 1
        $ip = $this->di->get("ip");
69 1
        $request = $this->di->get("request");
70 1
        $response = $this->di->get("response");
71 1
        $session = $this->di->get("session");
72 1
        $validator = new ValidWeather($request, $ip);
73 1
        if ($validator->errormsg()) {
74 1
            $session->set("warning", $validator->errormsg());
75 1
            return $response->redirect("weather");
76
        }
77 1
        $page = $this->di->get("page");
78 1
        $lat = $request->getPost("latitud");
79 1
        $long = $request->getPost("longitud");
80 1
        $type = $request->getPost("infotyp");
81 1
        $userip = $request->getPost("userip");
82
83 1
        $data = $this->generateData($userip, $lat, $long, $type);
84 1
        $page->add(
85 1
            "weather/info",
86
            $data
87
        );
88
89 1
        if (!($lat && $long)) {
90 1
            $msg = "<p class='warning'>No geodata could be detected.</p>";
91 1
            $session->set("warning", $msg);
92 1
            return $response->redirect("weather");
93
        }
94
        return $page->render([
95
            "title" => "Weather",
96
        ]);
97
    }
98
99 1
    public function generateData($userip, $lat, $long, $type)
100
    {
101 1
        $ipkey = "";
102 1
        $weatherkey = "";
103
        // this loads $ipkey and $weatherkey
104 1
        include(ANAX_INSTALL_PATH . '/config/api/apikeys.php');
105 1
        $geotag = new IPGeotag($ipkey);
106 1
        $geoinfo = "";
107 1
        if ($userip) {
108 1
            $geoinfo = $geotag->checkdefaultip($userip);
109 1
            $lat = $geoinfo["latitude"] ?? "";
110 1
            $long = $geoinfo["longitude"] ?? "";
111 1
            $geoinfo = $geotag->checkinputip($userip);
112
        }
113 1
        $map = $geotag->printmap($lat, $long);
114 1
        $data = $this->getWeather($weatherkey, $lat, $long, $type);
115 1
        $data["map"] = $map;
116 1
        $data["geoinfo"] = $geoinfo;
117 1
        return $data;
118
    }
119
120
    /**
121
     * This is the index method action, it handles:
122
     * ANY METHOD mountpoint
123
     * ANY METHOD mountpoint/
124
     * ANY METHOD mountpoint/index
125
     *
126
     * @return array
127
     */
128 2
    public function getWeather($weatherkey, $lat, $long, $type) : array
129
    {
130 2
        $openweather = new OpenWeather($weatherkey, $lat, $long);
131 2
        $weatherinfo = $openweather->currentweather();
132 2
        if ($type === "historik") {
133 2
            $historic = $openweather->historicweather();
134 2
            $forecast = "";
135
        } else {
136 2
            $forecast = $openweather->forecast();
137 2
            $historic = "";
138
        }
139
        $data = [
140 2
            "weatherinfo" => $weatherinfo,
141 2
            "forecast" => $forecast,
142 2
            "historic" => $historic
143
        ];
144 2
        return $data;
145
    }
146
}
147