|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Scheduler\Infrastructure\DBAL; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Connection; |
|
6
|
|
|
use Doctrine\DBAL\Statement; |
|
7
|
|
|
|
|
8
|
|
|
abstract class DbalMapper |
|
9
|
|
|
{ |
|
10
|
|
|
protected $db; |
|
11
|
|
|
protected $loadedMap; |
|
12
|
|
|
|
|
13
|
|
|
public function __construct(Connection $db) |
|
14
|
|
|
{ |
|
15
|
|
|
$this->db = $db; |
|
16
|
|
|
$this->loadedMap = []; |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
protected function abstractFind($id) |
|
20
|
|
|
{ |
|
21
|
|
|
if (isset($this->loadedMap[$id])) { |
|
22
|
|
|
return $this->loadedMap[$id]; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
$findStatement = $this->db->prepare($this->findStatement()); |
|
26
|
|
|
$findStatement->bindValue(1, $id, \PDO::PARAM_INT); |
|
27
|
|
|
$findStatement->execute(); |
|
28
|
|
|
$resultSet = $findStatement->fetch(); |
|
29
|
|
|
|
|
30
|
|
|
if (! $resultSet) { |
|
31
|
|
|
return null; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
return $this->load($resultSet); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
protected function abstractInsert($subject) |
|
38
|
|
|
{ |
|
39
|
|
|
$insertStatement = $this->db->prepare(static::insertStatement()); |
|
40
|
|
|
$this->doInsert($subject, $insertStatement); |
|
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
$id = (int) $this->db->lastInsertId(); |
|
43
|
|
|
$this->setPropertyValue($subject, "id", $id); |
|
44
|
|
|
|
|
45
|
|
|
$this->loadedMap[$id] = $subject; |
|
46
|
|
|
|
|
47
|
|
|
return $id; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function clean() |
|
51
|
|
|
{ |
|
52
|
|
|
$this->loadedMap = []; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Bind query parameters and execute insert statement |
|
57
|
|
|
* |
|
58
|
|
|
* @param $subject |
|
59
|
|
|
* @param Statement $insertStatement |
|
60
|
|
|
* |
|
61
|
|
|
* @return int The id of the inserted row |
|
62
|
|
|
*/ |
|
63
|
|
|
abstract protected function doInsert($subject, Statement $insertStatement); |
|
64
|
|
|
|
|
65
|
|
|
abstract protected function doLoad($id, array $resultSet); |
|
66
|
|
|
|
|
67
|
|
|
protected static function findStatement() |
|
68
|
|
|
{ |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
protected static function insertStatement() |
|
72
|
|
|
{ |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
protected function load(array $resultSet) |
|
76
|
|
|
{ |
|
77
|
|
|
$id = $resultSet["id"]; |
|
78
|
|
|
|
|
79
|
|
|
if (isset($this->loadedMap[$id])) { |
|
80
|
|
|
return $this->loadedMap[$id]; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
$result = $this->doLoad($id, $resultSet); |
|
84
|
|
|
$this->loadedMap[$id] = $result; |
|
85
|
|
|
|
|
86
|
|
|
return $result; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
protected function loadAll(Statement $statement) |
|
90
|
|
|
{ |
|
91
|
|
|
$result = []; |
|
92
|
|
|
foreach ($statement as $row) { |
|
93
|
|
|
$result[] = $this->load($row); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return $result; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
protected function setPropertyValue($subject, $name, $value) |
|
100
|
|
|
{ |
|
101
|
|
|
$reflector = new \ReflectionClass($subject); |
|
102
|
|
|
|
|
103
|
|
|
$property = $reflector->getProperty($name); |
|
104
|
|
|
$property->setAccessible(true); |
|
105
|
|
|
$property->setValue($subject, $value); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
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.