/
Transaction.php
157 lines (130 loc) · 3.17 KB
/
Transaction.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
declare(strict_types=1);
namespace DH\Auditor\Model;
use DH\Auditor\Event\Dto\AssociateEventDto;
use DH\Auditor\Event\Dto\DissociateEventDto;
use DH\Auditor\Event\Dto\InsertEventDto;
use DH\Auditor\Event\Dto\RemoveEventDto;
use DH\Auditor\Event\Dto\UpdateEventDto;
use DH\Auditor\Tests\Model\TransactionTest;
/**
* @see TransactionTest
*/
class Transaction implements TransactionInterface
{
/**
* @var string
*/
public const INSERT = 'insert';
/**
* @var string
*/
public const UPDATE = 'update';
/**
* @var string
*/
public const REMOVE = 'remove';
/**
* @var string
*/
public const ASSOCIATE = 'associate';
/**
* @var string
*/
public const DISSOCIATE = 'dissociate';
private ?string $transaction_hash = null;
/**
* @var InsertEventDto[]
*/
private array $inserted = [];
/**
* @var UpdateEventDto[]
*/
private array $updated = [];
/**
* @var RemoveEventDto[]
*/
private array $removed = [];
/**
* @var AssociateEventDto[]
*/
private array $associated = [];
/**
* @var DissociateEventDto[]
*/
private array $dissociated = [];
/**
* Returns transaction hash.
*/
public function getTransactionHash(): string
{
if (null === $this->transaction_hash) {
$this->transaction_hash = sha1(uniqid('tid', true));
}
return $this->transaction_hash;
}
/**
* @return array<InsertEventDto>
*/
public function getInserted(): array
{
return $this->inserted;
}
/**
* @return array<UpdateEventDto>
*/
public function getUpdated(): array
{
return $this->updated;
}
/**
* @return array<RemoveEventDto>
*/
public function getRemoved(): array
{
return $this->removed;
}
/**
* @return array<AssociateEventDto>
*/
public function getAssociated(): array
{
return $this->associated;
}
/**
* @return array<DissociateEventDto>
*/
public function getDissociated(): array
{
return $this->dissociated;
}
public function reset(): void
{
$this->transaction_hash = null;
$this->inserted = [];
$this->updated = [];
$this->removed = [];
$this->associated = [];
$this->dissociated = [];
}
public function insert(object $source, array $changeset): void
{
$this->inserted[] = new InsertEventDto($source, $changeset);
}
public function update(object $source, array $changeset): void
{
$this->updated[] = new UpdateEventDto($source, $changeset);
}
public function remove(object $source, mixed $id): void
{
$this->removed[] = new RemoveEventDto($source, $id);
}
public function associate(object $source, object $target, array $mapping): void
{
$this->associated[] = new AssociateEventDto($source, $target, $mapping);
}
public function dissociate(object $source, object $target, array $mapping): void
{
$this->dissociated[] = new DissociateEventDto($source, $target, $mapping);
}
}