Completed
Push — master ( a433d4...897506 )
by Kevin
06:44
created

RSMQueryResult::getQuery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Zenstruck\Porpaginas\Doctrine;
4
5
use Doctrine\DBAL\Query\QueryBuilder;
6
use Doctrine\ORM\EntityManagerInterface;
7
use Doctrine\ORM\NativeQuery;
8
use Doctrine\ORM\Query\ResultSetMappingBuilder;
9
use Zenstruck\Porpaginas\Callback\CallbackPage;
10
use Zenstruck\Porpaginas\Result;
11
12
/**
13
 * @author Florian Klein <[email protected]>
14
 * @author Kevin Bond <[email protected]>
15
 */
16
final class RSMQueryResult implements Result
17
{
18
    private $em;
19
    private $rsm;
20
    private $qb;
21
    private $queryBuilderResult;
22
23
    /**
24
     * @param EntityManagerInterface  $em
25
     * @param ResultSetMappingBuilder $rsm
26
     * @param QueryBuilder            $qb
27
     * @param callable|null           $countQueryBuilderModifier
28 8
     */
29
    public function __construct(EntityManagerInterface $em, ResultSetMappingBuilder $rsm, QueryBuilder $qb, callable $countQueryBuilderModifier = null)
30 8
    {
31 8
        $this->em = $em;
32 8
        $this->rsm = $rsm;
33 8
        $this->qb = $qb;
34 8
        $this->queryBuilderResult = new DBALQueryBuilderResult($qb, $countQueryBuilderModifier);
35
    }
36
37
    /**
38
     * {@inheritdoc}
39 5
     */
40 View Code Duplication
    public function take($offset, $limit)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
41 5
    {
42 5
        $qb = clone $this->qb;
43 5
        $results = function ($offset, $limit) use ($qb) {
44 5
            $qb->setFirstResult($offset)
45
                ->setMaxResults($limit);
46 5
47 5
            return $this->createQuery($qb)->execute();
48
        };
49 5
50 5
        return new CallbackPage($results, [$this, 'count'], $offset, $limit);
51
    }
52 5
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function count()
57
    {
58 3
        return $this->queryBuilderResult->count();
59
    }
60 3
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function getIterator()
65
    {
66 3
        return new \ArrayIterator($this->getQuery()->execute());
67
    }
68 3
69 3
    /**
70
     * @return NativeQuery
71 3
     */
72 2
    public function getQuery()
73 3
    {
74 3
        return $this->createQuery($this->qb);
75
    }
76
77
    /**
78
     * @param QueryBuilder $qb
79
     *
80
     * @return NativeQuery
81
     */
82
    private function createQuery(QueryBuilder $qb)
83
    {
84
        $query = $this->em->createNativeQuery($qb->getSQL(), $this->rsm);
85
        $query->setParameters($this->qb->getParameters());
86
87
        return $query;
88
    }
89
}
90