Enilsson9 /
weathery
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Edward\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 WeatherIpController implements ContainerInjectableInterface |
||
| 22 | { |
||
| 23 | use ContainerInjectableTrait; |
||
| 24 | |||
| 25 | |||
| 26 | |||
| 27 | /** |
||
| 28 | * @var string $db a sample member variable that gets initialised |
||
| 29 | */ |
||
| 30 | private $ipAddress; |
||
| 31 | private $time; |
||
| 32 | private $object; |
||
| 33 | private $requester; |
||
| 34 | |||
| 35 | |||
| 36 | /** |
||
| 37 | * Display the view |
||
| 38 | * |
||
| 39 | * @return object |
||
| 40 | */ |
||
| 41 | 1 | public function indexAction() : object |
|
| 42 | { |
||
| 43 | 1 | $title = "Check IP"; |
|
| 44 | |||
| 45 | 1 | $page = $this->di->get("page"); |
|
| 46 | 1 | $request = $this->di->get("request"); |
|
| 47 | 1 | $this->requester = $this->di->get("requester"); |
|
| 48 | |||
| 49 | 1 | $this->ipAddress = $request->getGet("ip"); |
|
| 50 | 1 | $this->time = $request->getGet("time"); |
|
| 51 | |||
| 52 | 1 | $currentIp = $this->ipAddress; |
|
|
0 ignored issues
–
show
Unused Code
introduced
by
Loading history...
|
|||
| 53 | 1 | $this->object = new WeatherIp(); |
|
| 54 | |||
| 55 | 1 | $currentIp = $this->object->validateIp($this->ipAddress); |
|
| 56 | |||
| 57 | 1 | $accessKey = '49a95e2b98f16776978bbf2d3097c542'; |
|
| 58 | 1 | $details = $this->requester->curlJson('http://api.ipstack.com/'.$currentIp.'?access_key='.$accessKey); |
|
| 59 | 1 | $weather = $this->multiCurlJson($details); |
|
| 60 | |||
| 61 | 1 | $data["details"] = $details; |
|
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
| 62 | 1 | $data["weather"] = $weather; |
|
| 63 | 1 | $data["currentIp"] = $currentIp; |
|
| 64 | |||
| 65 | |||
| 66 | 1 | $page->add("anax/v2/weather/index", $data); |
|
| 67 | |||
| 68 | 1 | return $page->render([ |
|
| 69 | 1 | "title" => $title, |
|
| 70 | ]); |
||
| 71 | } |
||
| 72 | |||
| 73 | 1 | public function multiCurlJson($details) |
|
| 74 | { |
||
| 75 | 1 | $accessKey = '29b7a65dbbc991295815b55e7a37f93b'; |
|
| 76 | |||
| 77 | 1 | $multiRequests = []; |
|
| 78 | #future weather |
||
| 79 | 1 | if ($this->time === "future") { |
|
| 80 | for ($i=0; $i < 7; $i++) { |
||
| 81 | $unixTime = time() + ($i * 24 * 60 * 60); |
||
| 82 | $multiRequests[] = 'https://api.darksky.net/forecast/'.$accessKey .'/'.$details['latitude'].','.$details['longitude'].','.$unixTime.'?exclude=minutely,hourly,daily,flags'; |
||
| 83 | } |
||
| 84 | } |
||
| 85 | |||
| 86 | #previous weather |
||
| 87 | 1 | if ($this->time === "past") { |
|
| 88 | for ($i=0; $i < 30; $i++) { |
||
| 89 | $unixTime = time() - ($i * 24 * 60 * 60); |
||
| 90 | $multiRequests[] = 'https://api.darksky.net/forecast/'.$accessKey .'/'.$details['latitude'].','.$details['longitude'].','.$unixTime.'?exclude=minutely,hourly,daily,flags'; |
||
| 91 | } |
||
| 92 | } |
||
| 93 | |||
| 94 | |||
| 95 | 1 | $weather = $this->requester->multiRequest($multiRequests); |
|
| 96 | |||
| 97 | 1 | foreach ($weather as $key => $value) { |
|
| 98 | $weather[$key] = json_decode(stripslashes($value), true); |
||
| 99 | } |
||
| 100 | |||
| 101 | 1 | return $weather; |
|
| 102 | } |
||
| 103 | } |
||
| 104 |