public function next() { $this->itemCursor++; // if cursor points at result of next page, try to load it if ($this->itemCursor < $this->perPage || $this->itemCursor % $this->perPage !== 0) { return; } if (isset($this->resources[$this->pageCursor + 1])) { // already loaded $this->itemCursor = 0; $this->pageCursor++; return; } if ($this->loadedMaxResults()) { return; } if (!($nextPage = $this->responses[$this->pageCursor]->getPaginationLink('next'))) { return; // end } try { $prevRequest = $this->responses[$this->pageCursor]->getRequest(); $response = $this->httpClient->makeRequest($prevRequest->copyWithUrl($nextPage)); $this->itemCursor = 0; $this->pageCursor++; $this->responses[$this->pageCursor] = $response; $this->resources[$this->pageCursor] = $response->toArray(); } catch (\Exception $e) { $this->itemCursor--; // revert back so the user can continue if needed } }
/** * Retrieves an access token for the given authorization code * (previously generated from www.github.com on behalf of a specific user). * The authorization code is sent to api.github.com/oauth * and a legitimate access token is generated provided the access token * and the user for which it was generated all match, and the user is * either logged in to Github or has granted an offline access permission. * * @param string $code An authorization code. * @param null $redirectUri * @return mixed An access token exchanged for the authorization code, or false if an access token could not be generated. */ protected function getAccessTokenFromCode($code, $redirectUri = NULL) { if (empty($code)) { return FALSE; } if ($redirectUri === NULL) { $redirectUri = $this->getCurrentUrl(); parse_str($redirectUri->getQuery(), $query); unset($query['code'], $query['state']); $redirectUri->setQuery($query); } try { $url = $this->config->createUrl('oauth', 'access_token', array('client_id' => $this->config->appId, 'client_secret' => $this->config->appSecret, 'code' => $code, 'redirect_uri' => $redirectUri)); $response = $this->httpClient->makeRequest(new Api\Request($url, Api\Request::POST, array(), array('Accept' => 'application/json'))); if (!$response->isOk() || !$response->isJson()) { return FALSE; } $token = $response->toArray(); } catch (\Exception $e) { // most likely that user very recently revoked authorization. // In any event, we don't have an access token, so say so. return FALSE; } return isset($token['access_token']) ? $token['access_token'] : FALSE; }