| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace wlbrough\clearbit\Abstracts; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use GuzzleHttp\Client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use wlbrough\clearbit\Exceptions\ApiException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | abstract class Api | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |     protected $endpointUrl = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     protected $useStreaming = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     protected $httpClient = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 | 27 |  |     public function setWebhookEndpoint($endpointUrl = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 | 27 |  |         self::validateUrl($endpointUrl); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 | 12 |  |         $this->endpointUrl = $endpointUrl; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 | 12 |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 20 | 3 |  |     public function enableStreaming() | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 22 | 3 |  |         $this->useStreaming = true; | 
            
                                                                        
                            
            
                                    
            
            
                | 23 | 3 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 | 27 |  |     private static function validateUrl($url) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 | 27 |  |         if (!is_string($url)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 | 9 |  |             throw new \InvalidArgumentException('Webhook endpoint is not a string'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 18 |  |         $isValid = preg_match('#((https?|ftp)://(\S*?\.\S*?))([\s)\[\]{},;"\':<]|\.\s|$)#i', $url); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 18 |  |         if (!$isValid) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 6 |  |             throw new \InvalidArgumentException('Webhook endpoint is not a URL'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 12 |  |         return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 24 |  |     protected static function call($url, $client) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 24 |  |         if (!$client) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |             $client = new Client(['http_errors' => false]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 24 |  |         $response = $client->get($url); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 24 |  |         $status = $response->getStatusCode(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 24 |  |         if ($status === 200) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 12 |  |             $returnData = json_decode($response->getBody()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 8 |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 12 |  |             throw new ApiException($status); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 12 |  |         return $returnData; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     protected static function post($url, $body, $successCode = 200, $client = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         if (!$client) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             $client = new Client(['http_errors' => false]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         $response = $client->post($url, [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             'form_params' => $body | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         $status = $response->getStatusCode(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         return $status === $successCode; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 71 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 72 |  |  |  |