Exemple #1
0
 /**
  * Check for time to live and refresh cache and token info
  *
  * @return mixed
  * @throws Exception
  */
 protected function _authenticate()
 {
     $parameters = array();
     $parameters['client_id'] = $this->client_id;
     $parameters['client_secret'] = $this->client_secret;
     /**
      * Hardcoded params, from documentation
      * @see https://msdn.microsoft.com/en-us/library/hh454950.aspx
      */
     $parameters['grant_type'] = "client_credentials";
     $parameters['scope'] = "http://api.microsofttranslator.com";
     $url = $this->oauth_url;
     $curl_opt = array(CURLOPT_POST => true, CURLOPT_POSTFIELDS => http_build_query($parameters), CURLOPT_TIMEOUT => 120);
     $rawValue = $this->_call($url, $curl_opt);
     $objResponse = json_decode($rawValue, true);
     if (isset($objResponse['error'])) {
         //format as a normal Translate Response and send to decoder to output the data
         $rawValue = sprintf($this->rawXmlErrStruct, $objResponse['error'], 'getToken', $objResponse['error_description']);
         $this->result = $this->_decode($rawValue, $parameters);
         throw new Exception($objResponse['error_description']);
     } else {
         $this->token = $objResponse['access_token'];
         /**
          * Gain a minute to not fallback into a recursion
          *
          * @see Engines_MicrosoftHub::get
          */
         $this->token_endlife = time() + $objResponse['expires_in'] - 60;
     }
     $record = clone $this->engineRecord;
     $engineDAO = new EnginesModel_EngineDAO(Database::obtain());
     /**
      * Use a generic Engine and not Engine_MicrosoftHubStruct
      * because the Engine Factory Class built the query as generic engine
      *
      */
     $engineStruct = EnginesModel_MicrosoftHubStruct::getStruct();
     $engineStruct->id = $record->id;
     //variable assignment only used for debugging purpose
     $debugParam = $engineDAO->destroyCache($engineStruct);
     $engineDAO->update($record);
 }