PageController::indexActionGet()   C
last analyzed

Complexity

Conditions 12
Paths 36

Size

Total Lines 48
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 31
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 12
eloc 32
c 1
b 0
f 0
nc 36
nop 0
dl 0
loc 48
ccs 31
cts 31
cp 1
crap 12
rs 6.9666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace EVB\Weather;
4
5
use Anax\Commons\ContainerInjectableInterface;
6
use Anax\Commons\ContainerInjectableTrait;
7
8
/**
9
 * Controller for Weather page.
10
 *
11
 * @SuppressWarnings(PHPMD.TooManyPublicMethods)
12
 */
13
class PageController implements ContainerInjectableInterface
14
{
15
    use ContainerInjectableTrait;
16
17
    /**
18
     * Index page.
19
     *
20
     * GET
21
     *
22
     * @return mixed
23
     */
24 3
    public function indexActionGet()
25
    {
26 3
        $request = $this->di->get("request");
27 3
        $weather = $request->getGet("search") == "example" ? $this->di->get("exampleWeather") : $this->di->get("weather");
28
29 3
        $forecasts = [];
30 3
        $mapLink = "";
31 3
        $error = "";
32
33 3
        $lat = "";
34 3
        $long = "";
35 3
        $ip = $request->getGet("ip", "");
36
37 3
        if ($ip) {
38 1
            $ipLocator = $this->di->get("ipLocator");
39
40 1
            $ipLocation = $ipLocator->getGeoInfo($ip);
41
42 1
            if ($ipLocation && $ipLocation["latitude"] && $ipLocation["longitude"]) {
43 1
                $lat = $ipLocation["latitude"];
44 1
                $long = $ipLocation["longitude"];
45
            } else {
46 1
                $error = "Inga koordinater hittades för den IP-adressen.";
47
            }
48
        } else {
49 2
            $lat = $request->getGet("lat", "");
50 2
            $long = $request->getGet("long", "");
51
        }
52
53 3
        if (!$error && $lat && $long) {
54 2
            $forecasts = $weather->getWeather($lat, $long);
55 2
            if (\is_string($forecasts)) {
56 1
                $error = $forecasts;
57 1
                $forecasts = [];
58
            }
59
        }
60
61 3
        if (!$error && $forecasts) {
62 1
            $mapGenerator = $this->di->get("mapGenerator");
63
64 1
            $mapLink = $mapGenerator->MakeLink((float)$lat, (float)$long);
65
        }
66
67 3
        return $this->renderPage([
68 3
            "lat" => $lat,
69 3
            "long" => $long,
70 3
            "ip" => $ip
71 3
        ], $forecasts, $mapLink, $error);
72
    }
73
74
    /**
75
     * Helper method for rendering the page.
76
     *
77
     * @param string $ip
78
     * @param array $result
79
     * @return mixed
80
     */
81 3
    public function renderPage(array $search, array $result, string $mapLink, string $error)
82
    {
83 3
        $page = $this->di->get("page");
84
85 3
        $page->add("weather/searchForm", [
86 3
            "search" => $search
87
        ]);
88
89 3
        if ($error) {
90 1
            $page->add("weather/error", [
91 1
                "error" => $error
92
            ]);
93 2
        } else if (!empty($result)) {
94 1
            $page->add("weather/map", [
95 1
                "link" => $mapLink
96
            ]);
97 1
            $page->add("weather/result", [
98 1
                "forecast" => $result[0],
99 1
                "historical" => \array_slice($result, 1)
100
            ]);
101 1
            $page->add("weather/attribution");
102
        }
103
104 3
        return $page->render([
105 3
            "title" => "Väder"
106
        ]);
107
    }
108
}
109