Skip to content

Commit a9a802d

Browse files
author
farhadzand
committed
add log
1 parent 88ad708 commit a9a802d

File tree

5 files changed

+40
-14
lines changed

5 files changed

+40
-14
lines changed

phpstan.neon

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,30 @@ includes:
44
parameters:
55
level: 5
66
paths:
7-
- src
7+
- src/
8+
treatPhpDocTypesAsCertain: false
89

910
reportUnmatchedIgnoredErrors: false
1011

1112
ignoreErrors:
1213
# Add specific error patterns to ignore if needed
1314
- '#PHPDoc tag @var#'
14-
-
15-
identifier: missingType.iterableValue
15+
-
16+
message: '#Property [a-zA-Z0-9\\_\\$\\:\\ ]+ has no type specified.#'
17+
reportUnmatched: false
18+
-
19+
message: '#Parameter \$[a-zA-Z0-9\\_]+ of (method|function) [a-zA-Z0-9\\_\\:\\(\\)]+ has no type specified.#'
20+
reportUnmatched: false
21+
-
22+
message: '#(Method|Function) [a-zA-Z0-9\\_\\:\\(\\)]+ has no return type specified.#'
23+
reportUnmatched: false
24+
-
25+
message: '#Call to an undefined static method Illuminate\\Support\\Facades\\Event::(fire|dispatch)\(\)#'
26+
reportUnmatched: false
27+
-
28+
message: '#Call to an undefined static method Illuminate\\Support\\Facades\\DB::(statement|transaction|beginTransaction|commit|rollBack|insert|raw)\(\)#'
29+
reportUnmatched: false
1630
-
1731
identifier: missingType.generics
32+
reportUnmatched: false
1833
- '#Construct empty\(\) is not allowed. Use more strict comparison.#'

src/Contracts/AuditDriverInterface.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ interface AuditDriverInterface
1111
*/
1212
public function store(AuditLogInterface $log): void;
1313

14+
/**
15+
* Store multiple audit log entries.
16+
*
17+
* @param array<AuditLogInterface> $logs
18+
*/
19+
public function storeBatch(array $logs): void;
20+
1421
/**
1522
* Create storage for a new entity type if needed.
1623
*/

src/Drivers/MySQLDriver.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ public function store(AuditLogInterface $log): void
3636
$oldValues = $log->getOldValues();
3737
$newValues = $log->getNewValues();
3838

39-
EloquentAuditLog::forEntity(entityClass: $log->getEntityType())->create([
39+
$model = EloquentAuditLog::forEntity(entityClass: $log->getEntityType());
40+
$model->fill([
4041
'entity_id' => $log->getEntityId(),
4142
'action' => $log->getAction(),
4243
'old_values' => $oldValues !== null ? json_encode($oldValues) : null,
@@ -46,24 +47,20 @@ public function store(AuditLogInterface $log): void
4647
'metadata' => json_encode($log->getMetadata()),
4748
'created_at' => $log->getCreatedAt(),
4849
]);
50+
$model->save();
4951
} catch (\Exception $e) {
5052
throw $e;
5153
}
5254
}
5355

5456
/**
55-
* Legacy method to maintain interface compatibility.
56-
* Simply stores logs one by one instead of in batch.
57+
* Store multiple audit logs.
5758
*
5859
* @param array<AuditLogInterface> $logs
5960
*/
6061
public function storeBatch(array $logs): void
6162
{
6263
foreach ($logs as $log) {
63-
if (! $log instanceof AuditLogInterface) {
64-
throw new \InvalidArgumentException('Log must be an instance of AuditLogInterface');
65-
}
66-
6764
$this->store($log);
6865
}
6966
}

src/Models/EloquentAuditLog.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ final class EloquentAuditLog extends Model
2121
/**
2222
* The attributes that are mass assignable.
2323
*
24-
* @var array<string>
24+
* @var array<int, string>
2525
*/
2626
protected $fillable = [
2727
'entity_id',
@@ -92,7 +92,10 @@ public function scopeDateLessThan(Builder $query, $date): Builder
9292

9393
public function scopeDateBetween(Builder $query, $startDate, $endDate): Builder
9494
{
95-
return $query->whereBetween('created_at', [$startDate, $endDate]);
95+
return $query->where(function (Builder $query) use ($startDate, $endDate) {
96+
$query->where('created_at', '>=', $startDate)
97+
->where('created_at', '<=', $endDate);
98+
});
9699
}
97100

98101
public static function forEntity(string $entityClass): static

src/Services/AuditBuilder.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,12 @@ public function withMetadata(array $metadata): self
8787
*/
8888
public function log(): void
8989
{
90-
// Merge model metadata with custom metadata
91-
$metadata = array_merge($this->model->getAuditMetadata(), $this->metadata);
90+
// Merge model metadata with custom metadata if available
91+
$modelMetadata = method_exists($this->model, 'getAuditMetadata')
92+
? $this->model->getAuditMetadata()
93+
: [];
94+
95+
$metadata = array_merge($modelMetadata, $this->metadata);
9296

9397
// If the model has getAuditableAttributes method, filter values
9498
if (method_exists($this->model, 'getAuditableAttributes')) {

0 commit comments

Comments
 (0)