YieldsItemsByCursor::yieldItemsByCursor()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 8
nc 1
nop 1
dl 0
loc 14
ccs 9
cts 9
cp 1
crap 2
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cerbero\LazyJsonPages\Concerns;
6
7
use Closure;
8
use Generator;
9
use Psr\Http\Message\ResponseInterface;
10
11
/**
12
 * The trait to yield items from cursor-aware paginations.
13
 */
14
trait YieldsItemsByCursor
15
{
16
    use RespectsRateLimits;
17
    use RetriesHttpRequests;
18
19
    /**
20
     * Yield the paginated items by following the cursor of each page.
21
     *
22
     * @param Closure(ResponseInterface): Generator<int, mixed> $callback
23
     * @return Generator<int, mixed>
24
     */
25 3
    protected function yieldItemsByCursor(Closure $callback): Generator
26
    {
27 3
        $request = clone $this->source->request();
28
29 2
        yield from $generator = $callback($this->source->pullResponse());
30
31 2
        yield from $this->retry(function () use ($callback, $request, $generator) {
32 2
            while ($cursor = $this->toPage($generator->getReturn(), onlyNumerics: false)) {
0 ignored issues
show
Bug introduced by
It seems like toPage() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

32
            while ($cursor = $this->/** @scrutinizer ignore-call */ toPage($generator->getReturn(), onlyNumerics: false)) {
Loading history...
33 2
                $this->respectRateLimits();
34
35 2
                $uri = $this->uriForPage($request->getUri(), (string) $cursor);
0 ignored issues
show
Bug introduced by
It seems like uriForPage() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

35
                /** @scrutinizer ignore-call */ 
36
                $uri = $this->uriForPage($request->getUri(), (string) $cursor);
Loading history...
36 2
                $response = $this->client->send($request->withUri($uri));
37
38 2
                yield from $generator = $callback($response);
39
            }
40 2
        });
41
    }
42
}
43