Passed
Push — main ( b79556...5cf45d )
by Johan
02:30
created

WeatherController   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 171
Duplicated Lines 0 %

Test Coverage

Coverage 92.21%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 78
c 1
b 0
f 0
dl 0
loc 171
ccs 71
cts 77
cp 0.9221
rs 10
wmc 11

3 Methods

Rating   Name   Duplication   Size   Complexity  
A initialize() 0 4 1
A indexActionPost() 0 63 5
A indexActionGet() 0 63 5
1
<?php
2
3
namespace Lefty\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
 * A sample controller to show how a controller class can be implemented.
14
 * The controller will be injected with $di if implementing the interface
15
 * ContainerInjectableInterface, like this sample class does.
16
 * The controller is mounted on a particular route and can then handle all
17
 * requests for that mount point.
18
 *
19
 * @SuppressWarnings(PHPMD.TooManyPublicMethods)
20
 */
21
class WeatherController implements ContainerInjectableInterface
22
{
23
    use ContainerInjectableTrait;
24
25
26
    /**
27
     * @var string $db a sample member variable that gets initialised
28
     */
29
    private $db = "not active";
30
31
32
33
    /**
34
     * The initialize method is optional and will always be called before the
35
     * target method/action. This is a convienient method where you could
36
     * setup internal properties that are commonly used by several methods.
37
     *
38
     * @return void
39
     */
40 8
    public function initialize(): void
41
    {
42
        // Use to initialise member variables.
43 8
        $this->db = "active";
44 8
    }
45
46
47
    /**
48
    * This is the index method action, it handles:
49
    * ANY METHOD mountpoint
50
    * ANY METHOD mountpoint/
51
    * ANY METHOD mountpoint/index
52
    *
53
    * @return object
54
    */
55 4
    public function indexActionGet(): object
56
    {
57 4
        $request     = $this->di->get("request");
58 4
        $iptocheck = $request->getGet("ip") ?? "";
59
60 4
        $weatherInfo = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $weatherInfo is dead and can be removed.
Loading history...
61 4
        $weatherInfoHist = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $weatherInfoHist is dead and can be removed.
Loading history...
62
63
64
        // Prepare page
65 4
        $page = $this->di->get("page");
66 4
        $page->add("weather/index");
67 4
        $title = "Weather | ramverk1";
68
69
        // Validate IP
70 4
        $ipAddress = new WeatherIpValidation($iptocheck);
71 4
        $data = $ipAddress->answer();
72
73 4
        if (empty($iptocheck)) {
74 1
            return $page->render([
75 1
                "title" => $title,
76
            ]);
77 3
        } elseif (!$ipAddress->isValid()) {
78 1
            $page->add("weather/notvalidIP", $data);
79 1
            return $page->render([
80 1
                "title" => $title,
81
            ]);
82
        }
83
        // Get IP location
84 2
        $geoLocation = new WeatherGeoLocation($iptocheck);
0 ignored issues
show
Unused Code introduced by
The call to Lefty\Weather\WeatherGeoLocation::__construct() has too many arguments starting with $iptocheck. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

84
        $geoLocation = /** @scrutinizer ignore-call */ new WeatherGeoLocation($iptocheck);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
85 2
        $geoLocation->setDI($this->di);
86 2
        $geoLocation->setAPI("ipstack");
87 2
        $geoLocation->checkGeoLocation($iptocheck);
88 2
        $locationInfo = $geoLocation->getGeoLocation();
89
90
91
        // Get Weather information Forecast
92 2
        $weatherRequest = new WeatherRequest();
93 2
        $weatherRequest->setDI($this->di);
94 2
        $weatherRequest->setAPI("openweathermap");
95 2
        $weatherRequest->checkWeather($geoLocation);
96 2
        $weatherInfo = (array)$weatherRequest->getWeather();
97
        
98
        // Get Weather information Historical Data
99
        
100 2
        $weatherInfoHist = array("weatherInfoHistorical" => $weatherRequest->checkWeatherMulti($geoLocation));
101 2
        $weatherInfo = array_merge($weatherInfo, $weatherInfoHist);
102
        
103
        // Merge location data with ip data
104 2
        $data = array_merge($data, (array)$locationInfo);
105
106
107 2
        if ($ipAddress->isValid() && $geoLocation->geoLocationOK()) {
108
            $page->add("weather/validIP", $data);
109
110
            $weatherInfo = array_merge($weatherInfo, (array)$weatherInfoHist);
111
            $page->add("weather/weather", $weatherInfo);
112
        } else {
113 2
            $page->add("weather/notvalidWeather", $data);
114
        }
115
116 2
        return $page->render([
117 2
            "title" => $title,
118
        ]);
119
    }
120
121
    /**
122
    * This is the index method action, it handles:
123
    * ANY METHOD mountpoint
124
    * ANY METHOD mountpoint/
125
    * ANY METHOD mountpoint/index
126
    *
127
    * @return object
128
    */
129 4
    public function indexActionPost(): object
130
    {
131 4
        $request     = $this->di->get("request");
132 4
        $iptocheck = $request->getPost("ip") ?? "";
133
134 4
        $weatherInfo = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $weatherInfo is dead and can be removed.
Loading history...
135 4
        $weatherInfoHist = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $weatherInfoHist is dead and can be removed.
Loading history...
136
137
138
        // Prepare page
139 4
        $page = $this->di->get("page");
140 4
        $page->add("weather/index");
141 4
        $title = "Weather | ramverk1";
142
143
        // Validate IP
144 4
        $ipAddress = new WeatherIpValidation($iptocheck);
145 4
        $data = $ipAddress->answer();
146
147 4
        if (empty($iptocheck)) {
148 1
            return $page->render([
149 1
                "title" => $title,
150
            ]);
151 3
        } elseif (!$ipAddress->isValid()) {
152 1
            $page->add("weather/notvalidIP", $data);
153 1
            return $page->render([
154 1
                "title" => $title,
155
            ]);
156
        }
157
        // Get IP location
158 2
        $geoLocation = new WeatherGeoLocation($iptocheck);
0 ignored issues
show
Unused Code introduced by
The call to Lefty\Weather\WeatherGeoLocation::__construct() has too many arguments starting with $iptocheck. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

158
        $geoLocation = /** @scrutinizer ignore-call */ new WeatherGeoLocation($iptocheck);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
159 2
        $geoLocation->setDI($this->di);
160 2
        $geoLocation->setAPI("ipstack");
161 2
        $geoLocation->checkGeoLocation($iptocheck);
162 2
        $locationInfo = $geoLocation->getGeoLocation();
163
164
165
        // Get Weather information Forecast
166 2
        $weatherRequest = new WeatherRequest();
167 2
        $weatherRequest->setDI($this->di);
168 2
        $weatherRequest->setAPI("openweathermap");
169 2
        $weatherRequest->checkWeather($geoLocation);
170 2
        $weatherInfo = (array)$weatherRequest->getWeather();
171
        
172
        // Get Weather information Historical Data
173
        
174 2
        $weatherInfoHist = array("weatherInfoHistorical" => $weatherRequest->checkWeatherMulti($geoLocation));
175 2
        $weatherInfo = array_merge($weatherInfo, $weatherInfoHist);
176
        
177
        // Merge location data with ip data
178 2
        $data = array_merge($data, (array)$locationInfo);
179
180
181 2
        if ($ipAddress->isValid() && $geoLocation->geoLocationOK()) {
182
            $page->add("weather/validIP", $data);
183
184
            $weatherInfo = array_merge($weatherInfo, (array)$weatherInfoHist);
185
            $page->add("weather/weather", $weatherInfo);
186
        } else {
187 2
            $page->add("weather/notvalidWeather", $data);
188
        }
189
190 2
        return $page->render([
191 2
            "title" => $title,
192
        ]);
193
    }
194
}
195