diff --git a/src/Resources/CreditInvoice.php b/src/Resources/CreditInvoice.php index c0b0adf..0bc104c 100644 --- a/src/Resources/CreditInvoice.php +++ b/src/Resources/CreditInvoice.php @@ -20,4 +20,9 @@ public function getResourceName(): string { return self::CONTROLLER_NAME; } + + public function getPluralResourceName(): string + { + return self::CONTROLLER_NAME.'s'; + } } diff --git a/src/Resources/Creditor.php b/src/Resources/Creditor.php index 3f3064f..077476e 100644 --- a/src/Resources/Creditor.php +++ b/src/Resources/Creditor.php @@ -20,4 +20,9 @@ public function getResourceName(): string { return self::CONTROLLER_NAME; } + + public function getPluralResourceName(): string + { + return self::CONTROLLER_NAME.'s'; + } } diff --git a/src/Resources/Debtor.php b/src/Resources/Debtor.php index 272e5a2..d7482eb 100644 --- a/src/Resources/Debtor.php +++ b/src/Resources/Debtor.php @@ -13,6 +13,11 @@ public function getResourceName(): string return self::CONTROLLER_NAME; } + public function getPluralResourceName(): string + { + return self::CONTROLLER_NAME.'s'; + } + /** * @return array */ diff --git a/src/Resources/Group.php b/src/Resources/Group.php index ea57b4d..80461bb 100644 --- a/src/Resources/Group.php +++ b/src/Resources/Group.php @@ -10,4 +10,9 @@ public function getResourceName(): string { return self::CONTROLLER_NAME; } + + public function getPluralResourceName(): string + { + return self::CONTROLLER_NAME.'s'; + } } diff --git a/src/Resources/Invoice.php b/src/Resources/Invoice.php index 20a7a12..89f743d 100644 --- a/src/Resources/Invoice.php +++ b/src/Resources/Invoice.php @@ -26,6 +26,11 @@ public function getResourceName(): string return self::CONTROLLER_NAME; } + public function getPluralResourceName(): string + { + return self::CONTROLLER_NAME.'s'; + } + /** * @param array $params * @return array diff --git a/src/Resources/Product.php b/src/Resources/Product.php index 46ea99c..36c00f8 100644 --- a/src/Resources/Product.php +++ b/src/Resources/Product.php @@ -10,4 +10,9 @@ public function getResourceName(): string { return self::CONTROLLER_NAME; } + + public function getPluralResourceName(): string + { + return self::CONTROLLER_NAME.'s'; + } } diff --git a/src/Resources/Resource.php b/src/Resources/Resource.php index 13a8f00..e19a93c 100644 --- a/src/Resources/Resource.php +++ b/src/Resources/Resource.php @@ -2,6 +2,7 @@ namespace Vormkracht10\WeFact\Resources; +use Exception; use GuzzleHttp\Client; use GuzzleHttp\Exception\BadResponseException; use GuzzleHttp\Exception\ClientException; @@ -40,6 +41,64 @@ public function list(array $params = []): array ); } + /** + * @return array + * + * @throws ClientException|ServerException|BadResponseException|JsonException + */ + public function listAll(int $offset = 0, int $perPage = 1000): array + { + // Rate limit the requests to prevent IP blocking. + $limitPerSecond = 300 / 60; // Per minute / seconds + $calls = 1; + + $data = []; + + $pluralResourceName = $this->getPluralResourceName(); + + try { + $result = $this->list(params: [ + 'limit' => $perPage, + 'offset' => $offset, + ] + ); + } catch (Exception $e) { + throw $e; + } + + foreach ($result[$pluralResourceName] as $index => $item) { + $calls++; + + if ($calls % $limitPerSecond == 0) { + sleep(1); + } + + try { + $resultItem = $this->show([ + 'Identifier' => $item['Identifier'], + ]); + } catch (Exception $e) { + throw $e; + } + + if (is_array($resultItem) && isset($resultItem[$this->getResourceName()])) { + $result[$pluralResourceName][$index] = $resultItem[$this->getResourceName()]; + } else { + // Handle the case where $resultItem is not an array or does not contain the expected resource name. + continue; + } + } + + $data = array_merge($data, $result[$pluralResourceName] ?? []); + + if ($result['currentresults'] >= $perPage) { + $offset += $perPage; + $data = array_merge($data, $this->listAll($offset, $perPage)); + } + + return $data; + } + /** * @param array $params * @return array|MethodNotAvailableException|ClientException|ServerException|BadResponseException|JsonException @@ -109,4 +168,6 @@ public function delete(array $params = []): array|MethodNotAvailableException|Cl } abstract public function getResourceName(): string; + + abstract public function getPluralResourceName(): string; } diff --git a/src/Resources/Settings/CostCategory.php b/src/Resources/Settings/CostCategory.php index 3fafb51..c76db16 100644 --- a/src/Resources/Settings/CostCategory.php +++ b/src/Resources/Settings/CostCategory.php @@ -15,13 +15,18 @@ class CostCategory extends Resource { use Request; - final public const CONTROLLER_NAME = 'settings'; + final public const CONTROLLER_NAME = 'costcategory'; public function getResourceName(): string { return self::CONTROLLER_NAME; } + public function getPluralResourceName(): string + { + return 'costcategories'; + } + /** * @param array $params * @return array diff --git a/src/Resources/Settings/Settings.php b/src/Resources/Settings/Settings.php index caa8dca..54245a8 100644 --- a/src/Resources/Settings/Settings.php +++ b/src/Resources/Settings/Settings.php @@ -18,6 +18,11 @@ public function getResourceName(): string return self::CONTROLLER_NAME; } + public function getPluralResourceName(): string + { + return self::CONTROLLER_NAME; + } + /** * @return array */ diff --git a/src/Resources/Subscription.php b/src/Resources/Subscription.php index 1d0da8c..29d62a5 100644 --- a/src/Resources/Subscription.php +++ b/src/Resources/Subscription.php @@ -18,6 +18,11 @@ public function getResourceName(): string return self::CONTROLLER_NAME; } + public function getPluralResourceName(): string + { + return self::CONTROLLER_NAME.'s'; + } + /** * @return array *