Skip to content

Commit

Permalink
Merge pull request #4 from vormkracht10/list-all-method
Browse files Browse the repository at this point in the history
List all method
  • Loading branch information
Baspa authored Aug 30, 2023
2 parents e5e3809 + 7ac897f commit a83a7cc
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/Resources/CreditInvoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,9 @@ public function getResourceName(): string
{
return self::CONTROLLER_NAME;
}

public function getPluralResourceName(): string
{
return self::CONTROLLER_NAME.'s';
}
}
5 changes: 5 additions & 0 deletions src/Resources/Creditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,9 @@ public function getResourceName(): string
{
return self::CONTROLLER_NAME;
}

public function getPluralResourceName(): string
{
return self::CONTROLLER_NAME.'s';
}
}
5 changes: 5 additions & 0 deletions src/Resources/Debtor.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ public function getResourceName(): string
return self::CONTROLLER_NAME;
}

public function getPluralResourceName(): string
{
return self::CONTROLLER_NAME.'s';
}

/**
* @return array<mixed, string>
*/
Expand Down
5 changes: 5 additions & 0 deletions src/Resources/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,9 @@ public function getResourceName(): string
{
return self::CONTROLLER_NAME;
}

public function getPluralResourceName(): string
{
return self::CONTROLLER_NAME.'s';
}
}
5 changes: 5 additions & 0 deletions src/Resources/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ public function getResourceName(): string
return self::CONTROLLER_NAME;
}

public function getPluralResourceName(): string
{
return self::CONTROLLER_NAME.'s';
}

/**
* @param array<string, mixed> $params
* @return array<string, mixed>
Expand Down
5 changes: 5 additions & 0 deletions src/Resources/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,9 @@ public function getResourceName(): string
{
return self::CONTROLLER_NAME;
}

public function getPluralResourceName(): string
{
return self::CONTROLLER_NAME.'s';
}
}
61 changes: 61 additions & 0 deletions src/Resources/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Vormkracht10\WeFact\Resources;

use Exception;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\BadResponseException;
use GuzzleHttp\Exception\ClientException;
Expand Down Expand Up @@ -40,6 +41,64 @@ public function list(array $params = []): array
);
}

/**
* @return array<string, mixed>
*
* @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<string, mixed> $params
* @return array<string, mixed>|MethodNotAvailableException|ClientException|ServerException|BadResponseException|JsonException
Expand Down Expand Up @@ -109,4 +168,6 @@ public function delete(array $params = []): array|MethodNotAvailableException|Cl
}

abstract public function getResourceName(): string;

abstract public function getPluralResourceName(): string;
}
7 changes: 6 additions & 1 deletion src/Resources/Settings/CostCategory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, mixed> $params
* @return array<string, mixed>
Expand Down
5 changes: 5 additions & 0 deletions src/Resources/Settings/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ public function getResourceName(): string
return self::CONTROLLER_NAME;
}

public function getPluralResourceName(): string
{
return self::CONTROLLER_NAME;
}

/**
* @return array<mixed, string>
*/
Expand Down
5 changes: 5 additions & 0 deletions src/Resources/Subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ public function getResourceName(): string
return self::CONTROLLER_NAME;
}

public function getPluralResourceName(): string
{
return self::CONTROLLER_NAME.'s';
}

/**
* @return array<string, mixed>
*
Expand Down

0 comments on commit a83a7cc

Please sign in to comment.