Passed
Pull Request — master (#361)
by Alejandro
05:54
created

ImplicitLoopPaginator::getIterator()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 11
rs 10
c 0
b 0
f 0
ccs 7
cts 7
cp 1
cc 3
nc 2
nop 0
crap 3
1
<?php
2
declare(strict_types=1);
3
4
namespace Shlinkio\Shlink\Common\Paginator;
5
6
use IteratorAggregate;
7
use Zend\Paginator\Paginator;
8
9
class ImplicitLoopPaginator implements IteratorAggregate
10
{
11
    /** @var Paginator */
12
    private $paginator;
13
    /** @var callable */
14
    private $valueParser;
15
16 21
    public function __construct(Paginator $paginator, callable $valueParser = null)
17
    {
18 21
        $this->paginator = $paginator;
19
        $this->valueParser = $valueParser ?? function ($value) {
20 20
            return $value;
21 20
        };
22
    }
23
24 21
    public function getIterator(): iterable
25
    {
26 21
        $totalPages = $this->paginator->count();
27 21
        $processedPages = 0;
28
29
        do {
30 21
            $processedPages++;
31 21
            $this->paginator->setCurrentPageNumber($processedPages);
32
33 21
            foreach ($this->paginator as $key => $value) {
34 21
                yield $key => ($this->valueParser)($value);
35
            }
36 21
        } while ($processedPages < $totalPages);
37
    }
38
}
39