/** * @param BuzzClientInterface $client */ public function __construct(BuzzClientInterface $client = null) { // @todo[1]: This exists for keeping BC. To be removed! $this->client = is_null($client) ? new Curl() : $client; $this->httpClient = new Client(array(), $client); $this->httpClient->addListener(new NormalizeArrayListener()); }
/** * Fetch access token with a grant_type of client_credentials * * @access public * @return array * * throws \InvalidArgumentException * @throws HttpResponseException */ protected function getAccessToken() { $response = $this->httpClient->post(self::ENDPOINT_ACCESS_TOKEN, array('grant_type' => 'client_credentials', 'client_id' => self::$config['client_id'], 'client_secret' => self::$config['client_secret'], 'scope' => implode(',', self::$config['scopes']))); $data = json_decode($response->getContent(), true); if (json_last_error() !== JSON_ERROR_NONE) { $ex = new HttpResponseException('[access_token] Invalid JSON: ' . json_last_error_msg()); $ex->setResponse($this->httpClient->getLastResponse())->setRequest($this->httpClient->getLastRequest()); throw $ex; } if (false === array_key_exists('access_token', $data)) { throw new HttpResponseException('access_token is missing from response. ' . $response->getContent()); } return $data; }
/** * Fetches all the results, using API version 1.0. * * @param array $result * @param string $valuesKey * * @return array * * @throws \RuntimeException */ protected function fetchApi1($result, $valuesKey = 'values') { /** @var Request $request */ $request = $this->client->getLastRequest(); if (!array_key_exists($valuesKey, $result)) { throw new \RuntimeException(sprintf('No values-key "%s" found in resource "%s", please report this bug to the Gush developers.', $valuesKey, $request->getResource())); } $fullResult = $result[$valuesKey]; if ($this->perPage !== null && $this->perPage <= 50) { return $fullResult; } // The api is limited to 50 per page // So everything higher then 50 requires additional call(s) $urlComponents = parse_url($request->getResource()); parse_str($urlComponents['query'], $query); $urlComponents['query'] = $query; $url = $request->getHost() . $urlComponents['path'] . '?'; $limit = $urlComponents['query']['limit']; $count = isset($result['count']) ? $result['count'] : $urlComponents['limit']; $pages = ceil($count / $limit); for ($page = $this->page + 1; $page <= $pages; ++$page) { $urlComponents['query']['start'] = abs($page - 1) * $limit; $response = $this->client->get($url . http_build_query($urlComponents['query'], '', '&')); $result = json_decode($response->getContent(), true); $fullResult = array_merge($fullResult, $result[$valuesKey]); // We only wanted one, so stop now if ($this->perPage !== null) { break; } } return $fullResult; }