Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 39 additions & 39 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/Audit/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ abstract public function create(array $log): Log;
* time: \DateTime|string|null,
* data?: array<string, mixed>
* }> $logs
* @return array<Log>
* @return bool
*
* @throws \Exception
*/
abstract public function createBatch(array $logs): array;
abstract public function createBatch(array $logs): bool;

/**
* Get logs by user ID.
Expand Down
29 changes: 3 additions & 26 deletions src/Audit/Adapter/ClickHouse.php
Original file line number Diff line number Diff line change
Expand Up @@ -500,10 +500,10 @@ public function create(array $log): Log
*
* @throws Exception
*/
public function createBatch(array $logs): array
public function createBatch(array $logs): bool
{
if (empty($logs)) {
return [];
return true;
}

$tableName = $this->getTableName();
Expand Down Expand Up @@ -579,30 +579,7 @@ public function createBatch(array $logs): array
VALUES " . implode(', ', $valueClauses);

$this->query($insertSql, $params);

// Return documents using the same IDs that were inserted
$documents = [];
foreach ($logs as $index => $log) {
$result = [
'$id' => $ids[$index],
'userId' => $log['userId'] ?? null,
'event' => $log['event'],
'resource' => $log['resource'],
'userAgent' => $log['userAgent'],
'ip' => $log['ip'],
'location' => $log['location'] ?? null,
'time' => $log['time'],
'data' => $log['data'] ?? [],
];

if ($this->sharedTables) {
$result['tenant'] = $this->tenant;
}

$documents[] = new Log($result);
}

return $documents;
return true;
}

/**
Expand Down
25 changes: 12 additions & 13 deletions src/Audit/Adapter/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,26 +81,25 @@ public function create(array $log): Log
* Create multiple audit log entries in batch.
*
* @param array<int, array<string, mixed>> $logs
* @return array<Log>
* @return bool
* @throws AuthorizationException|\Exception
*/
public function createBatch(array $logs): array
public function createBatch(array $logs): bool
{
$created = [];

$this->db->getAuthorization()->skip(function () use ($logs, &$created) {
foreach ($logs as $log) {
$this->db->getAuthorization()->skip(function () use ($logs) {
$documents = \array_map(function ($log) {
$time = $log['time'] ?? new \DateTime();
if (is_string($time)) {
$time = new \DateTime($time);
}
assert($time instanceof \DateTime);
$log['time'] = DateTime::format($time);
$created[] = $this->db->createDocument($this->getCollectionName(), new Document($log));
}
return new Document($log);
}, $logs);
$this->db->createDocuments($this->getCollectionName(), $documents);
});

return array_map(fn ($doc) => new Log($doc->getArrayCopy()), $created);
return true;
}

/**
Expand Down Expand Up @@ -152,7 +151,7 @@ public function getByUser(
$queries = [
Query::equal('userId', [$userId]),
...$timeQueries,
$ascending ? Query::orderAsc('time') : Query::orderDesc('time'),
$ascending ? Query::orderAsc() : Query::orderDesc(),
Query::limit($limit),
Query::offset($offset),
];
Expand Down Expand Up @@ -208,7 +207,7 @@ public function getByResource(
$queries = [
Query::equal('resource', [$resource]),
...$timeQueries,
$ascending ? Query::orderAsc('time') : Query::orderDesc('time'),
$ascending ? Query::orderAsc() : Query::orderDesc(),
Query::limit($limit),
Query::offset($offset),
];
Expand Down Expand Up @@ -269,7 +268,7 @@ public function getByUserAndEvents(
Query::equal('userId', [$userId]),
Query::equal('event', $events),
...$timeQueries,
$ascending ? Query::orderAsc('time') : Query::orderDesc('time'),
$ascending ? Query::orderAsc() : Query::orderDesc(),
Query::limit($limit),
Query::offset($offset),
];
Expand Down Expand Up @@ -333,7 +332,7 @@ public function getByResourceAndEvents(
Query::equal('resource', [$resource]),
Query::equal('event', $events),
...$timeQueries,
$ascending ? Query::orderAsc('time') : Query::orderDesc('time'),
$ascending ? Query::orderAsc() : Query::orderDesc(),
Query::limit($limit),
Query::offset($offset),
];
Expand Down
10 changes: 5 additions & 5 deletions src/Audit/Adapter/SQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ abstract class SQL extends Adapter
*
* @return string
*/
protected function getCollectionName(): string
public function getCollectionName(): string
{
return self::COLLECTION;
}
Expand All @@ -40,7 +40,7 @@ protected function getCollectionName(): string
*
* @return array<int, array<string, mixed>>
*/
protected function getAttributes(): array
public function getAttributes(): array
{
return [
[
Expand Down Expand Up @@ -124,7 +124,7 @@ protected function getAttributes(): array
*
* @return array<Document>
*/
protected function getAttributeDocuments(): array
public function getAttributeDocuments(): array
{
return array_map(static fn (array $attribute) => new Document($attribute), $this->getAttributes());
}
Expand All @@ -139,7 +139,7 @@ protected function getAttributeDocuments(): array
*
* @return array<int, array<string, mixed>>
*/
protected function getIndexes(): array
public function getIndexes(): array
{
return [
[
Expand Down Expand Up @@ -170,7 +170,7 @@ protected function getIndexes(): array
*
* @return array<Document>
*/
protected function getIndexDocuments(): array
public function getIndexDocuments(): array
{
return array_map(static fn (array $index) => new Document($index), $this->getIndexes());
}
Expand Down
4 changes: 2 additions & 2 deletions src/Audit/Audit.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ public function log(?string $userId, string $event, string $resource, string $us
* Add multiple event logs in batch.
*
* @param array<array{userId: string|null, event: string, resource: string, userAgent: string, ip: string, location: string, time: string, data?: array<string, mixed>}> $events
* @return array<Log>
* @return bool
*
* @throws \Exception
*/
public function logBatch(array $events): array
public function logBatch(array $events): bool
{
return $this->adapter->createBatch($events);
}
Expand Down
Loading