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

PaginatorFactory::instance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2
1
<?php
2
3
namespace Bdf\Prime\Query\Pagination;
4
5
use Bdf\Prime\Query\ReadCommandInterface;
6
7
/**
8
 * Factory for paginators classes
9
 */
10
class PaginatorFactory
11
{
12
    /**
13
     * @var PaginatorFactory
14
     */
15
    private static $instance;
16
17
    /**
18
     * Mapping for paginator classes
19
     *
20
     * @var string[]
21
     */
22
    private $paginatorAliases = [
23
        'walker'    => Walker::class,
24
        'paginator' => Paginator::class,
25
    ];
26
27
    /**
28
     * The paginator factory
29
     * Takes the class name as key and the factory function as value
0 ignored issues
show
introduced by
Doc comment short description must be on a single line, further text should be a separate paragraph
Loading history...
30
     *
31
     * @var callable[]
32
     */
33
    private $paginatorFactories = [];
34
35
    /**
36
     * Register an alias for a paginator class
37
     *
38
     * @param string $className The paginator class name
39
     * @param string $alias The alias
40
     */
41 1
    public function addAlias(string $className, string $alias): void
42
    {
43 1
        $this->paginatorAliases[$alias] = $className;
44 1
    }
45
46
    /**
47
     * Register a factory for a paginator class
48
     *
49
     * @param string $className The paginator class name
50
     * @param callable $factory The factory function
51
     */
52 116
    public function addFactory(string $className, callable $factory): void
53
    {
54 116
        $this->paginatorFactories[$className] = $factory;
55 116
    }
56
57
    /**
58
     * Create the paginator instance
59
     *
60
     * @param ReadCommandInterface $query The query to paginate
61
     * @param string $class The paginator class name
62
     * @param int|null $maxRows Number of entries by page
0 ignored issues
show
Coding Style introduced by
Expected "integer|null" but found "int|null" for parameter type
Loading history...
63
     * @param int|null $page The current page
0 ignored issues
show
Coding Style introduced by
Expected "integer|null" but found "int|null" for parameter type
Loading history...
64
     *
65
     * @return PaginatorInterface
66
     */
67 38
    public function create(ReadCommandInterface $query, string $class = 'paginator', ?int $maxRows = null, ?int $page = null): PaginatorInterface
68
    {
69 38
        if (isset($this->paginatorAliases[$class])) {
70 36
            $class = $this->paginatorAliases[$class];
71
        }
72
73 38
        if (!isset($this->paginatorFactories[$class])) {
74 28
            return new $class($query, $maxRows, $page);
75
        }
76
77 10
        return ($this->paginatorFactories[$class])($query, $maxRows, $page);
78
    }
79
80
    /**
81
     * Get the paginator instance
82
     *
83
     * @return $this
84
     */
85 9
    public static function instance(): self
86
    {
87 9
        if (self::$instance) {
88 9
            return self::$instance;
89
        }
90
91 1
        return self::$instance = new self;
0 ignored issues
show
introduced by
Calling class constructors must always include parentheses
Loading history...
Coding Style introduced by
Parentheses must be used when instantiating a new class
Loading history...
Coding Style introduced by
Assignments must be the first block of code on a line
Loading history...
92
    }
93
}
94