/** * @param $login * @param $lastEventId * @param int $page * * @return array */ private function findNewActivity($login, $lastEventId, $page = 1) { /** @var Response $response */ $response = GitHub::connection()->getHttpClient()->get(sprintf('/users/%s/received_events?page=%s', $login, (int) $page)); // Get the interval Github allows for polling $interval = $response->hasHeader('X-Poll-Interval') ? (string) $response->getHeader('X-Poll-Interval') : 60; $activity = ResponseMediator::getContent($response); $pagination = ResponseMediator::getPagination($response); $isLastPage = empty($pagination['next']); $caughtUp = false; $newActivity = []; foreach ($activity as $event) { if ($event['id'] == $lastEventId) { $caughtUp = true; break; } $newActivity[] = $event; } if (!$caughtUp && !$isLastPage) { // Try the next page list($interval, $activity) = $this->findNewActivity($login, $lastEventId, $page + 1); $newActivity += $activity; } return [$interval, $newActivity]; }
public function getPagination(Response $response) { $ghPagination = ResponseMediator::getPagination($response); $pagination = ['first' => null, 'prev' => null, 'next' => null, 'last' => null, 'count' => 0]; if ($ghPagination) { foreach ($ghPagination as $key => $url) { if (preg_match('/(.*?)\\?page=(.*?)$/', $url, $match)) { $pagination[$key] = (int) $match[2]; } } if (null !== $pagination['last']) { $pagination['count'] = $pagination['last']; } elseif (null !== $pagination['next']) { $pagination['count'] = $pagination['next']; } elseif (null !== $pagination['prev']) { $pagination['count'] = $pagination['prev'] + 1; } } return $pagination; }
/** * {@inheritdoc} */ public function postFetch() { $this->pagination = ResponseMediator::getPagination($this->client->getHttpClient()->getLastResponse()); }
/** * @test */ public function shouldHandlePagination() { $path = '/some/path'; $body = 'a = b'; $headers = array('c' => 'd'); $response = new Response(200); $response->addHeader('Link', "<page1>; rel=\"page2\", \n<page3>; rel=\"page4\""); $client = $this->getBrowserMock(); $httpClient = new HttpClient(array(), $client); $httpClient->request($path, $body, 'HEAD', $headers); $this->assertEquals(array('page2' => 'page1', 'page4' => 'page3'), ResponseMediator::getPagination($response)); }
private function getAllProjects(Remote $remote) { $client = $this->getClient($remote); $projects = array(); $page = 1; while (true) { $response = $client->getHttpClient()->get('/user/repos', array('page' => $page, 'per_page' => 100)); $projects = array_merge($projects, ResponseMediator::getContent($response)); $pageInfo = ResponseMediator::getPagination($response); if (!isset($pageInfo['next'])) { break; } $page++; } return $projects; }