Completed
Push — master ( 749d6b...11b362 )
by jesper
02:12
created

WeatherAPIController   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 28
c 1
b 0
f 0
dl 0
loc 60
ccs 0
cts 27
cp 0
rs 10
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getWeather() 0 19 4
A indexAction() 0 19 4
1
<?php
2
3
namespace Anax\Weather;
4
5
use Anax\Commons\ContainerInjectableInterface;
6
use Anax\Commons\ContainerInjectableTrait;
7
8
// use Anax\Route\Exception\ForbiddenException;
9
// use Anax\Route\Exception\NotFoundException;
10
// use Anax\Route\Exception\InternalErrorException;
11
12
/**
13
 *
14
 * @SuppressWarnings(PHPMD.TooManyPublicMethods)
15
 */
16
class WeatherAPIController implements ContainerInjectableInterface
17
{
18
    use ContainerInjectableTrait;
19
20
    /**
21
     * This is the index method action, it handles:
22
     * ANY METHOD mountpoint
23
     * ANY METHOD mountpoint/
24
     * ANY METHOD mountpoint/index
25
     *
26
     * @return string
27
     */
28
    public function indexAction()
29
    {
30
        $title = "Weather info";
31
        $page = $this->di->get("page");
32
        $request = $this->di->get("request");
0 ignored issues
show
Unused Code introduced by
The assignment to $request is dead and can be removed.
Loading history...
33
34
        $request = $this->di->get("request");
35
36
        if ($request->hasGet("location")) {
37
            if ($request->hasGet("prev", true) && $request->hasGet("days")) {
38
                return $this->getWeather($request->getGet("location"), $request->getGet("days"));
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getWeather...equest->getGet('days')) returns the type array<integer,array> which is incompatible with the documented return type string.
Loading history...
39
            }
40
            return $this->getWeather($request->getGet("location"));
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getWeather...st->getGet('location')) returns the type array<integer,array> which is incompatible with the documented return type string.
Loading history...
41
        }
42
        $page->add("weather/form-json-weather", []);
43
        $page->add("weather/api-exemple", []);
44
45
        return $page->render([
46
            "title" => $title,
47
        ]);
48
    }
49
50
    /**
51
     * Get weather
52
     * Today, and next 7 days or
53
     * Previous days
54
     *
55
     * @return array
56
     */
57
    public function getWeather($location, $days = null)
58
    {
59
        $CurlModel = $this->di->get("curl");
60
        $WeatherModel = $this->di->get("weather");
61
        $WeatherModel->setCurl($CurlModel);
62
        
63
        $weatherInfo = [];
64
        $convertedLocation = $WeatherModel->convertLocation($location);
65
        ini_set('serialize_precision', -1);
66
        
67
        if ($convertedLocation["match"] && $days !== null) {
68
            $weatherInfo = $WeatherModel->getWeatherMulti($convertedLocation["latitude"], $convertedLocation["longitude"], $days);
69
        } else if ($convertedLocation["match"]) {
70
            $weatherInfo = $WeatherModel->getWeather($convertedLocation["latitude"], $convertedLocation["longitude"], $days);
71
        }
72
        return [[
73
            "match" => $convertedLocation["match"],
74
            "location" => $convertedLocation,
75
            "weatherinfo" => $weatherInfo
76
        ]];
77
    }
78
}
79