Passed
Pull Request — main (#19)
by
unknown
08:15 queued 06:23
created

PaginatingIterator::fetchData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
/**
4
 * For the full copyright and license information, please view
5
 * the LICENSE file that was distributed with this source code.
6
 */
7
8
declare(strict_types=1);
9
10
namespace loophp\iterators\Pagination;
11
12
use Iterator;
13
use loophp\iterators\Contract\Pagination\PaginatedResultsInterface;
14
use loophp\iterators\Contract\Pagination\ResultsGeneratorInterface;
15
use function count;
16
17
/**
18
 * @template T
19
 * @template U
20
 *
21
 * @implements Iterator<int, list<T>>
22
 */
23
final class PaginatingIterator implements Iterator
24
{
25
    /**
26
     * @var ResultsGeneratorInterface<U>
27
     */
28
    private ResultsGeneratorInterface $fetch;
29
30
    private int $initialPage;
31
32
    private int $page;
33
34
    /**
35
     * @psalm-suppress PropertyNotSetInConstructor
36
     *
37
     * @var PaginatedResultsInterface<T>
38
     */
39
    private PaginatedResultsInterface $results;
40
41
    private bool $valid = true;
42
43
    /**
44
     * @param ResultsGeneratorInterface<U> $fetch
45
     */
46
    public function __construct(ResultsGeneratorInterface $fetch, int $page = 0)
47
    {
48
        $this->initialPage = $page;
49
        $this->page = $page;
50
        $this->fetch = $fetch;
51
    }
52
53
    /**
54
     * @return list<T>
0 ignored issues
show
Bug introduced by
The type loophp\iterators\Pagination\list was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
55
     */
56
    public function current(): array
57
    {
58
        return $this->results->getContent();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->results->getContent() returns the type array which is incompatible with the documented return type loophp\iterators\Pagination\list.
Loading history...
59
    }
60
61
    public function key(): int
62
    {
63
        return $this->page;
64
    }
65
66
    public function next(): void
67
    {
68
        ++$this->page;
69
70
        if (count($this->results->getContent()) === $this->results->getSize()) {
71
            $this->fetchData();
72
        } else {
73
            $this->valid = false;
74
        }
75
    }
76
77
    public function rewind(): void
78
    {
79
        $this->page = $this->initialPage;
80
    }
81
82
    public function valid(): bool
83
    {
84
        if ($this->page === $this->initialPage) {
85
            $this->fetchData();
86
        }
87
88
        return $this->valid && [] !== $this->results->getContent();
89
    }
90
91
    private function fetchData(): void
92
    {
93
        $this->results = ($this->fetch)($this->page);
94
    }
95
}
96