private function _findMaxResults($minimumPage, $maximumPage) { $currentPage = floor(($maximumPage + $minimumPage) / 2); if ($this->_shouldLog) { $this->_logDebug(sprintf('Starting new iteration. Minimum page is <code>%d</code>, maximum page is <code>%d</code>, current page is <code>%d</code>', $minimumPage, $maximumPage, $currentPage)); } $url = $this->_urlFetched->getClone(); $query = $url->getQuery(); $query->set('fields', 'id'); $query->set('limit', 100); $query->set('page', $currentPage); $result = $this->_apiUtility->getDecodedApiResponse($url); $currentPageCount = count($this->_arrayReader->getAsArray($result, 'list')); $hasMore = $this->_arrayReader->getAsBoolean($result, 'has_more'); unset($url); unset($query); unset($result); if ($this->_shouldLog) { $this->_logDebug(sprintf('Page <code>%d</code> contains <code>%d</code> videos and <code>has_more</code> is <code>%s</code>', $currentPage, $currentPageCount, $hasMore ? 'true' : 'false')); } /* * There are 4 possible cases here: * * 1. We have over 10,000 results. * 2. Middle page is the last page in the result set. * 3. Middle page overshoots the last page. * 4. Middle page undershoots the last page. */ if ($hasMore) { //scenario 1 if (intval($currentPage) === 100) { if ($this->_shouldLog) { $this->_logDebug('There are over 10K videos in this result set.'); } // we've hit the max return 10000; } if ($this->_shouldLog) { $this->_logDebug('We have undershot the last page in the result set.'); } //scenario 4 return $this->_findMaxResults($currentPage + 1, $maximumPage); } // scenario 2 if ($currentPageCount > 0) { if ($this->_shouldLog) { $this->_logDebug('Looks like this was the last page in the result set.'); } return ($currentPage - 1) * 100 + $currentPageCount; } if ($this->_shouldLog) { $this->_logDebug('We have overshot the last page in the result set.'); } // scenario 3 return $this->_findMaxResults($minimumPage, $currentPage - 1); }
/** * @dataProvider getDataDecodedResponse */ public function testGetDecodedApiResponse($responseContents, $exceptionMessage, $expected) { $mockUrl = $this->mock(tubepress_api_url_UrlInterface::_); $mockRequest = $this->mock('tubepress_api_http_message_RequestInterface'); $mockResponse = $this->mock('tubepress_api_http_message_ResponseInterface'); $mockBody = $this->mock('tubepress_api_streams_StreamInterface'); $this->_mockHttpClient->shouldReceive('createRequest')->once()->with('GET', $mockUrl, array('foo' => 'bar'))->andReturn($mockRequest); $this->_mockHttpClient->shouldReceive('send')->once()->with($mockRequest)->andReturn($mockResponse); $mockRequest->shouldReceive('getConfig')->once()->andReturn(array('initial' => 'config')); $mockRequest->shouldReceive('setConfig')->once()->with(array('initial' => 'config', 'tubepress-remote-api-call' => true)); $mockUrl->shouldReceive('toString')->once()->andReturn('some url'); $mockResponse->shouldReceive('getBody')->once()->andReturn($mockBody); $mockBody->shouldReceive('toString')->once()->andReturn($responseContents); if ($exceptionMessage) { $this->setExpectedException('RuntimeException', $exceptionMessage); } $actual = $this->_sut->getDecodedApiResponse($mockUrl, array('foo' => 'bar')); if ($expected) { $this->assertEquals($expected, $actual); } }
public function getValueMap() { if (!isset($this->_cache)) { $url = $this->_urlFactory->fromString($this->_urlAsString); $response = $this->_apiUtility->getDecodedApiResponse($url); $list = $this->_arrayReader->getAsArray($response, 'list'); $this->_cache = array(); foreach ($list as $entry) { if (!isset($entry[$this->_codeKey])) { continue; } $code = $entry[$this->_codeKey]; $displayName = $this->getDisplayNameFromCode($code, $entry); if ($displayName) { $this->_cache[$code] = "{$code} - {$displayName}"; } } ksort($this->_cache); $this->_cache = array_merge(array('none' => 'select ...'), $this->_cache); } return $this->_cache; }