Skip to content

Instantly share code, notes, and snippets.

@leeovery
Last active March 15, 2020 07:12
Show Gist options
  • Save leeovery/793a0332c87a12e6bb370035f206d08a to your computer and use it in GitHub Desktop.
Save leeovery/793a0332c87a12e6bb370035f206d08a to your computer and use it in GitHub Desktop.
<?php /** @noinspection PhpUndefinedMethodInspection */
namespace App\Console\Commands;
use Altek\Accountant\Context;
use Altek\Accountant\Notary;
use App\Models\Audit;
use App\Models\Ledger;
use Illuminate\Console\Command;
class MigrateAuditsToLedgers extends Command
{
protected $signature = 'migrate-audits-to-ledgers';
public function handle()
{
$bar = $this->output->createProgressBar(Audit::count());
$bar->start();
Audit::cursor()->each(function ($audit) use ($bar) {
$url = $audit->url;
$context = Context::WEB;
if ($url === 'console') {
$url = 'Command Line Interface';
$context = Context::CLI;
}
$extra = [];
if (filled($audit->old_values)) {
$extra = [
'audit' => [
'old_values' => $audit->old_values,
],
];
}
Ledger::unguard();
$ledger = new Ledger([
'user_type' => $audit->user_type,
'user_id' => $audit->user_id,
'recordable_type' => $audit->auditable_type,
'recordable_id' => $audit->auditable_id,
'event' => $audit->event,
'url' => $url,
'context' => $context,
'properties' => $audit->new_values,
'modified' => array_keys($audit->new_values),
'pivot' => [],
'extra' => $extra,
'ip_address' => $audit->ip_address,
'user_agent' => $audit->user_agent,
'created_at' => $audit->created_at,
'updated_at' => $audit->updated_at,
]);
$ledger->signature = (new Notary)->sign($ledger->attributesToArray());
$ledger->save();
$bar->advance();
});
$bar->finish();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment