| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace wlbrough\clearbit; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use wlbrough\clearbit\Exceptions\ClearbitException; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 7 |  |  | class Clearbit | 
            
                                                                        
                            
            
                                    
            
            
                | 8 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 9 |  |  |     private static $clearbitKey = null; | 
            
                                                                        
                            
            
                                    
            
            
                | 10 |  |  |     private static $httpClient = null; | 
            
                                                                        
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  |     private $instanceKey; | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |      * Clearbit constructor. | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |      * @param $key Clearbit API Key | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |      * @throws ClearbitException | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 | 27 |  |     public function __construct($key = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 | 27 |  |         if ($key === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 | 18 |  |             if (self::$clearbitKey === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 | 3 |  |                 $msg = 'No token provided and none globally set. '; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 | 3 |  |                 $msg .= 'Use Clearbit::setKey, or instantiate the Clearbit class with a $clearbit_key parameter.'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 | 8 |  |                 throw new ClearbitException($msg); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 | 10 |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 | 9 |  |             self::validateKey($key); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 9 |  |             $this->instanceKey = $key; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 33 | 24 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 66 |  |     public static function setKey($key) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 66 |  |         self::validateKey($key); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 48 |  |         self::$clearbitKey = $key; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 39 | 48 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 24 |  |     public static function setClient($client) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 24 |  |         self::$httpClient = $client; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 44 | 24 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 21 |  |     public static function createEnrichmentApi() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 21 |  |         return new EnrichmentApi(self::$clearbitKey, self::$httpClient); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 49 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 3 |  |     public function enrichmentApi() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 3 |  |         return new EnrichmentApi($this->getKey(), self::$httpClient); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 54 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 6 |  |     public static function createNameToDomain() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 6 |  |         return new NameToDomainApi(self::$clearbitKey, self::$httpClient); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 59 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     public function nameToDomain() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         return new NameToDomainApi($this->getKey(), self::$httpClient); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 64 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 69 |  |     private static function validateKey($key) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 69 |  |         if (!is_string($key)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 9 |  |             throw new \InvalidArgumentException('Key is not a string'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 60 |  |         if (strlen($key) < 4) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 9 |  |             throw new \InvalidArgumentException('Token too short'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 51 |  |         return true; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 77 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 9 |  |     public function getKey() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 9 |  |         return $this->instanceKey ?: self::$clearbitKey; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 82 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 83 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 84 |  |  |  |