Passed
Pull Request — master (#187)
by
unknown
01:53
created

Entry::getExtraField()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 11
rs 10
cc 3
nc 3
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace DH\Auditor\Model;
6
7
use DateTimeImmutable;
8
9
/**
10
 * @see \DH\Auditor\Tests\Model\EntryTest
11
 */
12
final class Entry
13
{
14
    private int $id;
15
16
    private string $type;
17
18
    private string $object_id;
19
20
    private ?string $discriminator = null;
21
22
    private ?string $transaction_hash = null;
23
24
    private string $diffs;
25
26
    private array $extra_fields = [];
27
28
    private int|null|string $blame_id = null;
29
30
    private ?string $blame_user = null;
31
32
    private ?string $blame_user_fqdn = null;
33
34
    private ?string $blame_user_firewall = null;
35
36
    private ?string $ip = null;
37
38
    private DateTimeImmutable $created_at;
39
40
    /**
41
     * Get the value of id.
42
     */
43
    public function getId(): int
44
    {
45
        return $this->id;
46
    }
47
48
    public function setExtraField($key, $value = null): void
49
    {
50
        if ($key === (array)$key) {
51
            $this->extra_fields = $key;
52
        } else {
53
            $this->extra_fields[$key] = $value;
54
        }
55
    }
56
57
    public function getExtraField($key = ''): mixed
58
    {
59
        if ('' === $key) {
60
            return $this->extra_fields;
61
        }
62
63
        if (isset($this->extra_fields[$key])) {
64
            return $this->extra_fields[$key];
65
        }
66
67
        return null;
68
    }
69
70
    /**
71
     * Get the value of type.
72
     */
73
    public function getType(): string
74
    {
75
        return $this->type;
76
    }
77
78
    /**
79
     * Get the value of object_id.
80
     */
81
    public function getObjectId(): string
82
    {
83
        return $this->object_id;
84
    }
85
86
    /**
87
     * Get the value of discriminator.
88
     */
89
    public function getDiscriminator(): ?string
90
    {
91
        return $this->discriminator;
92
    }
93
94
    /**
95
     * Get the value of transaction_hash.
96
     */
97
    public function getTransactionHash(): ?string
98
    {
99
        return $this->transaction_hash;
100
    }
101
102
    /**
103
     * Get the value of blame_id.
104
     */
105
    public function getUserId(): int|null|string
106
    {
107
        return $this->blame_id;
108
    }
109
110
    /**
111
     * Get the value of blame_user.
112
     */
113
    public function getUsername(): ?string
114
    {
115
        return $this->blame_user;
116
    }
117
118
    public function getUserFqdn(): ?string
119
    {
120
        return $this->blame_user_fqdn;
121
    }
122
123
    public function getUserFirewall(): ?string
124
    {
125
        return $this->blame_user_firewall;
126
    }
127
128
    /**
129
     * Get the value of ip.
130
     */
131
    public function getIp(): ?string
132
    {
133
        return $this->ip;
134
    }
135
136
    /**
137
     * Get the value of created_at.
138
     */
139
    public function getCreatedAt(): DateTimeImmutable
140
    {
141
        return $this->created_at;
142
    }
143
144
    /**
145
     * Get diff values.
146
     */
147
    public function getDiffs(bool $includeMedadata = false): array
148
    {
149
        $diffs = $this->sort(json_decode($this->diffs, true, 512, JSON_THROW_ON_ERROR));  // @phpstan-ignore-line
150
        if (!$includeMedadata) {
151
            unset($diffs['@source']);
152
        }
153
154
        return $diffs;
155
    }
156
157
    public static function fromArray(array $row): self
158
    {
159
        $entry = new self();
160
161
        foreach ($row as $key => $value) {
162
            if (property_exists($entry, $key)) {
163
                $entry->{$key} = 'id' === $key ? (int) $value : $value;
164
            } else {
165
                $entry->extra_fields[$key] = $value;
166
            }
167
        }
168
169
        return $entry;
170
    }
171
172
    private function sort(array $array): array
173
    {
174
        ksort($array);
175
        foreach ($array as $key => $value) {
176
            if (\is_array($value)) {
177
                $array[$key] = $this->sort($value);
178
            }
179
        }
180
181
        return $array;
182
    }
183
}
184