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