Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
41.03% |
16 / 39 |
|
40.00% |
2 / 5 |
CRAP | |
0.00% |
0 / 1 |
UserAuditLogsEntityDao | |
41.03% |
16 / 39 |
|
40.00% |
2 / 5 |
10.13 | |
0.00% |
0 / 1 |
selectLastLogId | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
selectAuditLogsPageTotalCountFromUserId | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
2 | |||
selectAuditLogsPageItemsFromUserId | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
2 | |||
selectAuditLogsFromUserId | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
2 | |||
insertLog | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace PeServer\App\Models\Dao\Entities; |
6 | |
7 | use PeServer\Core\Database\DaoBase; |
8 | use PeServer\Core\Database\DaoTrait; |
9 | use PeServer\Core\Database\DatabaseTableResult; |
10 | use PeServer\Core\Database\IDatabaseContext; |
11 | |
12 | class UserAuditLogsEntityDao extends DaoBase |
13 | { |
14 | use DaoTrait; |
15 | |
16 | #region function |
17 | |
18 | public function selectLastLogId(): int |
19 | { |
20 | $result = $this->context->queryFirst( |
21 | <<<SQL |
22 | |
23 | select |
24 | LAST_INSERT_ROWID() as [row_id] |
25 | |
26 | SQL |
27 | ); |
28 | |
29 | return intval($result->fields['row_id']); |
30 | } |
31 | |
32 | /** |
33 | * Undocumented function |
34 | * |
35 | * @param string $userId |
36 | * @return int |
37 | * @phpstan-return non-negative-int |
38 | */ |
39 | public function selectAuditLogsPageTotalCountFromUserId(string $userId): int |
40 | { |
41 | return $this->context->selectSingleCount( |
42 | <<<SQL |
43 | |
44 | select |
45 | count(*) |
46 | from |
47 | user_audit_logs |
48 | where |
49 | user_audit_logs.user_id = :user_id |
50 | |
51 | SQL, |
52 | [ |
53 | 'user_id' => $userId, |
54 | ] |
55 | ); |
56 | } |
57 | |
58 | /** |
59 | * Undocumented function |
60 | * |
61 | * @template TFieldArray of array{timestamp:string,event:string,info:string,ip_address:string,user_agent:string} |
62 | * @param string $userId |
63 | * @param int $index |
64 | * @param int $count |
65 | * @return DatabaseTableResult |
66 | * @phpstan-return DatabaseTableResult<TFieldArray> |
67 | */ |
68 | public function selectAuditLogsPageItemsFromUserId(string $userId, int $index, int $count): DatabaseTableResult |
69 | { |
70 | /** @var DatabaseTableResult<TFieldArray> */ |
71 | return $this->context->selectOrdered( |
72 | <<<SQL |
73 | |
74 | select |
75 | user_audit_logs.timestamp, |
76 | user_audit_logs.event, |
77 | user_audit_logs.info, |
78 | user_audit_logs.ip_address, |
79 | user_audit_logs.user_agent |
80 | from |
81 | user_audit_logs |
82 | where |
83 | user_audit_logs.user_id = :user_id |
84 | order by |
85 | user_audit_logs.sequence desc |
86 | limit |
87 | :count |
88 | offset |
89 | :index |
90 | |
91 | SQL, |
92 | [ |
93 | 'user_id' => $userId, |
94 | 'index' => $index, |
95 | 'count' => $count, |
96 | ] |
97 | ); |
98 | } |
99 | |
100 | /** |
101 | * Undocumented function |
102 | * |
103 | * @template TFieldArray of array{timestamp:string,event:string,info:string,ip_address:string,user_agent:string} |
104 | * @param string $userId |
105 | * @return DatabaseTableResult |
106 | * @phpstan-return DatabaseTableResult<TFieldArray> |
107 | */ |
108 | public function selectAuditLogsFromUserId(string $userId): DatabaseTableResult |
109 | { |
110 | /** @var DatabaseTableResult<TFieldArray> */ |
111 | return $this->context->selectOrdered( |
112 | <<<SQL |
113 | |
114 | select |
115 | user_audit_logs.timestamp, |
116 | user_audit_logs.event, |
117 | user_audit_logs.info, |
118 | user_audit_logs.ip_address, |
119 | user_audit_logs.user_agent |
120 | from |
121 | user_audit_logs |
122 | where |
123 | user_audit_logs.user_id = :user_id |
124 | order by |
125 | user_audit_logs.sequence |
126 | |
127 | SQL, |
128 | [ |
129 | 'user_id' => $userId, |
130 | ] |
131 | ); |
132 | } |
133 | |
134 | public function insertLog(string $userId, string $event, string $info, string $ipAddress, string $userAgent): void |
135 | { |
136 | $this->context->insertSingle( |
137 | <<<SQL |
138 | |
139 | insert into |
140 | user_audit_logs |
141 | ( |
142 | [user_id], |
143 | [timestamp], |
144 | [event], |
145 | [info], |
146 | [ip_address], |
147 | [user_agent] |
148 | ) |
149 | values |
150 | ( |
151 | :user_id, |
152 | CURRENT_TIMESTAMP, |
153 | :event, |
154 | :info, |
155 | :ip_address, |
156 | :user_agent |
157 | ) |
158 | |
159 | SQL, |
160 | [ |
161 | 'user_id' => $userId, |
162 | 'event' => $event, |
163 | 'info' => $info, |
164 | 'ip_address' => $ipAddress, |
165 | 'user_agent' => $userAgent, |
166 | ] |
167 | ); |
168 | } |
169 | |
170 | #endregion |
171 | } |