Skip to content

Commit

Permalink
Merge pull request #96 from findologic/high_memory_issue
Browse files Browse the repository at this point in the history
Added generator for requests
simonhintersonnleitner authored Dec 4, 2024
2 parents f5f12b7 + 43b0994 commit 5b1b7e9
Showing 3 changed files with 7 additions and 7 deletions.
Empty file modified bin/console
100755 → 100644
Empty file.
9 changes: 3 additions & 6 deletions src/Utils.php
Original file line number Diff line number Diff line change
@@ -29,7 +29,7 @@ final class Utils
* @param Client $client
* @param Request $request
*
* @return ResponseInterface[]
* @return \Generator
* @throws EmptyResponseException
* @throws PermissionException
* @throws CustomerException
@@ -39,7 +39,7 @@ final class Utils
* @throws CriticalException
*
*/
public static function sendIterableRequest(Client $client, Request $request, Config $config): array
public static function sendIterableRequest(Client $client, Request $request, Config $config): \Generator
{
if (!$request instanceof IterableRequestInterface) {
throw new InvalidArgumentException(sprintf(
@@ -48,7 +48,6 @@ public static function sendIterableRequest(Client $client, Request $request, Con
));
}

$responses = [];
$lastPage = false;
$request->setItemsPerPage($config->getItemsPerPage());

@@ -57,10 +56,8 @@ public static function sendIterableRequest(Client $client, Request $request, Con
$lastPage = self::parseIsLastPage($response);
$request->setPage($request->getPage() + 1);

$responses[] = $response;
yield $response;
}

return $responses;
}

public static function validateAndGetShopkey(?string $shopkey): ?string
5 changes: 4 additions & 1 deletion tests/UtilsTest.php
Original file line number Diff line number Diff line change
@@ -12,6 +12,7 @@
use FINDOLOGIC\PlentyMarketsRestExporter\Tests\Helper\DirectoryAware;
use FINDOLOGIC\PlentyMarketsRestExporter\Tests\Helper\ResponseHelper;
use FINDOLOGIC\PlentyMarketsRestExporter\Utils;
use Psr\Http\Message\ResponseInterface;
use GuzzleHttp\Client;
use InvalidArgumentException;
use PHPUnit\Framework\MockObject\MockObject;
@@ -260,6 +261,8 @@ public function testSendIterableRequestFailsForNonIterableRequests(): void
->disableOriginalConstructor()
->getMock();

Utils::sendIterableRequest($client, $nonIterableRequest, $config);
foreach (Utils::sendIterableRequest($client, $nonIterableRequest, $config) as $response) {
$this->assertInstanceOf(ResponseInterface::class, $response);
};
}
}

0 comments on commit 5b1b7e9

Please sign in to comment.