| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Lii\Controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Anax\Commons\ContainerInjectableInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Anax\Commons\ContainerInjectableTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Lii\Model\IPValidator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Lii\Model\WeatherReport; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | // use Anax\Route\Exception\ForbiddenException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | // use Anax\Route\Exception\NotFoundException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | // use Anax\Route\Exception\InternalErrorException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * A sample controller to show how a controller class can be implemented. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * The controller will be injected with $di if implementing the interface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * ContainerInjectableInterface, like this sample class does. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * The controller is mounted on a particular route and can then handle all | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * requests for that mount point. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * @SuppressWarnings(PHPMD.TooManyPublicMethods) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | class WeatherController implements ContainerInjectableInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     use ContainerInjectableTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @var string $db a sample member variable that gets initialised | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     private $db = "not active"; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     public $report = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     public $validator = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * The initialize method is optional and will always be called before the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * target method/action. This is a convienient method where you could | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * setup internal properties that are commonly used by several methods. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     public function initialize() : void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         // Use to initialise member variables. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | //         $this->db = "active"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $service = $this->di->get("apikeys"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $this->report = new WeatherReport($service->getWeatherKey()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $this->validator = new IPValidator($service->getIpKey()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | //     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | //      * This is how a general helper method can be created in the controller. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | //      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | //      * @param string $method as the method that handled the controller | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | //      *                       action. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | //      * @param array  $args   as an array of arguments. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | //      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | //      * @return string as a message to output to help understand how the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | //      *                controller method works. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | //      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | //     private function getDetailsOnRequest(string $method, array $args = []) : string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | //     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | //         $request     = $this->di->get("request"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | //         $router      = $this->di->get("router"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | //         $path        = $request->getRoute(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | //         $httpMethod  = $request->getMethod(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | //         $mount       = rtrim($router->getLastRoute(), "/"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | //         $numArgs     = count($args); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | //         $strArgs     = implode(", ", $args); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | //         $queryString = http_build_query($request->getGet(), '', ', '); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | // | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | //         return <<<EOD | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | //             <h1>$method</h1> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | // | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | //             <p>The controller mountpoint is '$mount'. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | //             <p>The request was '$path' ($httpMethod). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | //             <p>Got '$numArgs' arguments: '$strArgs'. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | //             <p>Query string contains: '$queryString'. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | //             <p>\$db is '{$this->db}'. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | //         EOD; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | //     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      * This is the index method action, it handles: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      * ANY METHOD mountpoint | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * ANY METHOD mountpoint/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * ANY METHOD mountpoint/index | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * @return object | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 1 |  |     public function indexAction() : object | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 1 |  |         $service = $this->di->get("userinfo"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 1 |  |         $page = $this->di->get("page"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         $data = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 1 |  |             "userIP" => $service->getUserIP(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 1 |  |         $page->add("Lii/weather", $data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 1 |  |         return $page->render([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 1 |  |             "title" => __METHOD__, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      * This is the index method action, it handles: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |      * ANY METHOD mountpoint | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |      * ANY METHOD mountpoint/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |      * ANY METHOD mountpoint/index | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |      * @return object | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 119 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 120 | 2 |  |     public function mapActionPost() : object | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 122 | 2 |  |         $request = $this->di->get("request"); | 
            
                                                                        
                            
            
                                    
            
            
                | 123 | 2 |  |         $inputIP = $request->getPost("ip"); | 
            
                                                                        
                            
            
                                    
            
            
                | 124 | 2 |  |         $allWeather = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 125 | 2 |  |         $historicWeather = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 126 |  |  | //         $service = $this->di->get("apikeys"); | 
            
                                                                        
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 128 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 129 |  |  | //         var_dump($inputIP); | 
            
                                                                        
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 131 |  |  | //         $validator = new IPValidator(); | 
            
                                                                        
                            
            
                                    
            
            
                | 132 | 2 |  |         $location = $this->validator->locateIP($inputIP); | 
            
                                                                        
                            
            
                                    
            
            
                | 133 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 134 |  |  | //         var_dump($location); | 
            
                                                                        
                            
            
                                    
            
            
                | 135 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 136 | 2 |  |         if ($location != 'Not valid IP-address.') { | 
            
                                                                        
                            
            
                                    
            
            
                | 137 |  |  | //             $report = new WeatherReport($service->getWeatherKey()); | 
            
                                                                        
                            
            
                                    
            
            
                | 138 | 1 |  |             $allWeather = $this->report->fetchAllWeather($location["latitude"], $location["longitude"]); | 
            
                                                                        
                            
            
                                    
            
            
                | 139 | 1 |  |             $historicWeather = $this->report->getHistoricWeather($this->report->getHistoricDates(), $location["latitude"], $location["longitude"]); | 
            
                                                                        
                            
            
                                    
            
            
                | 140 |  |  |         } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 141 | 1 |  |             $allWeather[0] = ""; | 
            
                                                                        
                            
            
                                    
            
            
                | 142 | 1 |  |             $allWeather[1] = ""; | 
            
                                                                        
                            
            
                                    
            
            
                | 143 | 1 |  |             $historicWeather = ""; | 
            
                                                                        
                            
            
                                    
            
            
                | 144 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 145 | 2 |  |         $page = $this->di->get("page"); | 
            
                                                                        
                            
            
                                    
            
            
                | 146 |  |  |         $data = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 147 | 2 |  |             "location" => $location, | 
            
                                                                        
                            
            
                                    
            
            
                | 148 | 2 |  |             "weather" => $allWeather[0], | 
            
                                                                        
                            
            
                                    
            
            
                | 149 | 2 |  |             "forecast" => $allWeather[1], | 
            
                                                                        
                            
            
                                    
            
            
                | 150 | 2 |  |             "historicWeather" => $historicWeather, | 
            
                                                                        
                            
            
                                    
            
            
                | 151 |  |  |         ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 152 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 153 | 2 |  |         $page->add("Lii/weathermap", $data); | 
            
                                                                        
                            
            
                                    
            
            
                | 154 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 155 | 2 |  |         return $page->render([ | 
            
                                                                        
                            
            
                                    
            
            
                | 156 | 2 |  |             "title" => __METHOD__, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  | //     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  | //      * Using a di-dependent service though $di. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  | //      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  | //     public function userInfoAction() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  | //     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  | //         $service = $this->di->get("userinfo"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  | // | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  | //         $data = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  | // //             "content" => $service->useService(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  | //             "content" => $service->getUserIP(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  | //         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  | // | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  | //         return $this->di->get("page") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  | //             ->add("anax/v2/plain/pre", $data) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  | //             ->render(["title" => "User Info"]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  | //     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  | //     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  | //      * Adding an optional catchAll() method will catch all actions sent to the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  | //      * router. You can then reply with an actual response or return void to | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  | //      * allow for the router to move on to next handler. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  | //      * A catchAll() handles the following, if a specific action method is not | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  | //      * created: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  | //      * ANY METHOD mountpoint/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  | //      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  | //      * @param array $args as a variadic parameter. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  | //      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  | //      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  | //      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  | //      * @SuppressWarnings(PHPMD.UnusedFormalParameter) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  | //      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  | //     public function catchAll(...$args) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  | //     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  | //         $page = $this->di->get("page"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  | //         $data = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  | //             "content" => $this->getDetailsOnRequest(__METHOD__, $args), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  | //         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  | //         $page->add("Lii/default", $data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  | // | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  | //         return $page->render([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  | //             "title" => __METHOD__, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  | //         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  | //     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 203 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 204 |  |  |  |