DoctrineQueryReader::getFields()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 0
cts 5
cp 0
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Mathielen\DataImport\Reader;
4
5
use Ddeboer\DataImport\Reader\DoctrineReader;
6
use Doctrine\ORM\Query;
7
8
/**
9
 * Reads entities through the Doctrine ORM via a definied query.
10
 */
11
class DoctrineQueryReader extends DoctrineReader
12
{
13
    /**
14
     * @var Query
15
     */
16
    private $query;
17
18 1
    public function __construct(Query $query)
19
    {
20 1
        $this->query = $query;
21 1
    }
22
23
    /**
24
     * {@inheritdoc}
25
     */
26
    public function getFields()
27
    {
28
        //TODO this totally sucks. Is there a better way?
29
        $firstResult = $this->query->iterate(array(), Query::HYDRATE_ARRAY);
30
        $firstResult->rewind();
31
        $row = $firstResult->current();
32
33
        return array_keys($row[0]);
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39 1
    public function rewind()
40
    {
41 1
        if (!$this->iterableResult) {
42 1
            $this->iterableResult = $this->query->iterate(array(), Query::HYDRATE_ARRAY);
43
        }
44
45 1
        $this->iterableResult->rewind();
46 1
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 1
    public function count()
52
    {
53 1
        $paginator = new \Doctrine\ORM\Tools\Pagination\Paginator($this->query);
54 1
        $totalRows = count($paginator);
55
56 1
        return $totalRows;
57
    }
58
}
59