Skip to content

Commit

Permalink
items per page from congiration
Browse files Browse the repository at this point in the history
  • Loading branch information
aserstobitov committed Nov 9, 2023
1 parent 0109205 commit e20545c
Show file tree
Hide file tree
Showing 18 changed files with 47 additions and 17 deletions.
5 changes: 5 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,11 @@ public function getAccessToken(): string
return $this->accessToken;
}

public function getConfig(): Config
{
return $this->config;
}

/**
* @codeCoverageIgnore Not yet implemented.
*/
Expand Down
14 changes: 14 additions & 0 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ class Config

private bool $exportFreeTextFields = true;

private int $itemsPerPage;

private ?float $exportReferrerId = null;

private string $exportDimensionUnit = 'mm';
Expand Down Expand Up @@ -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
]);
}
Expand All @@ -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')
]);
}
Expand Down Expand Up @@ -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)) {
Expand Down
3 changes: 2 additions & 1 deletion src/Exporter/Exporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/Request/AttributeRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
]
);
Expand Down
2 changes: 1 addition & 1 deletion src/Request/CategoryRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function __construct(int $storeIdentifier)
'with' => ['details'],
'plentyId' => $storeIdentifier,
'page' => $this->page,
'itemsPerPage' => self::$ITEMS_PER_PAGE
'itemsPerPage' => $this->itemsPerPage
]
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Request/ItemPropertyGroupRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function __construct(string $with)
[
'with' => $with,
'page' => $this->page,
'itemsPerPage' => self::$ITEMS_PER_PAGE
'itemsPerPage' => $this->itemsPerPage
]
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Request/ItemPropertyRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
]
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Request/ItemRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function __construct(
'variationRelatedUpdatedBetween' => $variationRelatedUpdatedBetween,
'or' => $or,
'page' => $this->page,
'itemsPerPage' => self::$ITEMS_PER_PAGE
'itemsPerPage' => $this->itemsPerPage
]
);
}
Expand Down
12 changes: 8 additions & 4 deletions src/Request/IterableRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down
4 changes: 4 additions & 0 deletions src/Request/IterableRequestInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
2 changes: 1 addition & 1 deletion src/Request/ManufacturerRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
]
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Request/PropertyGroupRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function __construct()
'v2/properties/groups',
[
'page' => $this->page,
'itemsPerPage' => self::$ITEMS_PER_PAGE,
'itemsPerPage' => $this->itemsPerPage,
'with' => 'names'
]
);
Expand Down
2 changes: 1 addition & 1 deletion src/Request/PropertyRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
]
);
Expand Down
2 changes: 1 addition & 1 deletion src/Request/PropertySelectionRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function __construct()
'properties/selections',
[
'page' => $this->page,
'itemsPerPage' => self::$ITEMS_PER_PAGE
'itemsPerPage' => $this->itemsPerPage
]
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Request/SalesPriceRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function __construct(?string $updatedAt = null)
[
'updatedAt' => $updatedAt,
'page' => $this->page,
'itemsPerPage' => self::$ITEMS_PER_PAGE
'itemsPerPage' => $this->itemsPerPage
]
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Request/UnitRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function __construct(?string $updatedAt = null)
[
'updatedAt' => $updatedAt,
'page' => $this->page,
'itemsPerPage' => self::$ITEMS_PER_PAGE
'itemsPerPage' => $this->itemsPerPage
]
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Request/VatRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function __construct()
'vat',
[
'page' => $this->page,
'itemsPerPage' => self::$ITEMS_PER_PAGE
'itemsPerPage' => $this->itemsPerPage
]
);
}
Expand Down
2 changes: 2 additions & 0 deletions src/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit e20545c

Please sign in to comment.