PruneAuditsCommand.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. <?php
  2. namespace Webkul\BagistoApi\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use Webkul\BagistoApi\Admin\Models\AdminApiAudit;
  5. /**
  6. * Deletes admin-API audit history older than the configured retention period.
  7. * Schedule it (or run manually) to keep the audit table from growing forever.
  8. */
  9. class PruneAuditsCommand extends Command
  10. {
  11. protected $signature = 'bagisto-api:prune-audits {--days= : Override bagistoapi.audit.retention_days}';
  12. protected $description = 'Delete admin-API audit history older than the retention period.';
  13. public function handle(): int
  14. {
  15. $days = $this->option('days') ?? config('bagistoapi.audit.retention_days');
  16. if ($days === null || ! is_numeric($days)) {
  17. $this->info('No retention period configured (bagistoapi.audit.retention_days is null). Nothing pruned.');
  18. return self::SUCCESS;
  19. }
  20. $cutoff = now()->subDays((int) $days);
  21. $deleted = AdminApiAudit::where('created_at', '<', $cutoff)->delete();
  22. $this->info("Pruned {$deleted} audit row(s) older than {$days} day(s).");
  23. return self::SUCCESS;
  24. }
  25. }