Passed
Push — master ( 6748f8...47fe00 )
by Damien
01:54
created

Transaction::trackAuditEvent()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 34
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 22
c 1
b 0
f 0
dl 0
loc 34
rs 8.9457
cc 6
nc 6
nop 2

3 Methods

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