Completed
Pull Request — master (#90)
by Arnaud
02:00
created

PagerfantaAdminAdapter::getSlice()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 2
1
<?php
2
3
namespace LAG\AdminBundle\Pager;
4
5
use LAG\AdminBundle\DataProvider\DataProviderInterface;
6
use Pagerfanta\Adapter\AdapterInterface;
7
8
class PagerfantaAdminAdapter implements AdapterInterface
9
{
10
    /**
11
     * @var DataProviderInterface
12
     */
13
    private $dataProvider;
14
15
    /**
16
     * @var array
17
     */
18
    private $criteria;
19
20
    /**
21
     * @var array
22
     */
23
    private $orderBy;
24
    
25
    /**
26
     * @var array
27
     */
28
    private $options;
29
    
30
    private $data;
31
    
32
    /**
33
     * PagerfantaAdminAdapter constructor.
34
     *
35
     * @param DataProviderInterface $dataProvider
36
     * @param array                 $criteria
37
     * @param array                 $orderBy
38
     * @param array                 $options
39
     */
40
    public function __construct(
41
        DataProviderInterface $dataProvider,
42
        array $criteria = [],
43
        array $orderBy = [],
44
        array $options = []
45
    ) {
46
        $this->dataProvider = $dataProvider;
47
        $this->criteria = $criteria;
48
        $this->orderBy = $orderBy;
49
        $this->options = $options;
50
    }
51
52
    /**
53
     * Returns the number of results.
54
     *
55
     * @return integer The number of results.
56
     */
57
    public function getNbResults()
58
    {
59
        return $this
60
            ->dataProvider
61
            ->count($this->criteria)
62
        ;
63
    }
64
    
65
    /**
66
     * Returns an slice of the results.
67
     *
68
     * @param integer $offset The offset.
69
     * @param integer $length The length.
70
     *
71
     * @return array|\Traversable The slice.
72
     */
73
    public function getSlice($offset, $length)
74
    {
75
        $this->retrieveData($length, $offset);
76
    
77
        return $this->data;
78
    }
79
    
80
    private function retrieveData($length, $offset)
81
    {
82
        $this->data = $this
83
            ->dataProvider
84
            ->findBy($this->criteria, $this->orderBy, $length, $offset)
85
        ;
86
    }
87
}
88