Entry::getUsername()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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 null|int|string $blame_id = null;
27
28
    private ?string $blame_user = null;
29
30
    private ?string $blame_user_fqdn = null;
31
32
    private ?string $blame_user_firewall = null;
33
34
    private ?string $ip = null;
35
36
    private DateTimeImmutable $created_at;
37
38
    /**
39
     * Get the value of id.
40
     */
41
    public function getId(): int
42
    {
43
        return $this->id;
44
    }
45
46
    /**
47
     * Get the value of type.
48
     */
49
    public function getType(): string
50
    {
51
        return $this->type;
52
    }
53
54
    /**
55
     * Get the value of object_id.
56
     */
57
    public function getObjectId(): string
58
    {
59
        return $this->object_id;
60
    }
61
62
    /**
63
     * Get the value of discriminator.
64
     */
65
    public function getDiscriminator(): ?string
66
    {
67
        return $this->discriminator;
68
    }
69
70
    /**
71
     * Get the value of transaction_hash.
72
     */
73
    public function getTransactionHash(): ?string
74
    {
75
        return $this->transaction_hash;
76
    }
77
78
    /**
79
     * Get the value of blame_id.
80
     */
81
    public function getUserId(): null|int|string
82
    {
83
        return $this->blame_id;
84
    }
85
86
    /**
87
     * Get the value of blame_user.
88
     */
89
    public function getUsername(): ?string
90
    {
91
        return $this->blame_user;
92
    }
93
94
    public function getUserFqdn(): ?string
95
    {
96
        return $this->blame_user_fqdn;
97
    }
98
99
    public function getUserFirewall(): ?string
100
    {
101
        return $this->blame_user_firewall;
102
    }
103
104
    /**
105
     * Get the value of ip.
106
     */
107
    public function getIp(): ?string
108
    {
109
        return $this->ip;
110
    }
111
112
    /**
113
     * Get the value of created_at.
114
     */
115
    public function getCreatedAt(): DateTimeImmutable
116
    {
117
        return $this->created_at;
118
    }
119
120
    /**
121
     * Get diff values.
122
     */
123
    public function getDiffs(bool $includeMedadata = false): array
124
    {
125
        $diffs = $this->sort(json_decode($this->diffs, true, 512, JSON_THROW_ON_ERROR));  // @phpstan-ignore-line
126
        if (!$includeMedadata) {
127
            unset($diffs['@source']);
128
        }
129
130
        return $diffs;
131
    }
132
133
    public static function fromArray(array $row): self
134
    {
135
        $entry = new self();
136
137
        foreach ($row as $key => $value) {
138
            if (property_exists($entry, $key)) {
139
                $entry->{$key} = 'id' === $key ? (int) $value : $value;
140
            }
141
        }
142
143
        return $entry;
144
    }
145
146
    private function sort(array $array): array
147
    {
148
        ksort($array);
149
        foreach ($array as $key => $value) {
150
            if (\is_array($value)) {
151
                $array[$key] = $this->sort($value);
152
            }
153
        }
154
155
        return $array;
156
    }
157
}
158