51 lines
1.6 KiB
PHP
51 lines
1.6 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace Esun\LingxingErp\Model\Store;
|
|
|
|
use Esun\LingxingErp\Model\Api\ApiException;
|
|
use Esun\LingxingErp\Model\Api\Client;
|
|
use Esun\LingxingErp\Model\Api\Endpoint;
|
|
|
|
class StoreFetcher
|
|
{
|
|
private const PAGE_SIZE = 200;
|
|
private const PLATFORM_CODE = 10009;
|
|
|
|
public function __construct(private readonly Client $client)
|
|
{
|
|
}
|
|
|
|
/** @return array<int, array<string, mixed>> */
|
|
public function fetchAll(): array
|
|
{
|
|
$stores = [];
|
|
$offset = 0;
|
|
do {
|
|
$response = $this->client->post(Endpoint::STORE_LIST, [
|
|
'offset' => $offset,
|
|
'length' => self::PAGE_SIZE,
|
|
'platform_code' => [self::PLATFORM_CODE],
|
|
]);
|
|
$list = is_array($response['data']['list'] ?? null) ? $response['data']['list'] : [];
|
|
foreach ($list as $store) {
|
|
if (!is_array($store)) {
|
|
continue;
|
|
}
|
|
$platformCode = (int)($store['platform_code'] ?? 0);
|
|
$storeId = trim((string)($store['store_id'] ?? ''));
|
|
if ($platformCode <= 0 || $storeId === '') {
|
|
throw new ApiException('StoreInfoV2 returned a store without platform_code or store_id.', 'invalid_store_data');
|
|
}
|
|
$stores[] = $store;
|
|
}
|
|
|
|
$received = count($list);
|
|
$offset += $received;
|
|
$total = max(0, (int)($response['data']['total'] ?? $offset));
|
|
} while ($received > 0 && $offset < $total);
|
|
|
|
return $stores;
|
|
}
|
|
}
|