|
1
|
|
|
<?php |
|
2
|
|
|
namespace Mathielen\ImportEngine\Storage\Provider; |
|
3
|
|
|
|
|
4
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
5
|
|
|
use Doctrine\ORM\Query; |
|
6
|
|
|
use Doctrine\ORM\QueryBuilder; |
|
7
|
|
|
use Mathielen\ImportEngine\Storage\Provider\Connection\ConnectionFactoryInterface; |
|
8
|
|
|
use Mathielen\ImportEngine\ValueObject\StorageSelection; |
|
9
|
|
|
use Mathielen\ImportEngine\Storage\DoctrineStorage; |
|
10
|
|
|
|
|
11
|
|
|
class DoctrineQueryStorageProvider implements \IteratorAggregate, StorageProviderInterface |
|
12
|
|
|
{ |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* |
|
16
|
|
|
* @var ConnectionFactoryInterface |
|
17
|
|
|
*/ |
|
18
|
|
|
private $connectionFactory; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var QueryBuilder[] |
|
22
|
|
|
*/ |
|
23
|
|
|
private $queries; |
|
24
|
|
|
|
|
25
|
|
|
public function __construct(ConnectionFactoryInterface $connectionFactory, $classNamesOrQueries) |
|
26
|
|
|
{ |
|
27
|
|
|
$this->connectionFactory = $connectionFactory; |
|
28
|
|
|
$this->resolveQueries($classNamesOrQueries); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
private function resolveQueries($classNamesOrQueries) |
|
32
|
|
|
{ |
|
33
|
|
|
if ( !is_array( $classNamesOrQueries ) && !$classNamesOrQueries instanceof \Traversable ) { |
|
34
|
|
|
throw new \InvalidArgumentException("classNamesOrQueries must be an array or Traversable"); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
//TODO what about different entity providers ? |
|
38
|
|
|
/** @var EntityManagerInterface $entityManager */ |
|
39
|
|
|
$entityManager = $this->connectionFactory->getConnection(); |
|
40
|
|
|
|
|
41
|
|
|
$this->queries = array(); |
|
42
|
|
|
foreach ($classNamesOrQueries as $k=>$classNameOrQuery) { |
|
43
|
|
|
if (is_string($classNameOrQuery) && class_exists($classNameOrQuery)) { |
|
44
|
|
|
$query = $entityManager->createQueryBuilder() |
|
45
|
|
|
->select('o') |
|
46
|
|
|
->from($classNameOrQuery, 'o') |
|
47
|
|
|
->getQuery(); |
|
48
|
|
|
$selection = new StorageSelection($query, $classNameOrQuery, $classNameOrQuery); |
|
49
|
|
|
|
|
50
|
|
|
} elseif (is_string($classNameOrQuery)) { |
|
51
|
|
|
$query = $entityManager->createQuery($classNameOrQuery); |
|
52
|
|
|
$selection = new StorageSelection($query, $query->getDQL(), $query->getDQL()); |
|
53
|
|
|
|
|
54
|
|
|
} elseif ($classNameOrQuery instanceof Query) { |
|
55
|
|
|
$selection = new StorageSelection($classNameOrQuery, $classNameOrQuery->getDQL(), $classNameOrQuery->getDQL()); |
|
56
|
|
|
|
|
57
|
|
|
} else { |
|
58
|
|
|
throw new \InvalidArgumentException("Only strings or QueryBuilder are allowed!"); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$this->queries[$k] = $selection; |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function getIterator() |
|
66
|
|
|
{ |
|
67
|
|
|
return new \ArrayIterator($this->queries); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* (non-PHPdoc) |
|
72
|
|
|
* @see \Mathielen\ImportEngine\Storage\Provider\StorageProviderInterface::storage() |
|
73
|
|
|
*/ |
|
74
|
|
|
public function storage(StorageSelection $selection) |
|
75
|
|
|
{ |
|
76
|
|
|
$connection = $this->connectionFactory->getConnection($selection); |
|
77
|
|
|
|
|
78
|
|
|
return new DoctrineStorage($connection, $selection->getName(), $selection->getImpl()); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* (non-PHPdoc) |
|
83
|
|
|
* @see \Mathielen\ImportEngine\Storage\Provider\StorageProviderInterface::select() |
|
84
|
|
|
*/ |
|
85
|
|
|
public function select($id = null) |
|
86
|
|
|
{ |
|
87
|
|
|
if ($id instanceof StorageSelection) { |
|
88
|
|
|
return $id; |
|
89
|
|
|
} else { |
|
90
|
|
|
return $this->queries[$id]; |
|
|
|
|
|
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
} |
|
95
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.