Completed
Branch v4 (4e54dd)
by Pieter
03:26
created

MockAdapter::getNbResults()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 1
1
<?php
2
3
namespace W2w\Lib\Apie\Plugins\Mock\Pagers;
4
5
use Pagerfanta\Adapter\AdapterInterface;
6
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
7
use W2w\Lib\Apie\Core\SearchFilters\SearchFilterHelper;
8
use W2w\Lib\Apie\Core\SearchFilters\SearchFilterRequest;
9
use W2w\Lib\Apie\Plugins\Mock\DataLayers\MockApiResourceDataLayer;
10
11
class MockAdapter implements AdapterInterface
12
{
13
    /**
14
     * @var MockApiResourceDataLayer
15
     */
16
    private $dataLayer;
17
18
    /**
19
     * @var (int|string)[]
20
     */
21
    private $idList;
22
23
    /**
24
     * @var array
25
     */
26
    private $searches;
27
28
    /**
29
     * @var string
30
     */
31
    private $resourceClass;
32
33
    /**
34
     * @var array
35
     */
36
    private $context;
37
38
    /**
39
     * @var PropertyAccessorInterface
40
     */
41
    private $propertyAccessor;
42
43
    public function __construct(
44
        MockApiResourceDataLayer $dataLayer,
45
        array $idList,
46
        array $searches,
47
        string $resourceClass,
48
        array $context,
49
        PropertyAccessorInterface  $propertyAccessor
50
    ) {
51
        $this->dataLayer = $dataLayer;
52
        $this->idList = $idList;
53
        $this->searches = $searches;
54
        $this->resourceClass = $resourceClass;
55
        $this->context = $context;
56
        $this->propertyAccessor = $propertyAccessor;
57
    }
58
59
    public function getNbResults()
60
    {
61
        return count($this->idList);
62
    }
63
64
    public function getSlice($offset, $length)
65
    {
66
        $searchFilterRequest = new SearchFilterRequest(
67
            $offset,
68
            $length,
69
            $this->searches
70
        );
71
72
        return array_map(
73
            function ($id) {
74
                return $this->dataLayer->retrieve($this->resourceClass, $id, $this->context);
75
            },
76
            SearchFilterHelper::applySearchFilter($this->idList, $searchFilterRequest, $this->propertyAccessor)
77
        );
78
    }
79
}
80