Completed
Pull Request — master (#615)
by Filippo
14:02 queued 03:29
created

Selectable   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 66.67%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 3
dl 0
loc 48
ccs 8
cts 12
cp 0.6667
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 2
A getItems() 0 6 1
A count() 0 9 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