Skip to content

Commit 9eb0f8c

Browse files
committed
Sync: Improve support for Curler's "always paginate" option
- Add `HttpSyncProvider::getAlwaysPaginate()` method - Add `$alwaysPaginate` parameter to `HttpSyncProvider::getCurler()` - Add `HttpSyncDefinition::$AlwaysPaginate` property - Add `$alwaysPaginate` parameter to `HttpSyncDefinition::withPager()` for consistency with the equivalent `Curler` method - Fix incorrect documentation re: `HttpSyncProvider::getCurler()` arguments taking precedence over `HttpSyncProvider::filterCurler()`
1 parent e277a35 commit 9eb0f8c

File tree

4 files changed

+41
-10
lines changed

4 files changed

+41
-10
lines changed

src/Toolkit/Curler/Curler.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -935,10 +935,8 @@ public function withoutMiddleware($middleware)
935935
/**
936936
* @inheritDoc
937937
*/
938-
public function withPager(
939-
?CurlerPagerInterface $pager,
940-
bool $alwaysPaginate = false
941-
) {
938+
public function withPager(?CurlerPagerInterface $pager, bool $alwaysPaginate = false)
939+
{
942940
return $this
943941
->with('Pager', $pager)
944942
->with('AlwaysPaginate', $pager && $alwaysPaginate);

src/Toolkit/Sync/Http/HttpSyncDefinition.php

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
* @property-read mixed[]|null $Query Query parameters applied to the sync operation URL
6666
* @property-read HttpHeadersInterface|null $Headers HTTP headers applied to the sync operation request
6767
* @property-read CurlerPagerInterface|null $Pager Pagination handler for the endpoint servicing the entity
68+
* @property-read bool $AlwaysPaginate Use the pager to process requests even if no pagination is required
6869
* @property-read int<-1,max>|null $Expiry Seconds before cached responses expire
6970
* @property-read array<OP::*,HttpRequestMethod::*> $MethodMap Array that maps sync operations to HTTP request methods
7071
* @property-read (callable(CurlerInterface, HttpSyncDefinition<TEntity,TProvider>, OP::*, SyncContextInterface, mixed...): CurlerInterface)|null $CurlerCallback Callback applied to the Curler instance created to perform each sync operation
@@ -156,6 +157,11 @@ final class HttpSyncDefinition extends AbstractSyncDefinition implements Buildab
156157
*/
157158
protected ?CurlerPagerInterface $Pager;
158159

160+
/**
161+
* Use the pager to process requests even if no pagination is required
162+
*/
163+
protected bool $AlwaysPaginate;
164+
159165
/**
160166
* Seconds before cached responses expire
161167
*
@@ -251,6 +257,7 @@ public function __construct(
251257
?array $query = null,
252258
?HttpHeadersInterface $headers = null,
253259
?CurlerPagerInterface $pager = null,
260+
bool $alwaysPaginate = false,
254261
?callable $callback = null,
255262
$conformity = ListConformity::NONE,
256263
?int $filterPolicy = null,
@@ -286,6 +293,7 @@ public function __construct(
286293
$this->Query = $query;
287294
$this->Headers = $headers;
288295
$this->Pager = $pager;
296+
$this->AlwaysPaginate = $pager && $alwaysPaginate;
289297
$this->Callback = $callback;
290298
$this->Expiry = $expiry;
291299
$this->MethodMap = $methodMap;
@@ -331,11 +339,15 @@ public function withHeaders(?HttpHeadersInterface $headers)
331339
/**
332340
* Get an instance with the given pagination handler
333341
*
342+
* @param bool $alwaysPaginate If `true`, the pager is used to process
343+
* requests even if no pagination is required.
334344
* @return static
335345
*/
336-
public function withPager(?CurlerPagerInterface $pager)
346+
public function withPager(?CurlerPagerInterface $pager, bool $alwaysPaginate = false)
337347
{
338-
return $this->with('Pager', $pager);
348+
return $this
349+
->with('Pager', $pager)
350+
->with('AlwaysPaginate', $pager && $alwaysPaginate);
339351
}
340352

341353
/**
@@ -716,7 +728,13 @@ private function doRunHttpOperation($operation, SyncContextInterface $ctx, ...$a
716728
);
717729
}
718730

719-
$curler = $this->Provider->getCurler($path, $this->Expiry, $this->Headers, $this->Pager);
731+
$curler = $this->Provider->getCurler(
732+
$path,
733+
$this->Expiry,
734+
$this->Headers,
735+
$this->Pager,
736+
$this->AlwaysPaginate,
737+
);
720738

721739
if ($this->CurlerCallback) {
722740
$curler = ($this->CurlerCallback)($curler, $this, $operation, $ctx, ...$args);
@@ -867,6 +885,7 @@ public static function getReadableProperties(): array
867885
'Query',
868886
'Headers',
869887
'Pager',
888+
'AlwaysPaginate',
870889
'Expiry',
871890
'MethodMap',
872891
'CurlerCallback',

src/Toolkit/Sync/Http/HttpSyncDefinitionBuilder.php

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Toolkit/Sync/Http/HttpSyncProvider.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,21 @@ abstract class HttpSyncProvider extends AbstractSyncProvider
3030
* expire, or:
3131
* - `null`: do not cache responses
3232
* - `0`: cache responses indefinitely
33-
* - `-1` (default): use the value returned by {@see getExpiry()}
33+
* - `-1` (default): use value returned by {@see getExpiry()}
3434
*/
3535
final public function getCurler(
3636
string $path,
3737
?int $expiry = -1,
3838
?HttpHeadersInterface $headers = null,
3939
?CurlerPagerInterface $pager = null,
40+
bool $alwaysPaginate = false,
4041
?DateFormatterInterface $dateFormatter = null
4142
): CurlerInterface {
4243
$builder = Curler::build()
4344
->uri($this->getEndpointUrl($path))
4445
->headers($headers ?? $this->getHeaders($path))
4546
->pager($pager ?? $this->getPager($path))
47+
->alwaysPaginate($pager ? $alwaysPaginate : $this->getAlwaysPaginate($path))
4648
->dateFormatter($dateFormatter ?? $this->getDateFormatter());
4749

4850
if ($expiry === -1) {
@@ -100,6 +102,17 @@ protected function getPager(string $path): ?CurlerPagerInterface
100102
return null;
101103
}
102104

105+
/**
106+
* Override if the pager returned by getPager() should be used to process
107+
* requests even if no pagination is required
108+
*
109+
* @codeCoverageIgnore
110+
*/
111+
protected function getAlwaysPaginate(string $path): bool
112+
{
113+
return false;
114+
}
115+
103116
/**
104117
* Override to specify the number of seconds before cached responses from
105118
* the upstream API expire
@@ -118,8 +131,8 @@ protected function getExpiry(string $path): ?int
118131
* Override to customise Curler instances before they are used to perform
119132
* sync operations
120133
*
121-
* Values passed to {@see HttpSyncProvider::getCurler()} take precedence
122-
* over values applied by this method.
134+
* Values passed to {@see HttpSyncProvider::getCurler()} are applied before
135+
* this method is called.
123136
*
124137
* @codeCoverageIgnore
125138
*/

0 commit comments

Comments
 (0)