/** * @param Request $request * @param int $limit * @param int $offset * @param int $num * * @return Response * @throws Exceptions\AppNexusAPIException * @throws \Exception */ private function getDataLoop(Request $request, $limit, $offset, $num) { do { $services = $request->limitBy($limit)->offsetBy($offset)->send(); $this->requestVolume++; if (!isset($response)) { $response = $this->newResponse($services); } else { $response = $this->appendToResponse($response, $services); } // If we're not unlimited and we've got everything we wanted, break. if ($num !== -1 && $response->getNumElements() >= $num) { break; } $offset += $services->getNumElements(); // If the offset and limit ends up being bigger than the total that we want then limit it. if ($num !== -1 && $offset + $limit > $num) { $limit = $num - $offset; } $this->throttleRequests($request); } while ($services->getCount() > $offset); return $response; }