Last active
March 15, 2020 07:12
-
-
Save leeovery/793a0332c87a12e6bb370035f206d08a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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