Selectable::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace DoctrineModule\Paginator\Adapter;
6
7
use Doctrine\Common\Collections\Criteria;
8
use Doctrine\Common\Collections\Selectable as DoctrineSelectable;
9
use Laminas\Paginator\Adapter\AdapterInterface;
10
use function count;
11
12
/**
13
 * Provides a wrapper around a Selectable object
14
 *
15
 * @link    http://www.doctrine-project.org/
16
 */
17
class Selectable implements AdapterInterface
18
{
19
    /** @var DoctrineSelectable */
20
    protected $selectable;
21
22
    /** @var Criteria */
23
    protected $criteria;
24
25
    /**
26
     * Create a paginator around a Selectable object. You can also provide an optional Criteria object with
27
     * some predefined filters
28
     */
29
    public function __construct(DoctrineSelectable $selectable, ?Criteria $criteria = null)
30
    {
31
        $this->selectable = $selectable;
32
        $this->criteria   = $criteria ? clone $criteria : new Criteria();
33
    }
34
35
    /**
36
     * {@inheritDoc}
37
     */
38 4
    public function getItems($offset, $itemCountPerPage)
39
    {
40 4
        $this->criteria->setFirstResult($offset)->setMaxResults($itemCountPerPage);
41
42 4
        return $this->selectable->matching($this->criteria)->toArray();
43
    }
44
45
    /**
46
     * {@inheritDoc}
47
     */
48 1
    public function count()
49
    {
50 1
        $criteria = clone $this->criteria;
51
52 1
        $criteria->setFirstResult(null);
53 1
        $criteria->setMaxResults(null);
54
55 1
        return count($this->selectable->matching($criteria));
56
    }
57
}
58