DoctrineSelectableAdapter::createCriteria()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
/*
4
 * This file is part of the Pagerfanta package.
5
 *
6
 * (c) Pablo Díez <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Pagerfanta\Adapter;
13
14
use Doctrine\Common\Collections\Criteria;
15
use Doctrine\Common\Collections\Selectable;
16
17
/**
18
 * DoctrineSelectableAdapter.
19
 *
20
 * @author Boris Guéry <[email protected]>
21
 */
22
class DoctrineSelectableAdapter implements AdapterInterface
23
{
24
    /**
25
     * @var Selectable
26
     */
27
    private $selectable;
28
29
    /**
30
     * @var Criteria
31
     */
32
    private $criteria;
33
34
    /**
35
     * Constructor.
36
     *
37
     * @param Selectable $selectable An implementation of the Selectable interface.
38
     * @param Criteria   $criteria   A Doctrine criteria.
39
     */
40 2
    public function __construct(Selectable $selectable, Criteria $criteria)
41
    {
42 2
        $this->selectable = $selectable;
43 2
        $this->criteria   = $criteria;
44 2
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49 1
    public function getNbResults()
50
    {
51 1
        $firstResult = null;
52 1
        $maxResults = null;
53
54 1
        $criteria = $this->createCriteria($firstResult, $maxResults);
55
56 1
        return $this->selectable->matching($criteria)->count();
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62 1
    public function getSlice($offset, $length)
63
    {
64 1
        $firstResult = $offset;
65 1
        $maxResults = $length;
66
67 1
        $criteria = $this->createCriteria($firstResult, $maxResults);
68
69 1
        return $this->selectable->matching($criteria);
70
    }
71
72 2
    private function createCriteria($firstResult, $maxResult)
73
    {
74 2
        $criteria = clone $this->criteria;
75 2
        $criteria->setFirstResult($firstResult);
76 2
        $criteria->setMaxResults($maxResult);
77
78 2
        return $criteria;
79
    }
80
}
81