Passed
Push — master ( 5197c9...313b01 )
by Sébastien
04:05 queued 15s
created

RepositoryPaginatorFactory::createWalker()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
dl 0
loc 12
ccs 6
cts 6
cp 1
rs 10
c 1
b 0
f 0
cc 3
nc 2
nop 3
crap 3
1
<?php
2
3
namespace Bdf\Prime\Repository;
4
5
use Bdf\Prime\Query\Pagination\PaginatorFactory;
6
use Bdf\Prime\Query\Pagination\Walker;
7
use Bdf\Prime\Query\Pagination\WalkStrategy\KeyWalkStrategy;
8
use Bdf\Prime\Query\Pagination\WalkStrategy\MapperPrimaryKey;
9
use Bdf\Prime\Query\ReadCommandInterface;
10
11
/**
12
 * Paginator factory for a repository query
13
 */
14
class RepositoryPaginatorFactory extends PaginatorFactory
15
{
16
    /**
17
     * @var RepositoryInterface
18
     */
19
    private $repository;
20
21
    /**
22
     * RepositoryPaginatorFactory constructor.
23
     *
24
     * @param RepositoryInterface $repository
25
     */
26 115
    public function __construct(RepositoryInterface $repository)
27
    {
28 115
        $this->repository = $repository;
29
30 115
        $this->addFactory(Walker::class, [$this, 'createWalker']);
31 115
    }
32
33
    /**
34
     * Create the walker instance for the given query
35
     *
36
     * @param ReadCommandInterface $query
37
     * @param int|null $limit
0 ignored issues
show
Coding Style introduced by
Expected "integer|null" but found "int|null" for parameter type
Loading history...
38
     * @param int|null $page
0 ignored issues
show
Coding Style introduced by
Expected "integer|null" but found "int|null" for parameter type
Loading history...
39
     *
40
     * @return Walker
41
     */
42 9
    protected function createWalker(ReadCommandInterface $query, ?int $limit, ?int $page): Walker
43
    {
44 9
        $walker = new Walker($query, $limit, $page);
45
46
        if (
0 ignored issues
show
Coding Style introduced by
Expected 0 spaces after opening bracket; newline found
Loading history...
Coding Style introduced by
First condition of a multi-line IF statement must directly follow the opening parenthesis
Loading history...
47 9
            !$this->repository->metadata()->isCompositePrimaryKey()
0 ignored issues
show
Coding Style introduced by
Each line in a multi-line IF statement must begin with a boolean operator
Loading history...
48 9
            && KeyWalkStrategy::supports($query, $page, $this->repository->metadata()->primary['attributes'][0])
49
        ) {
50 7
            $walker->setStrategy(new KeyWalkStrategy(new MapperPrimaryKey($this->repository->mapper())));
51
        }
52
53 9
        return $walker;
54
    }
55
}
56