Skip to content

Commit c695555

Browse files
committed
Merge branch 'cleanup'
2 parents c990c24 + 9eb0f8c commit c695555

File tree

14 files changed

+131
-118
lines changed

14 files changed

+131
-118
lines changed

src/Toolkit/Contract/Curler/CurlerInterface.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,12 @@ public function withoutMiddleware($middleware);
463463
*/
464464
public function getPager(): ?CurlerPagerInterface;
465465

466+
/**
467+
* Check if the endpoint's pagination handler is used to process every
468+
* request
469+
*/
470+
public function alwaysPaginates(): bool;
471+
466472
/**
467473
* Get an instance with the given pagination handler
468474
*

src/Toolkit/Contract/Curler/CurlerPageInterface.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,34 @@
22

33
namespace Salient\Contract\Curler;
44

5+
use Psr\Http\Message\RequestInterface;
6+
use OutOfRangeException;
7+
58
/**
69
* @api
710
*/
8-
interface CurlerPageInterface extends CurlerPageRequestInterface
11+
interface CurlerPageInterface
912
{
1013
/**
1114
* Get a list of entities returned by the endpoint
1215
*
1316
* @return list<mixed>
1417
*/
1518
public function getEntities(): array;
19+
20+
/**
21+
* Check if more data can be requested from the endpoint
22+
*/
23+
public function hasNextRequest(): bool;
24+
25+
/**
26+
* Get a request to retrieve the next page of data from the endpoint
27+
*
28+
* Return a {@see CurlerPageRequestInterface} to propagate query changes to
29+
* the next {@see CurlerPagerInterface::getPage()} call in array form.
30+
*
31+
* @return CurlerPageRequestInterface|RequestInterface
32+
* @throws OutOfRangeException if no more data can be requested.
33+
*/
34+
public function getNextRequest();
1635
}

src/Toolkit/Contract/Curler/CurlerPageRequestInterface.php

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,21 @@
33
namespace Salient\Contract\Curler;
44

55
use Psr\Http\Message\RequestInterface;
6-
use OutOfRangeException;
76

87
/**
98
* @api
109
*/
1110
interface CurlerPageRequestInterface
1211
{
1312
/**
14-
* Check if a page of data can be retrieved from the endpoint
13+
* Get a request to retrieve a page of data from the endpoint
1514
*/
16-
public function hasNextRequest(): bool;
15+
public function getRequest(): RequestInterface;
1716

1817
/**
19-
* Get a request to retrieve the next page of data from the endpoint
20-
*
21-
* @throws OutOfRangeException if there are no more pages to retrieve.
22-
*/
23-
public function getNextRequest(): RequestInterface;
24-
25-
/**
26-
* Get the query applied to the request to retrieve the next page of data
18+
* Get the query applied to the request to retrieve a page of data
2719
*
2820
* @return mixed[]|null
29-
* @throws OutOfRangeException if there are no more pages to retrieve.
3021
*/
31-
public function getNextQuery(): ?array;
22+
public function getQuery(): ?array;
3223
}

src/Toolkit/Contract/Curler/CurlerPagerInterface.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ interface CurlerPagerInterface
1515
*
1616
* Return `$request` if no special handling is required.
1717
*
18+
* Return a {@see CurlerPageRequestInterface} to propagate `$query` changes
19+
* to {@see CurlerPagerInterface::getPage()} in array form.
20+
*
1821
* @param mixed[]|null $query The query applied to `$request`.
1922
* @return CurlerPageRequestInterface|RequestInterface
2023
*/
@@ -27,9 +30,13 @@ public function getFirstRequest(
2730
/**
2831
* Convert data returned by the endpoint to a new page object
2932
*
33+
* @template TPage of CurlerPageInterface|null
34+
*
3035
* @param mixed $data
36+
* @param TPage $previousPage
3137
* @param mixed[]|null $query The query applied to `$request` or returned by
3238
* {@see CurlerPageRequestInterface::getNextQuery()}, if applicable.
39+
* @return (TPage is null ? CurlerPageInterface : TPage)
3340
*/
3441
public function getPage(
3542
$data,

src/Toolkit/Curler/Curler.php

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -376,11 +376,8 @@ private function process(string $method, ?array $query, $data = false)
376376
if ($pager) {
377377
$request = $pager->getFirstRequest($request, $this, $query);
378378
if ($request instanceof CurlerPageRequestInterface) {
379-
if (!$request->hasNextRequest()) {
380-
throw new LogicException('No first request');
381-
}
382-
$query = $request->getNextQuery() ?? $query;
383-
$request = $request->getNextRequest();
379+
$query = $request->getQuery() ?? $query;
380+
$request = $request->getRequest();
384381
}
385382
}
386383

@@ -455,15 +452,12 @@ private function paginate(string $method, ?array $query, $data = false): Generat
455452

456453
$request = $this->createRequest($method, $query, $data);
457454
$request = $this->Pager->getFirstRequest($request, $this, $query);
458-
if ($request instanceof CurlerPageRequestInterface) {
459-
if (!$request->hasNextRequest()) {
460-
throw new LogicException('No first request');
461-
}
462-
$query = $request->getNextQuery() ?? $query;
463-
$request = $request->getNextRequest();
464-
}
465455
$prev = null;
466456
do {
457+
if ($request instanceof CurlerPageRequestInterface) {
458+
$query = $request->getQuery() ?? $query;
459+
$request = $request->getRequest();
460+
}
467461
$response = $this->doSendRequest($request);
468462
$page = $this->Pager->getPage(
469463
$this->getResponseBody($response),
@@ -481,7 +475,6 @@ private function paginate(string $method, ?array $query, $data = false): Generat
481475
if (!$page->hasNextRequest()) {
482476
break;
483477
}
484-
$query = $page->getNextQuery() ?? $query;
485478
$request = $page->getNextRequest();
486479
$prev = $page;
487480
} while (true);
@@ -655,6 +648,14 @@ public function getPager(): ?CurlerPagerInterface
655648
return $this->Pager;
656649
}
657650

651+
/**
652+
* @inheritDoc
653+
*/
654+
public function alwaysPaginates(): bool
655+
{
656+
return $this->AlwaysPaginate;
657+
}
658+
658659
/**
659660
* @inheritDoc
660661
*/
@@ -934,10 +935,8 @@ public function withoutMiddleware($middleware)
934935
/**
935936
* @inheritDoc
936937
*/
937-
public function withPager(
938-
?CurlerPagerInterface $pager,
939-
bool $alwaysPaginate = false
940-
) {
938+
public function withPager(?CurlerPagerInterface $pager, bool $alwaysPaginate = false)
939+
{
941940
return $this
942941
->with('Pager', $pager)
943942
->with('AlwaysPaginate', $pager && $alwaysPaginate);

src/Toolkit/Curler/CurlerPage.php

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -57,29 +57,18 @@ public function getEntities(): array
5757
*/
5858
public function hasNextRequest(): bool
5959
{
60-
return $this->NextRequest !== null;
60+
return (bool) $this->NextRequest;
6161
}
6262

6363
/**
6464
* @inheritDoc
6565
*/
66-
public function getNextRequest(): RequestInterface
66+
public function getNextRequest()
6767
{
68-
if ($this->NextRequest === null) {
68+
if (!$this->NextRequest) {
6969
throw new OutOfRangeException('No more pages');
7070
}
71-
return $this->NextRequest->getNextRequest();
72-
}
73-
74-
/**
75-
* @inheritDoc
76-
*/
77-
public function getNextQuery(): ?array
78-
{
79-
if ($this->NextRequest === null) {
80-
throw new OutOfRangeException('No more pages');
81-
}
82-
return $this->NextRequest->getNextQuery();
71+
return $this->NextRequest;
8372
}
8473

8574
/**

src/Toolkit/Curler/CurlerPageRequest.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,15 @@ public function __construct(
3232
/**
3333
* @inheritDoc
3434
*/
35-
public function hasNextRequest(): bool
36-
{
37-
return true;
38-
}
39-
40-
/**
41-
* @inheritDoc
42-
*/
43-
public function getNextRequest(): RequestInterface
35+
public function getRequest(): RequestInterface
4436
{
4537
return $this->NextRequest;
4638
}
4739

4840
/**
4941
* @inheritDoc
5042
*/
51-
public function getNextQuery(): ?array
43+
public function getQuery(): ?array
5244
{
5345
return $this->NextQuery;
5446
}

src/Toolkit/Curler/Pager/LinkPager.php

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@
99
use Salient\Contract\Http\HttpHeader;
1010
use Salient\Contract\Http\HttpResponseInterface;
1111
use Salient\Curler\CurlerPage;
12+
use Salient\Curler\CurlerPageRequest;
1213
use Salient\Http\Http;
1314
use Salient\Http\Uri;
1415
use Closure;
1516

1617
/**
17-
* Follows "Link" headers in responses from the endpoint
18+
* Follows "Link" headers with rel="next" in responses from the endpoint
1819
*
1920
* @api
2021
*/
@@ -54,16 +55,16 @@ public function getFirstRequest(
5455
RequestInterface $request,
5556
CurlerInterface $curler,
5657
?array $query = null
57-
): RequestInterface {
58+
) {
5859
if ($this->PageSize === null) {
5960
return $request;
6061
}
6162

6263
$query[$this->PageSizeKey] = $this->PageSize;
63-
$uri = $request->getUri();
64-
$uri = $curler->replaceQuery($uri, $query);
65-
66-
return $request->withUri($uri);
64+
return new CurlerPageRequest(
65+
$curler->replaceQuery($request, $query),
66+
$query,
67+
);
6768
}
6869

6970
/**
@@ -79,18 +80,15 @@ public function getPage(
7980
): CurlerPageInterface {
8081
$data = ($this->EntitySelector)($data);
8182

82-
$response = $curler->getLastResponse();
83-
if ($response && ($links = $response->getHeaderValues(HttpHeader::LINK))) {
84-
foreach ($links as $link) {
85-
/** @var array{string,rel?:string} */
86-
$link = Http::getParameters($link);
87-
if (($link['rel'] ?? null) === 'next') {
88-
$link = trim($link[0], '<>');
89-
$uri = $request->getUri();
90-
$uri = Uri::from($uri)->follow($link);
91-
$nextRequest = $request->withUri($uri);
92-
break;
93-
}
83+
foreach ($response->getHeaderValues(HttpHeader::LINK) as $link) {
84+
/** @var array{string,rel?:string} */
85+
$link = Http::getParameters($link);
86+
if (($link['rel'] ?? null) === 'next') {
87+
$link = trim($link[0], '<>');
88+
$uri = $request->getUri();
89+
$uri = Uri::from($uri)->follow($link);
90+
$nextRequest = $request->withUri($uri);
91+
break;
9492
}
9593
}
9694

src/Toolkit/Curler/Pager/ODataPager.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,7 @@ public function getPage(
7373
throw new InvalidArgumentTypeException(1, 'data', 'mixed[]', $data);
7474
}
7575
/** @var array{'@odata.nextLink'?:string,'@nextLink'?:string,value:list<mixed>,...} $data */
76-
$response = $curler->getLastResponse();
77-
if ($response && $response->getHeaderLine(HttpHeader::ODATA_VERSION) === '4.0') {
76+
if ($response->getHeaderLine(HttpHeader::ODATA_VERSION) === '4.0') {
7877
$nextLink = $data['@odata.nextLink'] ?? null;
7978
} else {
8079
$nextLink = $data['@nextLink'] ?? $data['@odata.nextLink'] ?? null;

0 commit comments

Comments
 (0)