Completed
Push — master ( 57c0ee...30a057 )
by Tom
10s
created

Selectable::count()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

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