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

ORMQueryResult::getQuery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 1
cts 1
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\ORM\Query;
6
use Doctrine\ORM\QueryBuilder;
7
use Doctrine\ORM\Tools\Pagination\Paginator;
8
use Zenstruck\Porpaginas\Arrays\ArrayPage;
9
use Zenstruck\Porpaginas\Callback\CallbackPage;
10
use Zenstruck\Porpaginas\Result;
11
12
final class ORMQueryResult implements Result
13
{
14
    private $query;
15
    private $fetchCollection;
16
    private $count;
17
    private $result;
18
19
    /**
20
     * @param Query|QueryBuilder $query
21 16
     * @param bool               $fetchCollection
22
     */
23 16
    public function __construct($query, $fetchCollection = true)
24 8
    {
25 8
        if ($query instanceof QueryBuilder) {
26
            $query = $query->getQuery();
27 16
        }
28 16
29 16
        $this->query = $query;
30
        $this->fetchCollection = $fetchCollection;
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36 10
    public function take($offset, $limit)
37 10
    {
38 10
        if ($this->result !== null) {
39
            return new ArrayPage(
40 10
                array_slice($this->result, $offset, $limit),
41
                $offset,
42
                $limit,
43
                count($this->result)
44
            );
45
        }
46 6
47
        $results = function ($offset, $limit) {
48 6
            return iterator_to_array($this->createPaginator($offset, $limit));
49
        };
50
51
        return new CallbackPage($results, [$this, 'count'], $offset, $limit);
52 6
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function count()
58 6
    {
59
        if (null !== $this->count) {
60 6
            return $this->count;
61 4
        }
62 6
63 6
        return $this->count = count($this->createPaginator(0, 1));
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function getIterator()
70
    {
71 12
        if (null === $this->result) {
72
            $this->result = $this->query->execute();
73 12
            $this->count = count($this->result);
74 12
        }
75
76 12
        return new \ArrayIterator($this->result);
77 2
    }
78 12
79
    /**
80 12
     * @return Query
81
     */
82 12
    public function getQuery()
83
    {
84
        return $this->query;
85
    }
86
87
    /**
88
     * @param int $offset
89
     * @param int $limit
90
     *
91
     * @return Paginator
92
     */
93
    private function createPaginator($offset, $limit)
94
    {
95
        $query = clone $this->query;
96
        $query->setParameters($this->query->getParameters());
97
98
        foreach ($this->query->getHints() as $name => $value) {
99
            $query->setHint($name, $value);
100
        }
101
102
        $query->setFirstResult($offset)->setMaxResults($limit);
103
104
        return new Paginator($query, $this->fetchCollection);
105
    }
106
}
107