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 WeatherControllerAPI 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
|
2 |
|
public function initialize(): void |
41
|
|
|
{ |
42
|
|
|
// Use to initialise member variables. |
43
|
2 |
|
$this->db = "active"; |
44
|
2 |
|
} |
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
|
1 |
|
public function indexActionGet(): array |
56
|
|
|
{ |
57
|
1 |
|
$request = $this->di->get("request"); |
58
|
1 |
|
$iptocheck = $request->getGet("ip") ?? ""; |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
// Validate IP |
62
|
1 |
|
$ipAddress = new WeatherIpValidation($iptocheck); |
63
|
1 |
|
$data = $ipAddress->answer(); |
64
|
|
|
|
65
|
|
|
// Get IP location |
66
|
1 |
|
$geoLocation = new WeatherGeoLocation($iptocheck); |
|
|
|
|
67
|
1 |
|
$geoLocation->setDI($this->di); |
68
|
1 |
|
$geoLocation->setAPI("ipstack"); |
69
|
1 |
|
$geoLocation->checkGeoLocation($iptocheck); |
70
|
1 |
|
$locationInfo = $geoLocation->getGeoLocation(); |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
// Get Weather information |
74
|
1 |
|
$weatherRequest = new WeatherRequest(); |
75
|
1 |
|
$weatherRequest->setDI($this->di); |
76
|
1 |
|
$weatherRequest->setAPI("openweathermap"); |
77
|
1 |
|
$weatherRequest->checkWeather($geoLocation); |
78
|
1 |
|
$weatherInfo = (array)$weatherRequest->getWeather(); |
79
|
|
|
|
80
|
|
|
// Get Weather information Historical Data |
81
|
1 |
|
$weatherInfoHist = array("weatherInfoHistorical" => $weatherRequest->checkWeatherMulti($geoLocation)); |
82
|
1 |
|
$weatherInfo = array_merge($weatherInfo, $weatherInfoHist); |
83
|
|
|
|
84
|
|
|
|
85
|
|
|
|
86
|
|
|
// Merge location data with ip data |
87
|
1 |
|
$data = array_merge($data, (array)$locationInfo); |
88
|
1 |
|
$weatherArray = array("weatherInfo" => (array)$weatherInfo); |
89
|
1 |
|
$data = array_merge($data, $weatherArray); |
90
|
|
|
// $data = array_merge($data, (array)$weatherInfo); |
91
|
|
|
|
92
|
1 |
|
return [$data]; |
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* This is the index method action, it handles: |
97
|
|
|
* ANY METHOD mountpoint |
98
|
|
|
* ANY METHOD mountpoint/ |
99
|
|
|
* ANY METHOD mountpoint/index |
100
|
|
|
* |
101
|
|
|
* @return object |
102
|
|
|
*/ |
103
|
1 |
|
public function indexActionPost(): array |
104
|
|
|
{ |
105
|
1 |
|
$request = $this->di->get("request"); |
106
|
1 |
|
$iptocheck = $request->getPost("ip") ?? ""; |
107
|
|
|
|
108
|
|
|
|
109
|
|
|
// Validate IP |
110
|
1 |
|
$ipAddress = new WeatherIpValidation($iptocheck); |
111
|
1 |
|
$data = $ipAddress->answer(); |
112
|
|
|
|
113
|
|
|
// Get IP location |
114
|
1 |
|
$geoLocation = new WeatherGeoLocation($iptocheck); |
|
|
|
|
115
|
1 |
|
$geoLocation->setDI($this->di); |
116
|
1 |
|
$geoLocation->setAPI("ipstack"); |
117
|
1 |
|
$geoLocation->checkGeoLocation($iptocheck); |
118
|
1 |
|
$locationInfo = $geoLocation->getGeoLocation(); |
119
|
|
|
|
120
|
|
|
|
121
|
|
|
// Get Weather information |
122
|
|
|
|
123
|
1 |
|
$weatherRequest = new WeatherRequest("openweathermap"); |
|
|
|
|
124
|
1 |
|
$weatherRequest->setDI($this->di); |
125
|
1 |
|
$weatherRequest->setAPI("openweathermap"); |
126
|
1 |
|
$weatherRequest->checkWeather($geoLocation); |
127
|
1 |
|
$weatherInfo = (array)$weatherRequest->getWeather(); |
128
|
|
|
|
129
|
|
|
// Get Weather information Historical Data |
130
|
1 |
|
$weatherInfoHist = array("weatherInfoHistorical" => $weatherRequest->checkWeatherMulti($geoLocation)); |
131
|
1 |
|
$weatherInfo = array_merge($weatherInfo, $weatherInfoHist); |
132
|
|
|
|
133
|
|
|
|
134
|
|
|
// Merge location data with ip data |
135
|
1 |
|
$data = array_merge($data, (array)$locationInfo); |
136
|
1 |
|
$weatherArray = array("weatherInfo" => (array)$weatherInfo); |
137
|
1 |
|
$data = array_merge($data, $weatherArray); |
138
|
|
|
|
139
|
1 |
|
return [$data]; |
|
|
|
|
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
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.