Passed
Push — master ( 82a58b...d35cf8 )
by Tomasz
02:32
created

Paginator::getResults()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace ReadModel;
5
6
abstract class Paginator
7
{
8
    /** @var int */
9
    protected $limit;
10
11
    /** @var int */
12
    protected $offset;
13
14
    /** @var array */
15
    protected $meta = [];
16
17 6
    public function __construct(?int $limit, int $offset)
18
    {
19 6
        $this->limit = $limit;
20 6
        $this->offset = $offset;
21 6
    }
22
23
    /**
24
     * @param string $name
25
     * @param mixed  $value also a callable which will be resolved when meta data would be needed; see resolveMeta()
26
     */
27 3
    public function addMeta(string $name, $value): void
28
    {
29 3
        $this->meta[$name] = $value;
30 3
    }
31
32 2
    public function getResults(): array
33
    {
34 2
        return $this->findAll();
35
    }
36
37 4
    public function getMeta(): array
38
    {
39 4
        return array_merge([
40 4
            'limit' => $this->limit,
41 4
            'offset' => $this->offset,
42 4
            'total' => $this->getTotal()
43 4
        ], $this->resolveMeta());
44
    }
45
46
    private function resolveMeta(): array
47
    {
48 4
        return array_map(function ($value) {
49
            // execute callables
50 3
            if (is_callable($value)) {
51 2
                $value = call_user_func($value);
52
            }
53
54 3
            return $value;
55 4
        }, $this->meta);
56
    }
57
58
    abstract protected function findAll(): array;
59
60
    abstract protected function getTotal(): int;
61
}
62