Passed
Pull Request — master (#94)
by
unknown
02:57 queued 02:57
created

Transaction::reset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
c 0
b 0
f 0
dl 0
loc 8
rs 10
cc 1
nc 1
nop 0
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
        @trigger_error('This method is deprecated, use one of the Transaction::insert(), Transaction::update(), Transaction::remove(), Transaction::associate(), Transaction::dissociate() methods instead.', E_USER_DEPRECATED);
104
105
        switch ($type) {
106
            case self::INSERT:
107
                \assert(2 === \count($data));
108
                $this->insert(...$data);
109
110
                break;
111
112
            case self::UPDATE:
113
                \assert(2 === \count($data));
114
                $this->update(...$data);
115
116
                break;
117
118
            case self::REMOVE:
119
                \assert(2 === \count($data));
120
                $this->remove(...$data);
121
122
                break;
123
124
            case self::ASSOCIATE:
125
                \assert(3 === \count($data));
126
                $this->associate(...$data);
127
128
                break;
129
130
            case self::DISSOCIATE:
131
                \assert(4 === \count($data));
132
                $this->dissociate(...$data);
133
134
                break;
135
        }
136
    }
137
138
    public function insert(object $source, array $changeset): void
139
    {
140
        $this->inserted[] = new InsertEventDto($source, $changeset);
141
    }
142
143
    public function update(object $source, array $changeset): void
144
    {
145
        $this->updated[] = new UpdateEventDto($source, $changeset);
146
    }
147
148
    /**
149
     * @param mixed $id
150
     */
151
    public function remove(object $source, $id): void
152
    {
153
        $this->removed[] = new RemoveEventDto($source, $id);
154
    }
155
156
    public function associate(object $source, object $target, array $mapping): void
157
    {
158
        $this->associated[] = new AssociateEventDto($source, $target, $mapping);
159
    }
160
161
    /**
162
     * @param mixed $id
163
     */
164
    public function dissociate(object $source, object $target, array $mapping): void
165
    {
166
        $this->dissociated[] = new DissociateEventDto($source, $target, $mapping);
167
    }
168
}
169