WeatherController   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Test Coverage

Coverage 89.13%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 69
dl 0
loc 118
c 1
b 0
f 0
ccs 41
cts 46
cp 0.8913
rs 10
wmc 7

6 Methods

Rating   Name   Duplication   Size   Complexity  
A indexAction() 0 9 1
A verifyAction() 0 28 2
A jsonActionGet() 0 14 1
A formAction() 0 12 1
A toc() 0 30 1
A initialize() 0 4 1
1
<?php
2
3
namespace Teca\Weather;
4
5
use Anax\DI\DIFactoryConfig;
6
use Anax\Commons\ContainerInjectableInterface;
7
use Anax\Commons\ContainerInjectableTrait;
8
9
/**
10
 * @SuppressWarnings(PHPMD.TooManyPublicMethods)
11
 */
12
class WeatherController implements ContainerInjectableInterface
13
{
14
    use ContainerInjectableTrait;
15
16
    protected $weather = null;
17
    protected $validator = null;
18
19
    public function initialize()
20
    {
21
        $this->weather = $this->di->get("weather");
22
        $this->validator = $this->di->get("ipValidator");
23
    }
24
25 5
    public function toc() : array
26
    {
27
        return [
28 5
            "toc" => [
29
                "weather" => [
30
                    "sectionHeader" => true,
31
                    "title" => "info",
32
                    "linkable" => true,
33
                    "level" => 1
34
                ],
35
                "weather/forms" => [
36
                    "sectionHeader" => true,
37
                    "title" => "Formulär",
38
                    "linkable" => false,
39
                    "level" => 1
40
                ],
41
                "weather/form" => [
42
                    "sectionHeader" => false,
43
                    "title" => "väder",
44
                    "linkable" => true,
45
                    "level" => 1
46
                ],
47
                "weather/json" => [
48
                    "sectionHeader" => false,
49
                    "title" => "väder API",
50
                    "linkable" => true,
51
                    "level" => 1
52
                ]
53
            ],
54
            "title" => "Väder"
55
        ];
56
    }
57
58 1
    public function indexAction() : object
59
    {
60 1
        $page = $this->di->get("page");
61
62 1
        $page->add("anax/v2/toc/default", $this->toc());
63 1
        $page->add("Weather/info", []);
64
65 1
        return $page->render([
66 1
            "title" => "Väder information",
67
        ]);
68
    }
69
70 1
    public function formAction() : object
71
    {
72 1
        $page = $this->di->get("page");
73
74 1
        $page->add("anax/v2/toc/default", $this->toc());
75 1
        $page->add("Weather/form", [
76 1
            "action" => "../weather/verify",
77
            "method" => "GET",
78
        ]);
79
80 1
        return $page->render([
81 1
            "title" => "Väder",
82
        ]);
83
    }
84
85 1
    public function jsonActionGet() : object
86
    {
87 1
        $page = $this->di->get("page");
88
89 1
        $page->add("anax/v2/toc/default", $this->toc());
90 1
        $page->add("Weather/form", [
91 1
            "title" => "Väder JSON",
92
            "text" => "Formuläret skickar en POST request som sedan visas i webbläsaren som rå JSON. Mer <a href=\"./\">info</a>.",
93
            "action" => "../weatherapi",
94
            "method" => "POST"
95
        ]);
96
97 1
        return $page->render([
98 1
            "title" => "(JSON) Väder",
99
        ]);
100
    }
101
102 1
    public function verifyAction() : object
103
    {
104 1
        $page = $this->di->get("page");
105 1
        $request = $this->di->get("request");
106 1
        $query = $request->getGet("query");
107
108 1
        $geo = $this->validator->getGeo($query);
109
110 1
        if ($geo["city"] !== null) {
111
            $query = $geo["city"];
112
        }
113
114 1
        $forecast = $this->weather->getForecast($query);
115
116 1
        $page->add("anax/v2/toc/default", $this->toc());
117 1
        $page->add("Weather/result", [
118 1
            "error" => $forecast["error"],
119 1
            "history" => $forecast["history"],
120 1
            "today" => $forecast["today"],
121 1
            "forecast" => $forecast["forecast"],
122 1
            "query" => $query,
123 1
            "lon" => $forecast["lon"],
124 1
            "lat" => $forecast["lat"],
125 1
            "geo" => $geo
126
        ]);
127
128 1
        return $page->render([
129 1
            "title" => "Resultat av Väder sökning",
130
        ]);
131
    }
132
}
133