WeatherJsonController::indexAction()   B
last analyzed

Complexity

Conditions 9
Paths 25

Size

Total Lines 59
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 41
CRAP Score 9

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 44
c 1
b 0
f 0
nc 25
nop 0
dl 0
loc 59
ccs 41
cts 41
cp 1
crap 9
rs 7.6604

How to fix   Long Method   

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 Anax\Controller;
4
5
use Anax\Commons\ContainerInjectableInterface;
6
use Anax\Commons\ContainerInjectableTrait;
7
use Anax\Weather\Weather;
8
use Anax\IpGeo\IpGeo;
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 WeatherJsonController implements ContainerInjectableInterface
22
{
23
    use ContainerInjectableTrait;
24
    protected $weather;
25
    protected $ipGeo;
26
    protected $request;
27
28
29
    /**
30
     * @var string $db a sample member variable that gets initialised
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 7
    public function initialize(): void
40
    {
41
        // Use to initialise member variables.
42 7
        $this->weather = new Weather($this->di);
43 7
        $this->ipGeo = new IpGeo();
44 7
        $this->request = $this->di->get("request");
45 7
    }
46
47
48
49
    /**
50
     * This is the index method action, it handles:
51
     * ANY METHOD mountpoint
52
     * ANY METHOD mountpoint/
53
     * ANY METHOD mountpoint/index
54
     *
55
     * @return array
56
     */
57 7
    public function indexAction(): array
58
    {
59 7
        $title = "Väderprognos";
60 7
        $ipAddress = $this->request->getGet("ip");
61 7
        $city = $this->request->getGet("city");
62 7
        $searchType = $this->request->getGet("search_type");
63
        // $lat = $this->request->getGet("lat");
64
        // $long = $this->request->getGet("long");
65
66 7
        if ($city) {
67 2
            $res = $this->weather->getCoords($city);
68 2
            $lat = $res["lat"];
69 2
            $long = $res["long"];
70 5
        } elseif ($ipAddress) {
71 2
            $ipInfo = $this->ipGeo->getLocation($ipAddress);
72 2
            $lat = $ipInfo["lat"];
73 2
            $long = $ipInfo["long"];
74
        } else {
75 3
            $req = $this->di->get("request");
76 3
            if (!empty($req->getServer("HTTP_CLIENT_IP"))) {
77 1
                $ipAddress = $req->getServer("HTTP_CLIENT_IP");
78 2
            } elseif (!empty($req->getServer("HTTP_X_FORWARDED_FOR"))) {
79 1
                $ipAddress = $req->getServer("HTTP_X_FORWARDED_FOR");
80
            } else {
81 1
                $ipAddress = $req->getServer("REMOTE_ADDR");
82
            }
83 3
            $ipInfo = $this->ipGeo->getLocation($ipAddress);
84 3
            $lat = $ipInfo["lat"];
85 3
            $long = $ipInfo["long"];
86
        }
87 7
        if ($lat == "Missing") {
88
            $json = [
89 4
                "err" => "Oops, platsinformation saknas",
90 4
                "title" => $title,
91 4
                "ip" => $ipAddress,
92 4
                "weather" => "",
93
            ];
94
95 4
            return [$json];
96
        }
97
98 3
        $weatherInfo = $this->weather->getWeather($lat, $long, $searchType);
99
100
        $json = [
101 3
            "weather" => $weatherInfo,
102 3
            "ip" => $ipAddress,
103 3
            "title" => $title,
104
        ];
105 3
        if ($searchType == "history") {
106 1
            $json["weather"] = $weatherInfo["history"];
107 1
            $json["city"] = $weatherInfo["city"];
108 2
        } elseif ($searchType == "forecast") {
109 1
            $json["weather"] = $weatherInfo["daily"]["data"];
110 1
            $json["city"] = $weatherInfo["city"];
111 1
        } elseif ($searchType == "currently") {
112 1
            $json["weather"] = $weatherInfo;
113
        }
114
115 3
        return [$json];
116
    }
117
}
118