1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CultuurNet\UDB3\MyOrganizers\ReadModel\Doctrine; |
4
|
|
|
|
5
|
|
|
use DateTimeInterface; |
6
|
|
|
use CultuurNet\UDB3\MyOrganizers\ReadModel\RepositoryInterface; |
7
|
|
|
use Doctrine\DBAL\Connection; |
8
|
|
|
use ValueObjects\StringLiteral\StringLiteral; |
9
|
|
|
|
10
|
|
|
class DBALRepository implements RepositoryInterface |
11
|
|
|
{ |
12
|
|
|
use DBALHelperTrait; |
13
|
|
|
|
14
|
|
|
/** @var StringLiteral */ |
15
|
|
|
private $tableName; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param Connection $connection |
19
|
|
|
* @param StringLiteral $tableName |
20
|
|
|
*/ |
21
|
|
|
public function __construct( |
22
|
|
|
Connection $connection, |
23
|
|
|
StringLiteral $tableName |
24
|
|
|
) { |
25
|
|
|
$this->connection = $connection; |
26
|
|
|
$this->tableName = $tableName; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function add( |
30
|
|
|
string $id, |
31
|
|
|
string $userId, |
32
|
|
|
DateTimeInterface $created |
33
|
|
|
) { |
34
|
|
|
$this->connection->beginTransaction(); |
35
|
|
|
|
36
|
|
|
try { |
37
|
|
|
/** @var \Doctrine\DBAL\Query\QueryBuilder $q */ |
38
|
|
|
$q = $this->connection->createQueryBuilder(); |
39
|
|
|
$q->insert($this->tableName->toNative()) |
40
|
|
|
->values( |
41
|
|
|
[ |
42
|
|
|
Columns::ID => $this->parameter(Columns::ID), |
43
|
|
|
Columns::UID => $this->parameter(Columns::UID), |
44
|
|
|
Columns::CREATED => $this->parameter(Columns::CREATED), |
45
|
|
|
// We intentionally set updated the same as created. |
46
|
|
|
Columns::UPDATED => $this->parameter(Columns::CREATED), |
47
|
|
|
] |
48
|
|
|
); |
49
|
|
|
|
50
|
|
|
$q->setParameter(Columns::ID, $id); |
51
|
|
|
$q->setParameter(Columns::UID, $userId); |
52
|
|
|
$q->setParameter(Columns::CREATED, $created->getTimestamp()); |
53
|
|
|
|
54
|
|
|
$q->execute(); |
55
|
|
|
} catch (\Exception $e) { |
56
|
|
|
$this->connection->rollBack(); |
57
|
|
|
throw $e; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$this->connection->commit(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function delete(string $id) |
64
|
|
|
{ |
65
|
|
|
$q = $this->connection->createQueryBuilder(); |
66
|
|
|
|
67
|
|
|
$q->delete($this->tableName->toNative()) |
68
|
|
|
->where($this->matchesId()) |
69
|
|
|
->setParameter(Columns::ID, $id) |
70
|
|
|
->execute(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function setUpdateDate(string $id, DateTimeInterface $updated) |
74
|
|
|
{ |
75
|
|
|
$q = $this->connection->createQueryBuilder(); |
76
|
|
|
|
77
|
|
|
$q->update($this->tableName->toNative()) |
78
|
|
|
->where($this->matchesId()) |
79
|
|
|
->set(Columns::UPDATED, $updated->getTimestamp()) |
80
|
|
|
->setParameter(Columns::ID, $id) |
81
|
|
|
->execute(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
private function matchesId(): string |
85
|
|
|
{ |
86
|
|
|
$expr = $this->connection->getExpressionBuilder(); |
87
|
|
|
|
88
|
|
|
return $expr->eq(Columns::ID, $this->parameter(Columns::ID)); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|