Completed
Push — master ( 252438...82275d )
by Markus
03:16
created

DoctrineStorage   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 7

Test Coverage

Coverage 68.42%

Importance

Changes 9
Bugs 2 Features 0
Metric Value
wmc 13
c 9
b 2
f 0
lcom 2
cbo 7
dl 0
loc 95
ccs 26
cts 38
cp 0.6842
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 4
A getQuery() 0 4 1
A setQuery() 0 4 1
A getEntityName() 0 4 1
A setEntityName() 0 4 1
A writer() 0 11 2
A info() 0 10 1
A reader() 0 4 1
A getFields() 0 4 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 1
        }
36
37 1
        $this->query = $query;
38 1
    }
39
40
    public function getQuery()
41
    {
42
        return $this->query;
43
    }
44
45
    public function setQuery($query)
46
    {
47
        $this->query = $query;
48
    }
49
50 1
    public function getEntityName()
51
    {
52
        return $this->entityName;
53 1
    }
54
55
    public function setEntityName($entityName)
56
    {
57
        $this->entityName = $entityName;
58
    }
59
60
    /**
61
     * (non-PHPdoc) @see \Mathielen\ImportEngine\Storage\StorageInterface::writer()
62
     */
63 1
    public function writer()
64
    {
65 1
        if (empty($this->entityName)) {
66
            throw new InvalidConfigurationException("Can only use doctrine for writing if entityName is given.");
67
        }
68
69 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...
70 1
        $writer->setTruncate(false);
71
72 1
        return $writer;
73
    }
74
75
    /**
76
     * (non-PHPdoc) @see \Mathielen\ImportEngine\Storage\StorageInterface::info()
77
     */
78 1
    public function info()
79
    {
80 1
        $count = count($this->reader());
81
82 1
        return new StorageInfo(array(
83 1
            'name' => $this->query->getDQL(),
84 1
            'type' => 'DQL Query',
85
            'count' => $count
86 1
        ));
87
    }
88
89
    /**
90
     * (non-PHPdoc) @see \Mathielen\ImportEngine\Storage\StorageInterface::reader()
91
     */
92 1
    public function reader()
93
    {
94 1
        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