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); |
|
|
|
|
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
|
|
|
|
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.