diff --git a/src/Client.php b/src/Client.php index 7efaa02..ce2fd54 100644 --- a/src/Client.php +++ b/src/Client.php @@ -236,6 +236,11 @@ public function getAccessToken(): string return $this->accessToken; } + public function getConfig(): Config + { + return $this->config; + } + /** * @codeCoverageIgnore Not yet implemented. */ diff --git a/src/Config.php b/src/Config.php index 451867b..5b96aa1 100644 --- a/src/Config.php +++ b/src/Config.php @@ -47,6 +47,8 @@ class Config private bool $exportFreeTextFields = true; + private int $itemsPerPage; + private ?float $exportReferrerId = null; private string $exportDimensionUnit = 'mm'; @@ -97,6 +99,7 @@ public static function fromArray(array $data, bool $debug = false): self 'exportReferrerId' => self::getFloatCastExportReferrerId($plentyConfig['export_referrer_id'] ?? null), 'exportDimensionUnit' => $plentyConfig['export_dimension_unit'], 'exportWeightUnit' => $plentyConfig['export_weight_unit'], + 'itemsPerPage' => $plentyConfig['items_per_page'], 'debug' => $debug ]); } @@ -123,6 +126,7 @@ public static function fromEnvironment(): Config 'exportReferrerId' => self::getFloatCastExportReferrerId(Utils::env('EXPORT_REFERRER_ID')), 'exportDimensionUnit' => Utils::env('EXPORT_DIMENSION_UNIT'), 'exportWeightUnit' => Utils::env('EXPORT_WEIGHT_UNIT'), + 'itemsPerPage' => Utils::env('ITEMS_PER_PAGE'), 'debug' => (bool)Utils::env('DEBUG') ]); } @@ -334,6 +338,16 @@ public function setExportFreeTextFields(bool $exportFreeTextFields): void $this->exportFreeTextFields = $exportFreeTextFields; } + public function getItemsPerPage(): int + { + return $this->itemsPerPage; + } + + public function setItemsPerPage(int $itemsPerPage): void + { + $this->itemsPerPage = $itemsPerPage; + } + private static function getFloatCastExportReferrerId($exportReferrerId): ?float { if (is_numeric($exportReferrerId)) { diff --git a/src/Exporter/Exporter.php b/src/Exporter/Exporter.php index 1ddc633..ff630f3 100644 --- a/src/Exporter/Exporter.php +++ b/src/Exporter/Exporter.php @@ -217,7 +217,7 @@ protected function exportProducts(): void $products = $this->getItems($page); $variations = $products->getAllIds() ? $this->getItemVariations($products->getAllIds()) : []; $this->wrapData(count($products->all()), $products, $variations, $propertySelection); - $this->offset += ItemVariationRequest::$ITEMS_PER_PAGE; + $this->offset += $this->config->getItemsPerPage(); $page++; } while (!$products->isLastPage()); @@ -235,6 +235,7 @@ protected function exportProducts(): void private function getItems($page): ItemResponse { $this->itemRequest->setPage($page); + $this->itemRequest->setItemsPerPage($this->config->getItemsPerPage()); $response = $this->client->send($this->itemRequest); return ItemParser::parse($response); diff --git a/src/Request/AttributeRequest.php b/src/Request/AttributeRequest.php index 219e882..32b021c 100644 --- a/src/Request/AttributeRequest.php +++ b/src/Request/AttributeRequest.php @@ -15,7 +15,7 @@ public function __construct(?string $with = 'names') 'items/attributes', [ 'page' => $this->page, - 'itemsPerPage' => self::$ITEMS_PER_PAGE, + 'itemsPerPage' => $this->itemsPerPage, 'with' => $with ] ); diff --git a/src/Request/CategoryRequest.php b/src/Request/CategoryRequest.php index 46d830e..4367659 100644 --- a/src/Request/CategoryRequest.php +++ b/src/Request/CategoryRequest.php @@ -18,7 +18,7 @@ public function __construct(int $storeIdentifier) 'with' => ['details'], 'plentyId' => $storeIdentifier, 'page' => $this->page, - 'itemsPerPage' => self::$ITEMS_PER_PAGE + 'itemsPerPage' => $this->itemsPerPage ] ); } diff --git a/src/Request/ItemPropertyGroupRequest.php b/src/Request/ItemPropertyGroupRequest.php index e657ccd..5c7db90 100644 --- a/src/Request/ItemPropertyGroupRequest.php +++ b/src/Request/ItemPropertyGroupRequest.php @@ -16,7 +16,7 @@ public function __construct(string $with) [ 'with' => $with, 'page' => $this->page, - 'itemsPerPage' => self::$ITEMS_PER_PAGE + 'itemsPerPage' => $this->itemsPerPage ] ); } diff --git a/src/Request/ItemPropertyRequest.php b/src/Request/ItemPropertyRequest.php index 8b131d5..b059146 100644 --- a/src/Request/ItemPropertyRequest.php +++ b/src/Request/ItemPropertyRequest.php @@ -18,7 +18,7 @@ public function __construct(?array $with = null, ?string $updatedAt = null, ?str 'updatedAt' => $updatedAt, 'groupId' => $groupId, 'page' => $this->page, - 'itemsPerPage' => self::$ITEMS_PER_PAGE + 'itemsPerPage' => $this->itemsPerPage ] ); } diff --git a/src/Request/ItemRequest.php b/src/Request/ItemRequest.php index 13009cb..1fe4d31 100644 --- a/src/Request/ItemRequest.php +++ b/src/Request/ItemRequest.php @@ -37,7 +37,7 @@ public function __construct( 'variationRelatedUpdatedBetween' => $variationRelatedUpdatedBetween, 'or' => $or, 'page' => $this->page, - 'itemsPerPage' => self::$ITEMS_PER_PAGE + 'itemsPerPage' => $this->itemsPerPage ] ); } diff --git a/src/Request/IterableRequest.php b/src/Request/IterableRequest.php index 1c520be..3d3d8e0 100644 --- a/src/Request/IterableRequest.php +++ b/src/Request/IterableRequest.php @@ -6,15 +6,14 @@ trait IterableRequest { - /** Maximum count of entities per page. */ - public static int $ITEMS_PER_PAGE = 100; protected int $page = 1; + protected int $itemsPerPage = 100; public function getParams(): array { $params = parent::getParams(); $params['page'] = $this->page; - $params['itemsPerPage'] = static::$ITEMS_PER_PAGE; + $params['itemsPerPage'] = $this->itemsPerPage; return $params; } @@ -31,9 +30,14 @@ public function getPage(): int return $this->page; } + public function getItemsPerPage(): int + { + return $this->itemsPerPage; + } + public function setItemsPerPage(int $itemsPerPage): self { - static::$ITEMS_PER_PAGE = $itemsPerPage; + $this->itemsPerPage = $itemsPerPage; return $this; } diff --git a/src/Request/IterableRequestInterface.php b/src/Request/IterableRequestInterface.php index 2b99787..36dc51f 100644 --- a/src/Request/IterableRequestInterface.php +++ b/src/Request/IterableRequestInterface.php @@ -11,4 +11,8 @@ public function getParams(): array; public function setPage(int $page); public function getPage(): int; + + public function setItemsPerPage(int $itemsPerPage); + + public function getItemsPerPage(): int; } diff --git a/src/Request/ManufacturerRequest.php b/src/Request/ManufacturerRequest.php index 38b462d..d98c1cf 100644 --- a/src/Request/ManufacturerRequest.php +++ b/src/Request/ManufacturerRequest.php @@ -18,7 +18,7 @@ public function __construct(?string $with = null, ?string $updatedAt = null, ?st 'updatedAt' => $updatedAt, 'name' => $name, 'page' => $this->page, - 'itemsPerPage' => self::$ITEMS_PER_PAGE + 'itemsPerPage' => $this->itemsPerPage ] ); } diff --git a/src/Request/PropertyGroupRequest.php b/src/Request/PropertyGroupRequest.php index 8d71ffc..e740aa5 100644 --- a/src/Request/PropertyGroupRequest.php +++ b/src/Request/PropertyGroupRequest.php @@ -15,7 +15,7 @@ public function __construct() 'v2/properties/groups', [ 'page' => $this->page, - 'itemsPerPage' => self::$ITEMS_PER_PAGE, + 'itemsPerPage' => $this->itemsPerPage, 'with' => 'names' ] ); diff --git a/src/Request/PropertyRequest.php b/src/Request/PropertyRequest.php index 493d6c2..eb9dfe9 100644 --- a/src/Request/PropertyRequest.php +++ b/src/Request/PropertyRequest.php @@ -15,7 +15,7 @@ public function __construct() 'v2/properties', [ 'page' => $this->page, - 'itemsPerPage' => self::$ITEMS_PER_PAGE, + 'itemsPerPage' => $this->itemsPerPage, 'with' => 'names,amazon,options,groups' ] ); diff --git a/src/Request/PropertySelectionRequest.php b/src/Request/PropertySelectionRequest.php index a036f4d..5720fec 100644 --- a/src/Request/PropertySelectionRequest.php +++ b/src/Request/PropertySelectionRequest.php @@ -15,7 +15,7 @@ public function __construct() 'properties/selections', [ 'page' => $this->page, - 'itemsPerPage' => self::$ITEMS_PER_PAGE + 'itemsPerPage' => $this->itemsPerPage ] ); } diff --git a/src/Request/SalesPriceRequest.php b/src/Request/SalesPriceRequest.php index ab11c5b..3aad301 100644 --- a/src/Request/SalesPriceRequest.php +++ b/src/Request/SalesPriceRequest.php @@ -16,7 +16,7 @@ public function __construct(?string $updatedAt = null) [ 'updatedAt' => $updatedAt, 'page' => $this->page, - 'itemsPerPage' => self::$ITEMS_PER_PAGE + 'itemsPerPage' => $this->itemsPerPage ] ); } diff --git a/src/Request/UnitRequest.php b/src/Request/UnitRequest.php index 1f1e84d..416aee4 100644 --- a/src/Request/UnitRequest.php +++ b/src/Request/UnitRequest.php @@ -16,7 +16,7 @@ public function __construct(?string $updatedAt = null) [ 'updatedAt' => $updatedAt, 'page' => $this->page, - 'itemsPerPage' => self::$ITEMS_PER_PAGE + 'itemsPerPage' => $this->itemsPerPage ] ); } diff --git a/src/Request/VatRequest.php b/src/Request/VatRequest.php index a11d778..2fbbe0c 100644 --- a/src/Request/VatRequest.php +++ b/src/Request/VatRequest.php @@ -15,7 +15,7 @@ public function __construct() 'vat', [ 'page' => $this->page, - 'itemsPerPage' => self::$ITEMS_PER_PAGE + 'itemsPerPage' => $this->itemsPerPage ] ); } diff --git a/src/Utils.php b/src/Utils.php index b857138..6adcb1a 100644 --- a/src/Utils.php +++ b/src/Utils.php @@ -49,6 +49,8 @@ public static function sendIterableRequest(Client $client, Request $request): ar $responses = []; $lastPage = false; + $request->setItemsPerPage($client->getConfig()->getItemsPerPage()); + while (!$lastPage) { $response = $client->send($request); $lastPage = self::parseIsLastPage($response);