Passed
Push — master ( 6de464...cdd6df )
by Oscar
03:02
created

WeatherJsonController::responseActionGet()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 29
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 21
c 1
b 0
f 0
nc 5
nop 0
dl 0
loc 29
ccs 20
cts 20
cp 1
crap 4
rs 9.584
1
<?php
2
3
namespace Osln\Weather;
4
5
use Anax\Commons\ContainerInjectableInterface;
6
use Anax\Commons\ContainerInjectableTrait;
7
8
/**
9
 * @SuppressWarnings(PHPMD.TooManyPublicMethods)
10
 */
11
class WeatherJsonController implements ContainerInjectableInterface
12
{
13
    use ContainerInjectableTrait;
14 3
    public function initialize() : void
15
    {
16 3
        $this->config = new LoadConfig();
0 ignored issues
show
Bug Best Practice introduced by
The property config does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
17 3
        $this->ipvalidate = new Ipvalidate();
0 ignored issues
show
Bug Best Practice introduced by
The property ipvalidate does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
18 3
    }
19
    /**
20
     * This is the index method action, it handles:
21
     * ANY METHOD mountpoint
22
     * ANY METHOD mountpoint/
23
     * ANY METHOD mountpoint/index
24
     *
25
     * @return string
26
     */
27 1
    public function indexAction() : object
28
    {
29 1
        $page = $this->di->get("page");
30 1
        $session = $this->di->get("session");
31
32
        $data = [
33 1
            "title" => "Väder",
34 1
            "forecast" => $session->has("forecast") ? $session->get("forecast") : "",
35 1
            "month" => $session->has("month") ? $session->get("month") : "",
36 1
            "error" => $session->has("error") ? $session->get("error") : "",
37 1
            "location" => "",
38
        ];
39 1
        $page->add("osln/weather/defaultJson", $data);
40 1
        $session->delete("error");
41 1
        return $page->render();
42
    }
43
44 1
    public function responseActionGet()
45
    {
46 1
        $search = $this->di->request->getGet("location");
0 ignored issues
show
Bug introduced by
Accessing request on the interface Psr\Container\ContainerInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
47 1
        $session = $this->di->get("session");
48 1
        $session->delete("month");
49 1
        $url = $this->ipvalidate->getUrl($search);
50
51 1
        $data = $this->di->curl->fetch($url);
0 ignored issues
show
Bug introduced by
Accessing curl on the interface Psr\Container\ContainerInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
52
        // $forecast = null;
53
54 1
        if (isset($data["latitude"])) {
55 1
            $forecast = $this->di->weather->forecast($data["latitude"], $data["longitude"]);
0 ignored issues
show
Bug introduced by
Accessing weather on the interface Psr\Container\ContainerInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
56 1
        } elseif (isset($data[0]["lat"])) {
57 1
            $forecast = $this->di->weather->forecast($data[0]["lat"], $data[0]["lon"]);
58
        } else {
59 1
            return [["Could not find this location"]];
60
        }
61 1
        $data = [];
62 1
        $i = 1;
63
        // var_dump($forecast);
64 1
        foreach ($forecast[0]["daily"]["data"] as $day) {
65
            $newDay = [
66 1
                "day" => $i++,
67 1
                "summary" => $day["summary"],
68 1
                "temp" => $day["temperatureHigh"],
69 1
                "wind" => $day["windSpeed"]];
70 1
            $data[] = $newDay;
71
        }
72 1
        return [$data];
73
    }
74 1
    public function previousActionGet()
75
    {
76 1
        $search = $this->di->request->getGet("location");
0 ignored issues
show
Bug introduced by
Accessing request on the interface Psr\Container\ContainerInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
77 1
        $url = $this->ipvalidate->getUrl($search);
78
79 1
        $data = $this->di->curl->fetch($url);
0 ignored issues
show
Bug introduced by
Accessing curl on the interface Psr\Container\ContainerInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
80 1
        $forecast = null;
81 1
        if (isset($data["latitude"])) {
82 1
            $forecast = $this->di->weather->forecast($data["latitude"], $data["longitude"]);
0 ignored issues
show
Bug introduced by
Accessing weather on the interface Psr\Container\ContainerInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
Unused Code introduced by
The assignment to $forecast is dead and can be removed.
Loading history...
83 1
            $month = $this->di->weather->getLastMonth($data["latitude"], $data["longitude"]);
84 1
        } elseif (isset($data[0]["lat"])) {
85 1
            $forecast = $this->di->weather->forecast($data[0]["lat"], $data[0]["lon"]);
86 1
            $month = $this->di->weather->getLastMonth($data[0]["lat"], $data[0]["lon"]);
87
        }
88 1
        $data = [];
89 1
        $i = -1;
90 1
        foreach ($month[0] as $day) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $month does not seem to be defined for all execution paths leading up to this point.
Loading history...
91
            $newDay = [
92 1
                "day" => $i++,
93 1
                "summary" => $day["daily"]["data"][0]["summary"],
94 1
                "temp" => $day["daily"]["data"][0]["temperatureHigh"],
95 1
                "wind" => $day["daily"]["data"][0]["windSpeed"]
96
            ];
97 1
            $data[] = $newDay;
98
        }
99 1
        return [$data];
100
    }
101
}
102