| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace ddlzz\AmoAPI; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use ddlzz\AmoAPI\Exception\InvalidArgumentException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use ddlzz\AmoAPI\Validator\SettingsValidator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * Class SettingsStorage. All amo- and library related variables are stored here. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @author ddlzz | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 12 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  | class SettingsStorage | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |     const LIB_PATH = __DIR__.'/..'; | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |     const NAMESPACE_PREFIX = '\ddlzz\AmoAPI'; | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |     const SENDER_HTTP_HEADER = 'Content-Type: hal/json'; | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |     /** @var array */ | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |     private $methodsPaths = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |         'auth' => '/private/api/auth.php?type=json', | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |         'current' => '/api/v2/account?with=users,custom_fields', | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |         'leads' => '/api/v2/leads', | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |         'contacts' => '/api/v2/contacts', | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |         'companies' => '/api/v2/companies', | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |         'tasks' => '/api/v2/tasks', | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |         'customers' => '/api/v2/customers', | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |         'notes' => '/api/v2/notes', | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |     ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |     /** @var array */ | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |     private $entitiesTypes = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |         'lead' => 'leads', | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |         'contact' => 'contacts', | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |         'company' => 'companies', | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |         'customer' => 'customers', | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |         'task' => 'tasks', | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |         'note' => 'notes', | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |     ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |     /** @var SettingsValidator */ | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |     private $validator; | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |     /** @var string */ | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |     private $scheme = 'https'; | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |     /** @var string */ | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |     private $domain = 'amocrm.ru'; | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |     /** @var string */ | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |     private $userAgent = 'amoAPI PHP Client'; | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |     /** @var string */ | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |     private $cookiePath = self::LIB_PATH.'/var/cookie.txt'; | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |      * SettingsStorage constructor. | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 61 | 34 |  |     public function __construct() | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 63 | 34 |  |         $this->validator = new SettingsValidator(); // Composition | 
            
                                                                        
                            
            
                                    
            
            
                | 64 | 34 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |      * @return string | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 1 |  |     public function getScheme() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 71 | 1 |  |         return $this->scheme; | 
            
                                                                        
                                                                
            
                                    
            
            
                | 72 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |      * @param string $scheme | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 77 | 5 |  |     public function setScheme($scheme) | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 79 | 5 |  |         $this->validator->validateScheme($scheme); | 
            
                                                                        
                            
            
                                    
            
            
                | 80 | 1 |  |         $this->scheme = $scheme; | 
            
                                                                        
                            
            
                                    
            
            
                | 81 | 1 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |      * @return string | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 86 | 1 |  |     public function getDomain() | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 88 | 1 |  |         return $this->domain; | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |      * @param string $domain | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 94 | 5 |  |     public function setDomain($domain) | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 96 | 5 |  |         $this->validator->validateDomain($domain); | 
            
                                                                        
                            
            
                                    
            
            
                | 97 | 1 |  |         $this->domain = $domain; | 
            
                                                                        
                            
            
                                    
            
            
                | 98 | 1 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |      * @return string | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 103 | 1 |  |     public function getUserAgent() | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 105 | 1 |  |         return $this->userAgent; | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |      * @param string $userAgent | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 | 4 |  |     public function setUserAgent($userAgent) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 4 |  |         $this->validator->validateUserAgent($userAgent); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 114 | 1 |  |         $this->userAgent = $userAgent; | 
            
                                                                        
                            
            
                                    
            
            
                | 115 | 1 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  |      * @return array | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 120 | 2 |  |     public function getMethodsPaths() | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 122 | 2 |  |         return $this->methodsPaths; | 
            
                                                                        
                            
            
                                    
            
            
                | 123 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 125 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 126 |  |  |      * @param string $code | 
            
                                                                        
                            
            
                                    
            
            
                | 127 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 128 |  |  |      * @throws InvalidArgumentException | 
            
                                                                        
                            
            
                                    
            
            
                | 129 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 130 |  |  |      * @return string mixed | 
            
                                                                        
                            
            
                                    
            
            
                | 131 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 | 2 |  |     public function getMethodPath($code) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 | 2 |  |         if (!isset($this->methodsPaths[$code])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 | 1 |  |             throw new InvalidArgumentException("The method with code \"$code\" doesn't exist"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 138 | 1 |  |         return $this->methodsPaths[$code]; | 
            
                                                                        
                            
            
                                    
            
            
                | 139 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 140 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 141 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 142 |  |  |      * @param array $paths | 
            
                                                                        
                            
            
                                    
            
            
                | 143 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 | 7 |  |     public function setMethodsPaths(array $paths) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 | 7 |  |         $this->validator->validateMethodsPaths($paths); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 147 | 2 |  |         $this->methodsPaths = $paths; | 
            
                                                                        
                            
            
                                    
            
            
                | 148 | 2 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 149 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 150 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 151 |  |  |      * @return string | 
            
                                                                        
                            
            
                                    
            
            
                | 152 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 153 | 1 |  |     public function getCookiePath() | 
            
                                                                        
                            
            
                                    
            
            
                | 154 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 155 | 1 |  |         return $this->cookiePath; | 
            
                                                                        
                            
            
                                    
            
            
                | 156 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 157 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 158 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 159 |  |  |      * @param string $path | 
            
                                                                        
                            
            
                                    
            
            
                | 160 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 | 5 |  |     public function setCookiePath($path) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 | 5 |  |         $this->validator->validateCookiePath($path); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 164 | 1 |  |         $this->cookiePath = $path; | 
            
                                                                        
                                                                
            
                                    
            
            
                | 165 | 1 |  |     } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 166 |  |  |  | 
            
                                                                        
                                                                
            
                                    
            
            
                | 167 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 168 |  |  |      * @param string $type | 
            
                                                                        
                            
            
                                    
            
            
                | 169 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 170 |  |  |      * @throws InvalidArgumentException | 
            
                                                                        
                            
            
                                    
            
            
                | 171 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 172 |  |  |      * @return string | 
            
                                                                        
                            
            
                                    
            
            
                | 173 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 | 7 |  |     public function getMethodCodeByType($type) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 | 7 |  |         if (!isset($this->entitiesTypes[$type])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 | 1 |  |             throw new InvalidArgumentException("The entity with type \"$type\" doesn't exist"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 180 | 6 |  |         return $this->entitiesTypes[$type]; | 
            
                                                                        
                                                                
            
                                    
            
            
                | 181 |  |  |     } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 182 |  |  | } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 183 |  |  |  |