Paginator   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
eloc 18
dl 0
loc 55
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A addMeta() 0 3 1
A __construct() 0 4 1
A getResults() 0 3 1
A getMeta() 0 7 1
A resolveMeta() 0 10 2
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