Completed
Push — master ( 53ff75...252438 )
by Markus
04:10
created

DoctrineStorage::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 0
Metric Value
c 1
b 0
f 0
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
namespace Mathielen\ImportEngine\Storage;
3
4
use Ddeboer\DataImport\Writer\DoctrineWriter;
5
use Doctrine\ORM\EntityManagerInterface;
6
use Doctrine\ORM\Query;
7
use Mathielen\DataImport\Reader\DoctrineQueryReader;
8
use Mathielen\ImportEngine\Exception\InvalidConfigurationException;
9
10
class DoctrineStorage implements StorageInterface
11
{
12
13
    /**
14
     * @var EntityManagerInterface
15
     */
16
    private $entityManager;
17
18
    /**
19
     * @var Query
20
     */
21
    private $query;
22
23
    private $entityName;
24
25 1
    public function __construct(EntityManagerInterface $entityManager, $entityName=null, Query $query=null)
26
    {
27 1
        $this->entityManager = $entityManager;
28 1
        $this->entityName = $entityName;
29
30 1
        if (is_null($query) && is_string($entityName) && class_exists($entityName)) {
31 1
            $query = $this->entityManager->createQueryBuilder()
32 1
                ->select('o')
33 1
                ->from($this->entityName, 'o')
34 1
                ->getQuery();
35
        }
36
37 1
        $this->query = $query;
38 1
    }
39
40
    public function getQuery()
41
    {
42
        return $this->query;
43 1
    }
44
45 1
    public function setQuery($query)
46
    {
47
        $this->query = $query;
48
    }
49 1
50 1
    public function getEntityName()
51
    {
52 1
        return $this->entityName;
53
    }
54
55
    public function setEntityName($entityName)
56
    {
57
        $this->entityName = $entityName;
58 1
    }
59
60 1
    /**
61
     * (non-PHPdoc) @see \Mathielen\ImportEngine\Storage\StorageInterface::writer()
62 1
     */
63 1
    public function writer()
64 1
    {
65 1
        if (empty($this->entityName)) {
66
            throw new InvalidConfigurationException("Can only use doctrine for writing if entityName is given.");
67
        }
68
69
        $writer = new DoctrineWriter($this->entityManager, $this->entityName);
0 ignored issues
show
Compatibility introduced by
$this->entityManager of type object<Doctrine\ORM\EntityManagerInterface> is not a sub-type of object<Doctrine\ORM\EntityManager>. It seems like you assume a concrete implementation of the interface Doctrine\ORM\EntityManagerInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
70
        $writer->setTruncate(false);
71
72 1
        return $writer;
73
    }
74 1
75
    /**
76
     * (non-PHPdoc) @see \Mathielen\ImportEngine\Storage\StorageInterface::info()
77
     */
78
    public function info()
79
    {
80
        $count = count($this->reader());
81
82
        return new StorageInfo(array(
83
            'name' => $this->query->getDQL(),
84
            'type' => 'DQL Query',
85
            'count' => $count
86
        ));
87
    }
88
89
    /**
90
     * (non-PHPdoc) @see \Mathielen\ImportEngine\Storage\StorageInterface::reader()
91
     */
92
    public function reader()
93
    {
94
        return new DoctrineQueryReader($this->query);
95
    }
96
97
    /**
98
     * (non-PHPdoc) @see \Mathielen\ImportEngine\Storage\StorageInterface::getFields()
99
     */
100
    public function getFields()
101
    {
102
        return $this->reader()->getFields();
103
    }
104
}
105