diff --git a/src/include/class-wc-retailcrm-history.php b/src/include/class-wc-retailcrm-history.php index 349999e..95ae7b5 100644 --- a/src/include/class-wc-retailcrm-history.php +++ b/src/include/class-wc-retailcrm-history.php @@ -112,19 +112,20 @@ if (!class_exists('WC_Retailcrm_History')) : */ protected function customersHistory($date, $sinceId) { - $filter = array('startDate' => $date); + $filter = ['startDate' => $date]; if ($sinceId) { - $filter = array('sinceId' => $sinceId); + $filter = ['sinceId' => $sinceId]; } $request = new WC_Retailcrm_Paginated_Request(); $history = $request ->setApi($this->retailcrm) ->setMethod('customersHistory') - ->setParams(array($filter, '{{page}}')) + ->setParams([$filter, '{{page}}']) ->setDataKey('history') ->setLimit(100) + ->setPageLimit(25) ->execute() ->getData(); @@ -200,20 +201,21 @@ if (!class_exists('WC_Retailcrm_History')) : */ protected function ordersHistory($date, $sinceId) { - $filter = array('startDate' => $date); + $filter = ['startDate' => $date]; $options = array_flip(array_filter($this->retailcrmSettings)); if ($sinceId) { - $filter = array('sinceId' => $sinceId); + $filter = ['sinceId' => $sinceId]; } $request = new WC_Retailcrm_Paginated_Request(); $history = $request ->setApi($this->retailcrm) ->setMethod('ordersHistory') - ->setParams(array($filter, '{{page}}')) + ->setParams([$filter, '{{page}}']) ->setDataKey('history') ->setLimit(100) + ->setPageLimit(25) ->execute() ->getData(); diff --git a/src/include/components/class-wc-retailcrm-paginated-request.php b/src/include/components/class-wc-retailcrm-paginated-request.php index eb62ae3..2aaff6f 100644 --- a/src/include/components/class-wc-retailcrm-paginated-request.php +++ b/src/include/components/class-wc-retailcrm-paginated-request.php @@ -43,6 +43,11 @@ class WC_Retailcrm_Paginated_Request */ private $data; + /** + * @var int|null + */ + private $pageLimit; + /** * WC_Retailcrm_Paginated_Request constructor. */ @@ -61,6 +66,7 @@ class WC_Retailcrm_Paginated_Request public function setApi($api) { $this->api = $api; + return $this; } @@ -74,6 +80,7 @@ class WC_Retailcrm_Paginated_Request public function setMethod($method) { $this->method = $method; + return $this; } @@ -87,6 +94,7 @@ class WC_Retailcrm_Paginated_Request public function setParams($params) { $this->params = $params; + return $this; } @@ -100,6 +108,7 @@ class WC_Retailcrm_Paginated_Request public function setDataKey($dataKey) { $this->dataKey = $dataKey; + return $this; } @@ -113,6 +122,21 @@ class WC_Retailcrm_Paginated_Request public function setLimit($limit) { $this->limit = $limit; + + return $this; + } + + /** + * Sets page limit per call + * + * @param int $pageLimit + * + * @return self + */ + public function setPageLimit($pageLimit) + { + $this->pageLimit = $pageLimit; + return $this; } @@ -129,7 +153,7 @@ class WC_Retailcrm_Paginated_Request do { $response = call_user_func_array( - array($this->api, $this->method), + [$this->api, $this->method], $this->buildParams($this->params, $page) ); @@ -138,6 +162,10 @@ class WC_Retailcrm_Paginated_Request $page = $response['pagination']['currentPage'] + 1; } + if ($this->pageLimit !== null && $page > $this->pageLimit) { + break; + } + time_nanosleep(0, 300000000); } while ($response && (isset($response['pagination']) && $response['pagination']['currentPage'] < $response['pagination']['totalPageCount'])); @@ -164,6 +192,7 @@ class WC_Retailcrm_Paginated_Request { $this->method = ''; $this->limit = 100; + $this->pageLimit = null; $this->data = array(); return $this;