Passed
Push — master ( 6843e8...6b4be4 )
by Damien
04:41
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
namespace DH\Auditor\Model;
4
5
class Transaction implements TransactionInterface
6
{
7
    public const INSERT = 'insert';
8
    public const UPDATE = 'update';
9
    public const REMOVE = 'remove';
10
    public const ASSOCIATE = 'associate';
11
    public const DISSOCIATE = 'dissociate';
12
13
    /**
14
     * @var null|string
15
     */
16
    private $transaction_hash;
17
18
    /**
19
     * @var array
20
     */
21
    private $inserted = [];     // [$source, $changeset]
22
23
    /**
24
     * @var array
25
     */
26
    private $updated = [];      // [$source, $changeset]
27
28
    /**
29
     * @var array
30
     */
31
    private $removed = [];      // [$source, $id]
32
33
    /**
34
     * @var array
35
     */
36
    private $associated = [];   // [$source, $target, $mapping]
37
38
    /**
39
     * @var array
40
     */
41
    private $dissociated = [];  // [$source, $target, $id, $mapping]
42
43
    /**
44
     * Returns transaction hash.
45
     */
46
    public function getTransactionHash(): string
47
    {
48
        if (null === $this->transaction_hash) {
49
            $this->transaction_hash = sha1(uniqid('tid', true));
50
        }
51
52
        return $this->transaction_hash;
53
    }
54
55
    public function getInserted(): array
56
    {
57
        return $this->inserted;
58
    }
59
60
    public function getUpdated(): array
61
    {
62
        return $this->updated;
63
    }
64
65
    public function getRemoved(): array
66
    {
67
        return $this->removed;
68
    }
69
70
    public function getAssociated(): array
71
    {
72
        return $this->associated;
73
    }
74
75
    public function getDissociated(): array
76
    {
77
        return $this->dissociated;
78
    }
79
80
    public function reset(): void
81
    {
82
        $this->transaction_hash = null;
83
        $this->inserted = [];
84
        $this->updated = [];
85
        $this->removed = [];
86
        $this->associated = [];
87
        $this->dissociated = [];
88
    }
89
90
    public function trackAuditEvent(string $type, array $data): void
91
    {
92
        switch ($type) {
93
            case self::INSERT:
94
                $this->inserted[] = $data;
95
96
                break;
97
            case self::UPDATE:
98
                $this->updated[] = $data;
99
100
                break;
101
            case self::REMOVE:
102
                $this->removed[] = $data;
103
104
                break;
105
            case self::ASSOCIATE:
106
                $this->associated[] = $data;
107
108
                break;
109
            case self::DISSOCIATE:
110
                $this->dissociated[] = $data;
111
112
                break;
113
        }
114
    }
115
}
116