| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace DevinPearson\BinList; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use GuzzleHttp\Client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use GuzzleHttp\Exception\ClientException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use GuzzleHttp\Exception\ConnectException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  *  BinListConnector. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  *  connects to binlist and retrieves the result | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * @author Devin Pearson | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | class BinListConnector | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      * @var Client guzzle client for calling the http requests | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     private $client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * @var string the base url of the binlist api | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     private $baseUrl = 'https://lookup.binlist.net/'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * BinListConnector constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @param Client|null $client | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 31 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 32 | 15 |  |     public function __construct(Client $client = null) | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 34 | 15 |  |         $this->client = $client; | 
            
                                                                        
                            
            
                                    
            
            
                | 35 | 15 |  |         if (!$client instanceof Client) { | 
            
                                                                        
                            
            
                                    
            
            
                | 36 | 12 |  |             $this->client = new Client(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 15 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * Check Method. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * Fetches the bin number details from binlist.com | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * @param string $binNumber the bin number your searching for | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 3 |  |     public function check($binNumber) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 3 |  |         $url = $this->baseUrl.$binNumber; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 3 |  |         return $this->request($url); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * Returns the json encoded result. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * @param $url | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * @throws BinListException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 3 |  |     private function request($url) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 3 |  |             $response = $this->client->request( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 3 |  |                 'GET', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 3 |  |                 $url, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                 [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 3 |  |                     'headers' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |                         'Accept-Version' => 3, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |                     ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                 ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 3 |  |             return (string) $response->getBody(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 3 |  |         } catch (ConnectException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 3 |  |             throw new BinListException($e->getMessage()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 3 |  |         } catch (ClientException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 3 |  |             if ($e->getResponse()->getStatusCode() == '404') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 3 |  |                 throw new BinListException('bin not found'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 3 |  |             throw new BinListException($e->getMessage()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 3 |  |         } catch (\GuzzleHttp\Exception\GuzzleException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 3 |  |             throw new BinListException($e->getMessage()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 91 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 92 |  |  |  |