| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace CodeblogPro\GeoLocation\Controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use CodeblogPro\GeoLocation\Application\Services\GeoLocationService; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use \Illuminate\Routing\Controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use function GuzzleHttp\json_decode; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use function GuzzleHttp\json_encode; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use CodeblogPro\GeoLocation\Application\Exceptions\IncorrectIpException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use CodeblogPro\GeoLocation\Application\Exceptions\ConnectException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use CodeblogPro\GeoLocation\Application\Exceptions\GeoLocationAppException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use CodeblogPro\GeoLocation\Application\Exceptions\IncorrectLanguageCodeException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use CodeblogPro\GeoLocation\Application\Exceptions\IncorrectResponseContent; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  | class GeoLocationController extends Controller | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     public function show(string $ip = '', string $language = '') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |         $geoLocationService = resolve(GeoLocationService::class); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |             $result = ['data' => $geoLocationService->getLocationArrayByIpAndLanguageResultCode($ip, $language)]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         } catch (IncorrectIpException $exception) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |             $result = ['error' => 'Incorect ip.' . $exception->getMessage()]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |             return response()->json($result, 400); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         } catch (IncorrectLanguageCodeException $exception) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |             $result = ['error' => 'Incorect language code.' . $exception->getMessage()]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |             return response()->json($result, 400); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         } catch (ConnectException | GeoLocationAppException | IncorrectResponseContent $exception) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |             $result = ['error' => 'Server error. ' . $exception->getMessage()]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |             return response()->json($result, 500); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         } catch (\Exception $exception) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |             $result = ['error' => 'Unexpected error. ' . $exception->getMessage()]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |             return response()->json($result, 500); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 41 |  |  |         return response()->json($result); | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     public function incorrectMethod() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         $result = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             'status' => 405, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             'body' => ['error' => ['message' => 'Method Not Allowed']] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 51 |  |  |         return response()->json($result['body'], $result['status']); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 53 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 54 |  |  |  |