Issues (21)

src/Weather/WeatherController.php (3 issues)

1
<?php
2
3
namespace Anax\Weather;
4
5
use Anax\Commons\ContainerInjectableInterface;
6
use Anax\Commons\ContainerInjectableTrait;
7
use Anax\Models\WeatherForecast;
8
use Anax\Models\GetGeo;
9
10
class WeatherController implements ContainerInjectableInterface
11
{
12
    use ContainerInjectableTrait;
13
14 2
    public function indexAction()
15
    {
16 2
        $page = $this->di->get("page");
17 2
        $title = "Väderprognos";
18
19 2
        $page->add("weather/index");
20 2
        return $page->render([
21 2
            "title" => $title,
22
        ]);
23
    }
24
25
26
    // public function getGeoCords($location)
27
    // {
28
    //     $geo = new GetGeo();
29
    //
30
    //     $res = $geo->getGeo($location);
31
    //
32
    //     return $res;
33
    // }
34
35
    public function checkWeatherAction()
36
    {
37
        $page = $this->di->get("page");
38
        $title = "Resultat";
39
        $location = $_GET["location"];
40
41
42
        $type = $this->di->get("request")->getGet("type");
43
44
        $weather = $this->di->get("weather");
45
46
        if ($type == 'prognos') {
47
            if (strpos($location, ",") == true) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing strpos($location, ',') of type integer to the boolean true. If you are specifically checking for non-zero, consider using something more explicit like > 0 or !== 0 instead.
Loading history...
48
                $exploded = explode(",", $location);
49
                $answer = $weather->checkWeather($exploded[0], $exploded[1]);
50
            } else {
51
                $geo = new GetGeo();
52
                $res = $geo->getGeo($location);
53
                if ($res["longitude"] !== null) {
54
                    $answer = $weather->checkWeather($res["latitude"], $res["longitude"]);
55
                } else {
56
                    $error = "Felaktig input, prova igen!";
57
                }
58
            }
59
        } elseif ($type == 'history') {
60
            if (strpos($location, ",") == true) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing strpos($location, ',') of type integer to the boolean true. If you are specifically checking for non-zero, consider using something more explicit like > 0 or !== 0 instead.
Loading history...
61
                $exploded = explode(",", $location);
62
                $answer = $weather->checkHistory($exploded[0], $exploded[1]);
63
            } else {
64
                $geo = new GetGeo();
65
                $res = $geo->getGeo($location);
66
                if ($res["longitude"] !== null) {
67
                    $answer = $weather->checkHistory($res["latitude"], $res["longitude"]);
68
                } else {
69
                    $error = "Felaktig input, prova igen!";
70
                }
71
            }
72
        }
73
74
75
76
        $data = [
77
            "forecast" => $answer ?? null,
78
            "error" => $error ?? null,
79
            "lat" => $exploded[0] ?? $res["latitude"],
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $res does not seem to be defined for all execution paths leading up to this point.
Loading history...
80
            "lon" => $exploded[1] ?? $res["longitude"]
81
        ];
82
83
84
        $page->add("weather/resultpage", $data);
85
86
        return $page->render([
87
            "title" => $title,
88
        ]);
89
    }
90
}
91