Completed
Push — master ( 0fb3d7...53ff75 )
by Markus
03:02
created

DoctrineStorage::writer()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

Changes 4
Bugs 1 Features 0
Metric Value
c 4
b 1
f 0
dl 0
loc 11
ccs 5
cts 6
cp 0.8333
rs 9.4285
cc 2
eloc 6
nc 2
nop 0
crap 2.0185
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
    /**
41
     * (non-PHPdoc) @see \Mathielen\ImportEngine\Storage\StorageInterface::writer()
42
     */
43 1
    public function writer()
44
    {
45 1
        if (empty($this->entityName)) {
46
            throw new InvalidConfigurationException("Can only use doctrine for writing if entityName is given.");
47
        }
48
49 1
        $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...
50 1
        $writer->setTruncate(false);
51
52 1
        return $writer;
53
    }
54
55
    /**
56
     * (non-PHPdoc) @see \Mathielen\ImportEngine\Storage\StorageInterface::info()
57
     */
58 1
    public function info()
59
    {
60 1
        $count = count($this->reader());
61
62 1
        return new StorageInfo(array(
63 1
            'name' => $this->query->getDQL(),
64 1
            'type' => 'DQL Query',
65 1
            'count' => $count
66
        ));
67
    }
68
69
    /**
70
     * (non-PHPdoc) @see \Mathielen\ImportEngine\Storage\StorageInterface::reader()
71
     */
72 1
    public function reader()
73
    {
74 1
        return new DoctrineQueryReader($this->query);
75
    }
76
77
    /**
78
     * (non-PHPdoc) @see \Mathielen\ImportEngine\Storage\StorageInterface::getFields()
79
     */
80
    public function getFields()
81
    {
82
        return $this->reader()->getFields();
83
    }
84
}
85