Beispiel #1
0
 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);
 }