/** * @param array $config an array of configuration parameters * @param \MicrosoftTranslator\HttpInterface|null $http if null, a new Http manager will be used * @param \MicrosoftTranslator\AuthInterface|null $auth if null, a new Auth manager will be used * @param \MicrosoftTranslator\LoggerInterface|null $logger if null, a new Logger manager will be used * * @throws Exception */ public function __construct($config = array(), $http = null, $auth = null, $logger = null) { // Init logger at first if (is_null($logger)) { $logger = new Logger($config); } if (!$logger instanceof LoggerInterface) { throw new Exception('Logger Manager is not an instance of MicrosoftTranslator\\LoggerInterface'); } $this->logger = $logger; // Load configuration for Client foreach ($this->config_keys as $key) { if (isset($config[$key])) { $this->{$key} = $config[$key]; $this->logger->debug(__CLASS__, 'config', sprintf('%s = %s', $key, $this->{$key})); } } // Init HTTP Manager if (is_null($http)) { $http = new Http($config, $logger); } if (!$http instanceof HttpInterface) { throw new Exception('HTTP Manager is not an instance of MicrosoftTranslator\\HttpInterface'); } $this->http = $http; // Init Auth Manager if (is_null($auth)) { $auth = new Auth($config, $logger, $http); } if (!$auth instanceof AuthInterface) { throw new Exception('Auth Manager is not an instance of MicrosoftTranslator\\AuthInterface'); } $this->auth = $auth; }
/** * @param array $config * @param \MicrosoftTranslator\LoggerInterface $logger */ public function __construct($config, LoggerInterface $logger) { $this->logger = $logger; foreach ($this->config_keys as $key) { if (isset($config[$key])) { $this->{$key} = $config[$key]; $this->logger->debug(__CLASS__, 'config', $key . ' = ' . $this->{$key}); } } }
/** * Save AT and timestamp in file * * @param string $access_token * @param int $timestamp * * @return bool */ private function save($access_token, $timestamp) { unset($this->runtime_file_content); $bytes = @file_put_contents($this->getFilePath(), json_encode(array('a' => $access_token, 'e' => $timestamp))); if ($bytes === false) { // fatal log will throw an exception $this->logger->fatal(__CLASS__, 'store', sprintf('Unable to store access token in %s', $this->getFilePath())); } $this->logger->debug(__CLASS__, 'store', sprintf('Access token stored in %s', $this->getFilePath())); return true; }
/** * @return array|string * @throws \MicrosoftTranslator\Exception */ private function generateAndStoreNewAccessToken() { $url = trim($this->auth_base_url, "/ \t\n\r\v"); $access_token = null; $auth = array('grant_type' => 'client_credentials', 'client_id' => $this->api_client_id, 'client_secret' => $this->api_client_secret, 'scope' => $this->api_client_scope); $result = $this->http->post($url, null, $auth, null); if (Http::isRequestOk($result)) { $result['http_body'] = json_decode($result['http_body'], true); if (!isset($result['http_body']['access_token'])) { throw new Exception('Access token not found in response'); } if (!is_string($result['http_body']['access_token'])) { throw new Exception('Access token found in response but it is not a string'); } $access_token = strval(@$result['http_body']['access_token']); $expires_in = @(int) $result['http_body']['expires_in']; $this->logger->debug(__CLASS__, 'oauth', sprintf('New access_token generated %s...', substr($access_token, 0, 10))); $this->guard->storeAccessTokenForSeconds($access_token, $expires_in); } else { $this->logger->fatal(__CLASS__, 'oauth', 'Unable to generate a new access token : ' . json_encode($result)); } return $access_token; }