Issues (11)

src/Weather/WeatherController.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace Bjos\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
 * Controller to get weather information.
14
 */
15
class WeatherController implements ContainerInjectableInterface
16
{
17
    use ContainerInjectableTrait;
18
19
    private $validate;
20
    protected $geo;
21
    protected $ipStack;
22
    protected $option;
23
    protected $weather;
24
25
    /**
26
     * The initialize method is optional and will always be called before the
27
     * target method/action. This is a convienient method where you could
28
     * setup internal properties that are commonly used by several methods.
29
     *
30
     * @return void
31
     */
32 6
    public function initialize() : void
33
    {
34
        // Use to initialise member variables.
35 6
        $this->ipStack = "http://api.ipstack.com/";
36 6
        $this->option = "?access_key=";
37 6
        $this->geo = $this->di->get("geolocation");
38 6
        $this->validate = $this->di->get("validate");
39 6
        $this->weather = $this->di->get("weather");
40 6
    }
41
42
    /**
43
     * Get wheter info by ip-address or coordinates.
44
     *
45
     * @return object
46
     */
47 6
    public function indexActionGet() : object
48
    {
49 6
        $title = "Weather";
50 6
        $page = $this->di->get("page");
51 6
        $lat = null;
52 6
        $long = null;
53 6
        $location= null;
54 6
        $type = null;
55 6
        $userIp = $this->geo->getUserIp();
56
57 6
        $ipAdr = $this->di->request->getGet("search") ? $this->di->request->getGet("search") : $userIp;
0 ignored issues
show
Accessing request on the interface Psr\Container\ContainerInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
58 6
        $forecast = $this->di->request->getGet("weather") ? $this->di->request->getGet("weather") : "forecast";
59 6
        $valid = $this->validate->validate($ipAdr);
60
61 6
        if ($valid) {
62 2
            $location = $this->geo->getLocation($ipAdr, $this->ipStack, $this->option);
63 2
            $lat = $location["latitude"];
64 2
            $long = $location["longitude"];
65
        } else {
66 4
            $location = $this->weather->getLatLong($ipAdr);
67 4
            $lat = $location["latitude"];
68 4
            $long = $location["longitude"];
69
        }
70
71 6
        if ($forecast === "hist") {
72 2
            $forecast = $this->weather->getHistory($lat, $long);
73 2
            if ($forecast) {
74 2
                $type = "history";
75
            }
76 4
        } elseif ($forecast === "forecast") {
77 4
            $forecast = $this->weather->getWeather($lat, $long);
78 4
            if ($forecast) {
79 4
                $type = "forecast";
80
            }
81
        }
82
83
        $data = [
84 6
            "userIp" => $userIp,
85 6
            "valid" => $valid
86
        ];
87
88 6
        $page->add("bjos/weather/index", $data);
89
90 6
        $page->add("bjos/geolocation/geomap", [
91 6
            "location" => $location
92
        ]);
93 6
        $page->add("bjos/weather/table", [
94 6
            "location" => $location,
95 6
            "type" => $type,
96 6
            "forecast" => $forecast,
97 6
            "valid" => $valid,
98
        ]);
99
100 6
        return $page->render([
101 6
            "title" => $title
102
        ]);
103
    }
104
}
105