WeatherAPIController   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 26
c 2
b 0
f 0
dl 0
loc 43
ccs 20
cts 20
cp 1
rs 10
wmc 8

1 Method

Rating   Name   Duplication   Size   Complexity  
B indexAction() 0 34 8
1
<?php
2
3
namespace Linder\Controller;
4
5
use Anax\Commons\ContainerInjectableInterface;
6
use Anax\Commons\ContainerInjectableTrait;
7
use Linder\Model\Coordinates;
8
use Linder\Model\DarkSky;
9
10
/**
11
 * A controller that handles a get request
12
 * and returns if its a valid ip or not.
13
 */
14
class WeatherAPIController implements ContainerInjectableInterface
15
{
16
    use ContainerInjectableTrait;
17
18
    /**
19
     * This is the index method action, it handles:
20
     *
21
     * @return Array
22
     */
23 1
    public function indexAction() : array
24
    {
25 1
        $search = $this->di->get("request")->getGet("search");
26 1
        $latlon = "";
27
28 1
        if (filter_var($search, FILTER_VALIDATE_IP)) {
29 1
            $res = $this->di->get("ipverifier")->getJson($search);
30 1
            $latlon = ((!$res["latitude"]) || (!$res["longitude"])) ? "" : $res["latitude"] . "," . $res["longitude"];
31 1
        } else if ($search) {
32 1
            $geocoder = $this->di->get("geocoder");
33 1
            $coords = new Coordinates($geocoder);
34 1
            $latlon = $coords->getCoordinates($search);
35
        }
36 1
        if (($search) && ($latlon != "")) {
37 1
            $darksky = $this->di->get("darksky");
38 1
            $type = $this->di->get("request")->getGet("type");
39 1
            if ($type == "past") {
40 1
                $res = $darksky->getWeatherPast($latlon);
41
            } else {
42 1
                $res = $darksky->getWeatherComing($latlon);
43
            }
44
    
45
            $data = [
46 1
                "latlon" => $latlon,
47 1
                "res" => $res
48
            ];
49
        } else {
50
            $data = [
51 1
                "code" => 400,
52
                "error" => "You didnt search for anything"
53
            ];
54
        }
55
56 1
        return [$data];
57
    }
58
}
59