Issues (13)

src/Weather/WeatherController.php (2 issues)

Severity
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
        // Prepare page
61 4
        $page = $this->di->get("page");
62 4
        $page->add("weather/index");
63 4
        $title = "Weather | ramverk1";
64
65
        // Validate IP
66 4
        $ipAddress = new WeatherIpValidation($iptocheck);
67 4
        $data = $ipAddress->answer();
68
69 4
        if (empty($iptocheck)) {
70 1
            return $page->render([
71 1
                "title" => $title,
72
            ]);
73 3
        } elseif (!$ipAddress->isValid()) {
74 1
            $page->add("weather/notvalidIP", $data);
75 1
            return $page->render([
76 1
                "title" => $title,
77
            ]);
78
        }
79
        // Get IP location
80 2
        $geoLocation = new WeatherGeoLocation($iptocheck);
0 ignored issues
show
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

80
        $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...
81 2
        $geoLocation->setDI($this->di);
82 2
        $geoLocation->setAPI("ipstack");
83 2
        $geoLocation->checkGeoLocation($iptocheck);
84 2
        $locationInfo = $geoLocation->getGeoLocation();
85
86
87
        // Get Weather information Forecast
88 2
        $weatherRequest = new WeatherRequest();
89 2
        $weatherRequest->setDI($this->di);
90 2
        $weatherRequest->setAPI("openweathermap");
91 2
        $weatherRequest->checkWeather($geoLocation);
92 2
        $weatherInfo = (array)$weatherRequest->getWeather();
93
        
94
        // Get Weather information Historical Data
95
        
96 2
        $weatherInfoHist = array("weatherInfoHistorical" => $weatherRequest->checkWeatherMulti($geoLocation));
97 2
        $weatherInfo = array_merge($weatherInfo, $weatherInfoHist);
98
        
99
        // Merge location data with ip data
100 2
        $data = array_merge($data, (array)$locationInfo);
101
102
103 2
        if ($ipAddress->isValid() && $geoLocation->geoLocationOK()) {
104
            $page->add("weather/validIP", $data);
105
106
            $weatherInfo = array_merge($weatherInfo, (array)$weatherInfoHist);
107
            $page->add("weather/weather", $weatherInfo);
108
        } else {
109 2
            $page->add("weather/notvalidWeather", $data);
110
        }
111
112 2
        return $page->render([
113 2
            "title" => $title,
114
        ]);
115
    }
116
117
    /**
118
    * This is the index method action, it handles:
119
    * ANY METHOD mountpoint
120
    * ANY METHOD mountpoint/
121
    * ANY METHOD mountpoint/index
122
    *
123
    * @return object
124
    */
125 4
    public function indexActionPost(): object
126
    {
127 4
        $request = $this->di->get("request");
128 4
        $iptocheck = $request->getPost("ip") ?? "";
129
130
        // Prepare page
131 4
        $page = $this->di->get("page");
132 4
        $page->add("weather/index");
133 4
        $title = "Weather | ramverk1";
134
135
        // Validate IP
136 4
        $ipAddress = new WeatherIpValidation($iptocheck);
137 4
        $data = $ipAddress->answer();
138
139 4
        if (empty($iptocheck)) {
140 1
            return $page->render([
141 1
                "title" => $title,
142
            ]);
143 3
        } elseif (!$ipAddress->isValid()) {
144 1
            $page->add("weather/notvalidIP", $data);
145 1
            return $page->render([
146 1
                "title" => $title,
147
            ]);
148
        }
149
        // Get IP location
150 2
        $geoLocation = new WeatherGeoLocation($iptocheck);
0 ignored issues
show
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

150
        $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...
151 2
        $geoLocation->setDI($this->di);
152 2
        $geoLocation->setAPI("ipstack");
153 2
        $geoLocation->checkGeoLocation($iptocheck);
154 2
        $locationInfo = $geoLocation->getGeoLocation();
155
156
157
        // Get Weather information Forecast
158 2
        $weatherRequest = new WeatherRequest();
159 2
        $weatherRequest->setDI($this->di);
160 2
        $weatherRequest->setAPI("openweathermap");
161 2
        $weatherRequest->checkWeather($geoLocation);
162 2
        $weatherInfo = (array)$weatherRequest->getWeather();
163
        
164
        // Get Weather information Historical Data
165
        
166 2
        $weatherInfoHist = array("weatherInfoHistorical" => $weatherRequest->checkWeatherMulti($geoLocation));
167 2
        $weatherInfo = array_merge($weatherInfo, $weatherInfoHist);
168
        
169
        // Merge location data with ip data
170 2
        $data = array_merge($data, (array)$locationInfo);
171
172
173 2
        if ($ipAddress->isValid() && $geoLocation->geoLocationOK()) {
174
            $page->add("weather/validIP", $data);
175
176
            $weatherInfo = array_merge($weatherInfo, (array)$weatherInfoHist);
177
            $page->add("weather/weather", $weatherInfo);
178
        } else {
179 2
            $page->add("weather/notvalidWeather", $data);
180
        }
181
182 2
        return $page->render([
183 2
            "title" => $title,
184
        ]);
185
    }
186
}
187