| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * ©[2016] SugarCRM Inc.  Licensed by SugarCRM under the Apache 2.0 license. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | namespace SugarAPI\SDK\Client\Abstracts; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use SugarAPI\SDK\Client\Interfaces\ClientInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use SugarAPI\SDK\Exception\EntryPoint\EntryPointException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use SugarAPI\SDK\Helpers\Helpers; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use SugarAPI\SDK\EntryPoint\POST\OAuth2Logout; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use SugarAPI\SDK\Exception\Authentication\AuthenticationException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * The Abstract Client implementation for Sugar | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * @package SugarAPI\SDK\Client\Abstracts\AbstractClient | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * @method EPInterface ping() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * @method EPInterface getRecord(string $module = '') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * @method EPInterface getAttachment(string $module = '',string $record_id = '') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * @method EPInterface getChangeLog(string $module = '',string $record_id = '') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * @method EPInterface filterRelated(string $module = '') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * @method EPInterface getRelated(string $module = '',string $record_id = '',string $relationship = '',string $related_id = '') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * @method EPInterface me() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  * @method EPInterface search() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  * @method EPInterface oauth2Token() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  * @method EPInterface oauth2Refresh() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  * @method EPInterface createRecord() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  * @method EPInterface filterRecords() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  * @method EPInterface attachFile() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  * @method EPInterface oauth2Logout() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  * @method EPInterface createRelated() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  * @method EPInterface linkRecords() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  * @method EPInterface bulk() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  * @method EPInterface updateRecord() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  * @method EPInterface favorite() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  * @method EPInterface deleteRecord() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  * @method EPInterface unfavorite() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  * @method EPInterface deleteFile() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  * @method EPInterface unlinkRecords() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | abstract class AbstractClient implements ClientInterface { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * Array of Statically Bound Tokens for SDK Clients | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * - Allows for reinstating objects in multiple areas, without needing to Sign-in | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * - Allows for multiple client_id's to be used between SDK Clients | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * @var array = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      *      $client_id => $token | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     protected static $_STORED_TOKENS = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * The configured server domain name/url on the SDK Client | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     protected $server = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * The API Url configured on the SDK Client | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     protected $apiURL = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * The full token object returned by the Login method | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * @var \stdClass | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     protected $token; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * Array of OAuth Creds to be used by SDK Client | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     protected $credentials = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * Token expiration time | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * @var | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     protected $expiration; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * The list of registered EntryPoints | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     protected $entryPoints = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 1 |  |     public function __construct($server = '',array $credentials = array()){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 1 |  |         $server = (empty($server)?$this->server:$server); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 1 |  |         $this->setServer($server); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 1 |  |         $credentials = (empty($credentials)?$this->credentials:$credentials); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 1 |  |         $this->setCredentials($credentials); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 1 |  |         $this->registerSDKEntryPoints(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |      * @inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |      * @param string $server | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 1 |  |     public function setServer($server) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 1 |  |         $this->server = $server; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 1 |  |         $this->apiURL = Helpers::configureAPIURL($this->server); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 1 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |      * @inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 | 1 |  |     public function getAPIUrl() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 1 |  |         return $this->apiURL; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |      * @inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |      * Retrieves stored token based on passed in Credentials | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 | 2 |  |     public function setCredentials(array $credentials){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 | 2 |  |         $this->credentials = $credentials; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 | 2 |  |         if (isset($this->credentials['client_id'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 | 2 |  |             $token = static::getStoredToken($this->credentials['client_id']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 | 2 |  |             if (!empty($token)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 | 1 |  |                 $this->setToken($token); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 | 1 |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 | 2 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 | 2 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |      * @inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 | 1 |  |     public function setToken(\stdClass $token){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 | 1 |  |         $this->token = $token; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 | 1 |  |         $this->expiration = time()+$token->expires_in; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 | 1 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |      * @inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 | 2 |  |     public function getToken(){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 | 2 |  |         return $this->token; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |      * @inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 | 1 |  |     public function getCredentials(){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 | 1 |  |         return $this->credentials; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |      * @inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 | 1 |  |     public function getServer() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 | 1 |  |         return $this->server; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |      * @inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 | 1 |  |     public function authenticated(){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 | 1 |  |         return time() < $this->expiration; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |      * Register the defined EntryPoints in SDK, located in src/EntryPoint/registry.php file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |      * @throws EntryPointException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 | 1 |  |     protected function registerSDKEntryPoints(){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 | 1 |  |         $entryPoints = Helpers::getSDKEntryPointRegistry(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 | 1 |  |         foreach ($entryPoints as $funcName => $className){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 | 1 |  |             $this->registerEntryPoint($funcName, $className); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 | 1 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |      * @param $funcName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |      * @param $className | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |      * @return self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |      * @throws EntryPointException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 | 2 |  |     public function registerEntryPoint($funcName, $className){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 | 2 |  |         $implements = class_implements($className); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 | 2 |  |         if (is_array($implements) && in_array('SugarAPI\SDK\EntryPoint\Interfaces\EPInterface',$implements)){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 | 1 |  |             $this->entryPoints[$funcName] = $className; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 | 1 |  |         }else{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 | 1 |  |             throw new EntryPointException($className,'Class must extend SugarAPI\SDK\EntryPoint\Interfaces\EPInterface'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 | 1 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |      * Generates the EntryPoint objects based on a Method name that was called | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |      * @param $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |      * @param $params | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  |      * @throws EntryPointException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 | 2 |  |     public function __call($name, $params){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 | 2 |  |         if (array_key_exists($name, $this->entryPoints)){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 | 1 |  |             $Class = $this->entryPoints[$name]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 | 1 |  |             $EntryPoint = new $Class($this->apiURL, $params); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 | 1 |  |             if ($EntryPoint->authRequired() && $this->authenticated()){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 208 | 1 |  |                 $EntryPoint->setAuth($this->token->access_token); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 209 | 1 |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 210 | 1 |  |             return $EntryPoint; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 211 |  |  |         }else{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 212 | 1 |  |             throw new EntryPointException($name,'Unregistered EntryPoint'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 213 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 214 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 215 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 216 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 217 |  |  |      * @inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 218 |  |  |      * @throws AuthenticationException - When Login request fails | 
            
                                                                                                            
                            
            
                                    
            
            
                | 219 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 220 | 1 |  |     public function login() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 221 | 1 |  |         if (!empty($this->credentials)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 222 | 1 |  |             $response = $this->oauth2Token()->execute($this->credentials)->getResponse(); | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 223 | 1 | View Code Duplication |             if ($response->getStatus() == '200') { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 224 |  |  |                 $this->setToken($response->getBody(FALSE)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 225 |  |  |                 static::storeToken($this->token, $this->credentials['client_id']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 226 |  |  |                 return TRUE; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 227 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 228 | 1 |  |                 $error = $response->getBody(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 229 | 1 |  |                 throw new AuthenticationException("Login Response [" . $error['error'] . "] " . $error['error_message']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 230 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 231 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 232 | 1 |  |         return FALSE; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 233 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 234 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 235 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 236 |  |  |      * @inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 237 |  |  |      * @throws AuthenticationException - When Refresh Request fails | 
            
                                                                                                            
                            
            
                                    
            
            
                | 238 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 239 | 1 |  |     public function refreshToken(){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 240 | 1 |  |         if (isset($this->credentials['client_id'])&& | 
            
                                                                                                            
                            
            
                                    
            
            
                | 241 | 1 |  |             isset($this->credentials['client_secret'])&& | 
            
                                                                                                            
                            
            
                                    
            
            
                | 242 | 1 |  |             isset($this->token)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 243 |  |  |             $refreshOptions = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 244 | 1 |  |                 'client_id' => $this->credentials['client_id'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 245 | 1 |  |                 'client_secret' => $this->credentials['client_secret'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 246 | 1 |  |                 'refresh_token' => $this->token->refresh_token | 
            
                                                                                                            
                            
            
                                    
            
            
                | 247 | 1 |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 248 | 1 |  |             $response = $this->oauth2Refresh()->execute($refreshOptions)->getResponse(); | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 249 | 1 | View Code Duplication |             if ($response->getStatus() == '200') { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 250 |  |  |                 $this->setToken($response->getBody(FALSE)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 251 |  |  |                 static::storeToken($this->token, $this->credentials['client_id']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 252 |  |  |                 return TRUE; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 253 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 254 | 1 |  |                 $error = $response->getBody(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 255 | 1 |  |                 throw new AuthenticationException("Refresh Response [" . $error['error'] . "] " . $error['error_message']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 256 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 257 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 258 | 1 |  |         return FALSE; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 259 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 260 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 261 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 262 |  |  |      * @inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 263 |  |  |      * @throws AuthenticationException - When logout request fails | 
            
                                                                                                            
                            
            
                                    
            
            
                | 264 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 265 | 1 |  |     public function logout(){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 266 | 1 |  |         if ($this->authenticated()){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 267 | 1 |  |             $response = $this->oauth2Logout()->execute()->getResponse(); | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 268 | 1 | View Code Duplication |             if ($response->getStatus()=='200'){ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 269 |  |  |                 unset($this->token); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 270 |  |  |                 static::removeStoredToken($this->credentials['client_id']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 271 |  |  |                 return TRUE; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 272 |  |  |             }else{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 273 | 1 |  |                 $error = $response->getBody(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 274 | 1 |  |                 throw new AuthenticationException("Logout Response [".$error['error']."] ".$error['message']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 275 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 276 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 277 | 1 |  |         return FALSE; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 278 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 279 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 280 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 281 |  |  |      * @inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 282 |  |  |      * @param \stdClass $token | 
            
                                                                                                            
                            
            
                                    
            
            
                | 283 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 284 | 1 |  |     public static function storeToken($token, $client_id) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 285 | 1 |  |         static::$_STORED_TOKENS[$client_id] = $token; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 286 | 1 |  |         return TRUE; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 287 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 288 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 289 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 290 |  |  |      * @inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 291 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 292 | 1 |  |     public static function getStoredToken($client_id) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 293 | 1 |  |         return (isset(static::$_STORED_TOKENS[$client_id])?static::$_STORED_TOKENS[$client_id]:NULL); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 294 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 295 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 296 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 297 |  |  |      * @inheritdoc | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 298 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 299 | 1 |  |     public static function removeStoredToken($client_id) { | 
            
                                                        
            
                                    
            
            
                | 300 | 1 |  |         unset(static::$_STORED_TOKENS[$client_id]); | 
            
                                                        
            
                                    
            
            
                | 301 | 1 |  |         return TRUE; | 
            
                                                        
            
                                    
            
            
                | 302 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 303 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 304 |  |  | } | 
            
                        
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.