DoctrineQueryReader   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 70.59%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 4
dl 0
loc 48
ccs 12
cts 17
cp 0.7059
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getFields() 0 9 1
A rewind() 0 8 2
A count() 0 7 1
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