Skip to content

Commit fa39929

Browse files
author
farhadzand
committed
update github action and fix code style
1 parent a2db5b5 commit fa39929

File tree

14 files changed

+135
-88
lines changed

14 files changed

+135
-88
lines changed

.github/workflows/coding-standards.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ on:
66
pull_request:
77
branches: [ main, master ]
88

9+
env:
10+
COMPOSER_PROCESS_TIMEOUT: 0
11+
COMPOSER_NO_INTERACTION: 1
12+
COMPOSER_NO_AUDIT: 1
13+
914
jobs:
1015
lint:
1116
name: PHP CS & Static Analysis
@@ -21,6 +26,24 @@ jobs:
2126
php-version: 8.2
2227
extensions: dom, curl, libxml, mbstring, zip, pcntl, bcmath, intl
2328
coverage: none
29+
tools: composer:v2
30+
31+
- name: Setup problem matchers
32+
run: |
33+
echo "::add-matcher::/opt/hostedtoolcache/php.json"
34+
shell: /usr/bin/bash -e {0}
35+
36+
- name: Get composer cache directory
37+
id: composer-cache
38+
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT
39+
40+
- name: Cache dependencies
41+
uses: actions/cache@v3
42+
with:
43+
path: ${{ steps.composer-cache.outputs.dir }}
44+
key: ${{ runner.os }}-composer-8.2-${{ hashFiles('**/composer.json') }}
45+
restore-keys: |
46+
${{ runner.os }}-composer-8.2-
2447
2548
- name: Install dependencies
2649
run: composer update --prefer-dist --no-interaction --no-progress

.github/workflows/tests.yml

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ on:
88
schedule:
99
- cron: '0 0 * * *' # Run daily at midnight
1010

11+
env:
12+
COMPOSER_PROCESS_TIMEOUT: 0
13+
COMPOSER_NO_INTERACTION: 1
14+
COMPOSER_NO_AUDIT: 1
15+
1116
jobs:
1217
tests:
1318
runs-on: ubuntu-latest
@@ -37,11 +42,26 @@ jobs:
3742
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, intl, exif, iconv
3843
coverage: xdebug
3944
ini-values: error_reporting=E_ALL
45+
tools: composer:v2
4046

4147
- name: Setup problem matchers
4248
run: |
43-
echo "::add-matcher::${{ runner.tool_cache }}/php.json"
44-
echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"
49+
echo "::add-matcher::/opt/hostedtoolcache/php.json"
50+
echo "::add-matcher::/opt/hostedtoolcache/phpunit.json"
51+
shell: /usr/bin/bash -e {0}
52+
53+
- name: Get composer cache directory
54+
id: composer-cache
55+
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT
56+
57+
- name: Cache dependencies
58+
uses: actions/cache@v3
59+
with:
60+
path: ${{ steps.composer-cache.outputs.dir }}
61+
key: ${{ runner.os }}-composer-${{ matrix.php }}-${{ matrix.laravel }}-${{ hashFiles('**/composer.json') }}-${{ matrix.stability }}
62+
restore-keys: |
63+
${{ runner.os }}-composer-${{ matrix.php }}-${{ matrix.laravel }}-${{ hashFiles('**/composer.json') }}-
64+
${{ runner.os }}-composer-${{ matrix.php }}-
4565
4666
- name: Install dependencies
4767
run: |
@@ -54,5 +74,5 @@ jobs:
5474
- name: Run PHP Static Analysis
5575
run: |
5676
if [ -f vendor/bin/phpstan ]; then
57-
vendor/bin/phpstan analyze
77+
vendor/bin/phpstan analyse
5878
fi

src/AuditLoggerServiceProvider.php

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,30 @@
44

55
namespace iamfarhad\LaravelAuditLog;
66

7-
use iamfarhad\LaravelAuditLog\Commands\CreateAuditTablesCommand;
8-
use iamfarhad\LaravelAuditLog\Contracts\CauserResolverInterface;
7+
use Illuminate\Support\ServiceProvider;
98
use iamfarhad\LaravelAuditLog\Events\ModelAudited;
10-
use iamfarhad\LaravelAuditLog\Listeners\AuditModelChanges;
119
use iamfarhad\LaravelAuditLog\Services\AuditLogger;
12-
use iamfarhad\LaravelAuditLog\Services\CauserResolver;
13-
use Illuminate\Support\ServiceProvider;
1410
use Illuminate\Support\Facades\Event as EventFacade;
11+
use iamfarhad\LaravelAuditLog\Services\CauserResolver;
12+
use iamfarhad\LaravelAuditLog\Listeners\AuditModelChanges;
13+
use iamfarhad\LaravelAuditLog\Contracts\CauserResolverInterface;
1514

16-
class AuditLoggerServiceProvider extends ServiceProvider
15+
final class AuditLoggerServiceProvider extends ServiceProvider
1716
{
1817
/**
1918
* Register services.
2019
*/
2120
public function register(): void
2221
{
2322
$this->mergeConfigFrom(
24-
__DIR__ . '/Config/audit-logger.php',
23+
__DIR__.'/Config/audit-logger.php',
2524
'audit-logger'
2625
);
2726

2827
// Register the causer resolver
2928
$this->app->singleton(
3029
CauserResolverInterface::class,
31-
fn($app) =>
32-
isset($app['config']['audit-logger.causer']['resolver']) && $app['config']['audit-logger.causer']['resolver']
30+
fn ($app) => isset($app['config']['audit-logger.causer']['resolver']) && $app['config']['audit-logger.causer']['resolver']
3331
? $app->make($app['config']['audit-logger.causer']['resolver'])
3432
: new CauserResolver(
3533
guard: $app['config']['audit-logger.causer']['guard'] ?? null,
@@ -38,7 +36,7 @@ public function register(): void
3836
);
3937

4038
// Register the main audit logger service
41-
$this->app->singleton('audit-logger', fn($app) => new AuditLogger(
39+
$this->app->singleton('audit-logger', fn ($app) => new AuditLogger(
4240
causerResolver: $app->make(CauserResolverInterface::class),
4341
config: $app['config']['audit-logger']
4442
));
@@ -54,7 +52,7 @@ public function boot(): void
5452
// Publish config
5553
if ($this->app->runningInConsole()) {
5654
$this->publishes([
57-
__DIR__ . '/Config/audit-logger.php' => config_path('audit-logger.php'),
55+
__DIR__.'/Config/audit-logger.php' => config_path('audit-logger.php'),
5856
], 'audit-logger-config');
5957
}
6058

src/Drivers/MongoDBDriver.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,19 @@
44

55
namespace iamfarhad\LaravelAuditLog\Drivers;
66

7-
use iamfarhad\LaravelAuditLog\Contracts\AuditDriverInterface;
8-
use iamfarhad\LaravelAuditLog\Contracts\AuditLogInterface;
9-
use iamfarhad\LaravelAuditLog\Models\AuditLog;
107
use Illuminate\Support\Str;
11-
use Illuminate\Support\Carbon;
12-
use Illuminate\Database\Connection;
138
use MongoDB\BSON\UTCDateTime;
9+
use Illuminate\Database\Connection;
10+
use iamfarhad\LaravelAuditLog\Models\AuditLog;
11+
use iamfarhad\LaravelAuditLog\Contracts\AuditLogInterface;
12+
use iamfarhad\LaravelAuditLog\Contracts\AuditDriverInterface;
1413

1514
final class MongoDBDriver implements AuditDriverInterface
1615
{
1716
private Connection $connection;
17+
1818
private string $collectionPrefix;
19+
1920
private string $collectionSuffix;
2021

2122
public function __construct(array $config = [])
@@ -45,8 +46,8 @@ public function store(AuditLogInterface $log): void
4546
/**
4647
* Legacy method to maintain interface compatibility.
4748
* Simply stores logs one by one instead of in batch.
48-
*
49-
* @param array<AuditLogInterface> $logs
49+
*
50+
* @param array<AuditLogInterface> $logs
5051
*/
5152
public function storeBatch(array $logs): void
5253
{
@@ -137,7 +138,7 @@ public function ensureStorageExists(string $entityClass): void
137138
{
138139
// MongoDB collections are created automatically when data is inserted
139140
// No need to explicitly create them, just check if auto_migration is enabled
140-
if (!config('audit-logger.auto_migration', true)) {
141+
if (! config('audit-logger.auto_migration', true)) {
141142
return;
142143
}
143144

@@ -148,6 +149,7 @@ private function getCollectionName(string $entityType): string
148149
{
149150
$baseName = Str::snake(class_basename($entityType));
150151
$pluralName = Str::plural($baseName);
151-
return $this->collectionPrefix . $pluralName . $this->collectionSuffix;
152+
153+
return $this->collectionPrefix.$pluralName.$this->collectionSuffix;
152154
}
153155
}

src/Drivers/MySQLDriver.php

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,23 @@
44

55
namespace iamfarhad\LaravelAuditLog\Drivers;
66

7-
use iamfarhad\LaravelAuditLog\Contracts\AuditDriverInterface;
8-
use iamfarhad\LaravelAuditLog\Contracts\AuditLogInterface;
9-
use iamfarhad\LaravelAuditLog\Models\AuditLog;
10-
use Illuminate\Database\Connection;
11-
use Illuminate\Database\Query\Builder;
12-
use Illuminate\Database\Schema\Blueprint;
7+
use Illuminate\Support\Str;
138
use Illuminate\Support\Carbon;
14-
use Illuminate\Support\Facades\DB;
9+
use Illuminate\Database\Connection;
1510
use Illuminate\Support\Facades\Log;
11+
use Illuminate\Database\Query\Builder;
1612
use Illuminate\Support\Facades\Schema;
17-
use Illuminate\Support\Str;
13+
use Illuminate\Database\Schema\Blueprint;
14+
use iamfarhad\LaravelAuditLog\Models\AuditLog;
15+
use iamfarhad\LaravelAuditLog\Contracts\AuditLogInterface;
16+
use iamfarhad\LaravelAuditLog\Contracts\AuditDriverInterface;
1817

1918
final class MySQLDriver implements AuditDriverInterface
2019
{
2120
private Connection $connection;
21+
2222
private string $tablePrefix;
23+
2324
private string $tableSuffix;
2425

2526
public function __construct(array $config = [])
@@ -35,7 +36,7 @@ public function store(AuditLogInterface $log): void
3536
Log::info('Entering store method for audit log', [
3637
'entity_type' => $log->getEntityType(),
3738
'entity_id' => $log->getEntityId(),
38-
'action' => $log->getAction()
39+
'action' => $log->getAction(),
3940
]);
4041

4142
$tableName = $this->getTableName($log->getEntityType());
@@ -54,14 +55,14 @@ public function store(AuditLogInterface $log): void
5455
Log::debug('Audit log inserted into database', [
5556
'table' => $tableName,
5657
'entity_id' => $log->getEntityId(),
57-
'action' => $log->getAction()
58+
'action' => $log->getAction(),
5859
]);
5960
} catch (\Exception $e) {
6061
Log::error('Failed to store audit log in database', [
6162
'table' => $tableName,
6263
'entity_id' => $log->getEntityId(),
6364
'error' => $e->getMessage(),
64-
'trace' => $e->getTraceAsString()
65+
'trace' => $e->getTraceAsString(),
6566
]);
6667
throw $e;
6768
}
@@ -70,8 +71,8 @@ public function store(AuditLogInterface $log): void
7071
/**
7172
* Legacy method to maintain interface compatibility.
7273
* Simply stores logs one by one instead of in batch.
73-
*
74-
* @param array<AuditLogInterface> $logs
74+
*
75+
* @param array<AuditLogInterface> $logs
7576
*/
7677
public function storeBatch(array $logs): void
7778
{
@@ -84,7 +85,7 @@ public function getLogsForEntity(string $entityType, string|int $entityId, array
8485
{
8586
$tableName = $this->getTableName($entityType);
8687

87-
if (!Schema::hasTable($tableName)) {
88+
if (! Schema::hasTable($tableName)) {
8889
return [];
8990
}
9091

@@ -148,11 +149,11 @@ public function storageExistsForEntity(string $entityClass): bool
148149
*/
149150
public function ensureStorageExists(string $entityClass): void
150151
{
151-
if (!config('audit-logger.auto_migration', true)) {
152+
if (! config('audit-logger.auto_migration', true)) {
152153
return;
153154
}
154155

155-
if (!$this->storageExistsForEntity($entityClass)) {
156+
if (! $this->storageExistsForEntity($entityClass)) {
156157
$this->createStorageForEntity($entityClass);
157158
}
158159
}
@@ -165,7 +166,7 @@ private function getTableName(string $entityType): string
165166
// Handle pluralization
166167
$tableName = Str::plural($className);
167168

168-
return $this->tablePrefix . $tableName . $this->tableSuffix;
169+
return $this->tablePrefix.$tableName.$this->tableSuffix;
169170
}
170171

171172
private function applyFilters(Builder $query, array $options): void

src/Events/ModelAudited.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@
44

55
namespace iamfarhad\LaravelAuditLog\Events;
66

7-
use Illuminate\Broadcasting\InteractsWithSockets;
7+
use Illuminate\Queue\SerializesModels;
88
use Illuminate\Database\Eloquent\Model;
99
use Illuminate\Foundation\Events\Dispatchable;
10-
use Illuminate\Queue\SerializesModels;
10+
use Illuminate\Broadcasting\InteractsWithSockets;
1111

1212
final class ModelAudited
1313
{
14-
use Dispatchable, InteractsWithSockets, SerializesModels;
14+
use Dispatchable;
15+
use InteractsWithSockets;
16+
use SerializesModels;
1517

1618
public function __construct(
1719
public readonly Model $model,

src/Facades/AuditLogger.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
*
1818
* @see \iamfarhad\LaravelAuditLog\Services\AuditLogger
1919
*/
20-
class AuditLogger extends Facade
20+
final class AuditLogger extends Facade
2121
{
2222
protected static function getFacadeAccessor(): string
2323
{

src/Listeners/AuditModelChanges.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
namespace iamfarhad\LaravelAuditLog\Listeners;
66

7+
use Illuminate\Support\Facades\Log;
78
use iamfarhad\LaravelAuditLog\Events\ModelAudited;
89
use iamfarhad\LaravelAuditLog\Services\AuditLogger;
9-
use Illuminate\Support\Facades\Log;
1010

1111
final class AuditModelChanges
1212
{
@@ -21,19 +21,20 @@ public function handle(ModelAudited $event): void
2121
{
2222
Log::debug('AuditModelChanges listener triggered', [
2323
'model' => get_class($event->model),
24-
'action' => $event->action
24+
'action' => $event->action,
2525
]);
2626

2727
try {
2828
$model = $event->model;
2929

3030
// Check if model has required methods from Auditable trait
3131
if (
32-
!method_exists($model, 'getAuditableAttributes') ||
33-
!method_exists($model, 'getAuditEntityType') ||
34-
!method_exists($model, 'getAuditMetadata')
32+
! method_exists($model, 'getAuditableAttributes') ||
33+
! method_exists($model, 'getAuditEntityType') ||
34+
! method_exists($model, 'getAuditMetadata')
3535
) {
3636
Log::warning('Model missing required audit methods', ['model' => get_class($model)]);
37+
3738
return;
3839
}
3940

@@ -44,13 +45,14 @@ public function handle(ModelAudited $event): void
4445
// Skip if no changes after filtering
4546
if ($event->action === 'updated' && empty($newValues)) {
4647
Log::debug('No changes after filtering, skipping audit log');
48+
4749
return;
4850
}
4951

5052
// Log the audit
5153
Log::debug('Calling AuditLogger to log audit event', [
5254
'entity_type' => $model->getAuditEntityType(),
53-
'entity_id' => $model->getKey()
55+
'entity_id' => $model->getKey(),
5456
]);
5557
$this->auditLogger->log(
5658
entityType: $model->getAuditEntityType(),
@@ -63,13 +65,13 @@ public function handle(ModelAudited $event): void
6365

6466
Log::debug('Audit log stored', [
6567
'entity' => $model->getAuditEntityType(),
66-
'id' => $model->getKey()
68+
'id' => $model->getKey(),
6769
]);
6870
} catch (\Throwable $e) {
6971
Log::error('Audit log failed', [
7072
'error' => $e->getMessage(),
7173
'model' => $model->getKey() ?? 'unknown',
72-
'trace' => $e->getTraceAsString()
74+
'trace' => $e->getTraceAsString(),
7375
]);
7476
throw $e;
7577
}

0 commit comments

Comments
 (0)