Skip to content

Instantly share code, notes, and snippets.

@iamshanto
Created August 26, 2019 09:58
Show Gist options
  • Save iamshanto/ae2088e618f7fc23031564c7d0225070 to your computer and use it in GitHub Desktop.
Save iamshanto/ae2088e618f7fc23031564c7d0225070 to your computer and use it in GitHub Desktop.
AuditLogService
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;
class Audit extends Model
{
protected $table = 'audits';
protected $fillable = ['user_type', 'user_id', 'event', 'auditable_id', 'auditable_type',
'old_values', 'new_values', 'url'];
protected $casts = [
'old_values' => 'json',
'new_values' => 'json',
];
public function prepareAndSave($event, $model, $old, $new)
{
$audit = new static();
$user = getLoggedInUser();
if ($user) {
$audit->user_id = $user->id;
$audit->user_type = get_class($user);
} else {
$audit->user_id = 0;
$audit->user_type = 'Anonymous';
}
$audit->event = $event;
$audit->auditable_id = $model->id;
$audit->auditable_type = get_class($model);
$audit->old_values = $old;
$audit->new_values = $new;
$audit->url = request()->fullUrl();
$audit->save();
}
public function modified()
{
$data = [];
foreach ($this->new_values as $key => $value) {
$data[$key] = ['new' => $value, 'old' => isset($this->old_values[$key]) ? $this->old_values[$key] : ''];
}
return $data;
}
}
<?php
namespace App\Providers;
use App\Observer\DynamicDiscountObserver;
use App\Observer\ModelObserver;
use App\User;
use Illuminate\Support\ServiceProvider;
use PhpParser\Node\Expr\AssignOp\Mod;
use App\CityServiceMapping;
class AuditServiceProvider extends ServiceProvider
{
/**
* This namespace is applied to your controller routes.
*
* In addition, it is set as the URL generator's root namespace.
*
* @var string
*/
protected $namespace = 'App\Http\Controllers';
/**
* Define your route model bindings, pattern filters, etc.
*
* @return void
*/
public function boot()
{
if ('cli' !== PHP_SAPI) {
User::observe(ModelObserver::class);
}
}
public function register()
{
//
}
}
<?php
namespace App\Observer;
use App\Audit;
use Illuminate\Database\Eloquent\Model;
class ModelObserver
{
public function created(Model $model)
{
$audit = new Audit();
$audit->prepareAndSave('created', $model, [], $this->sanitizeValues($model->getAttributes()));
}
/**
* Handle the User "updated" event.
*
* @param Model $model
* @return void
*/
public function updated(Model $model)
{
if (!$model->isDirty()) {
return;
}
if ($this->shouldIgnore($model)) {
return;
}
$audit = new Audit();
$old = [];
foreach ($model->getDirty() as $attribute => $value) {
$old[$attribute] = $model->getOriginal($attribute);
}
$audit->prepareAndSave('updated', $model, $this->sanitizeValues($old), $this->sanitizeValues($model->getDirty()));
}
/**
* Handle the User "deleted" event.
*
* @param Model $model
* @return void
*/
public function deleted(Model $model)
{
$audit = new Audit();
$audit->prepareAndSave('deleted', $model, $this->sanitizeValues($model->getAttributes()), []);
}
protected function sanitizeValues($data)
{
$output = [];
foreach ($data as $key => $value) {
if ($value instanceof \DateTime) {
$value = $value->format('Y-m-d H:i:s');
}
$output[$key] = $value;
}
return $output;
}
public function shouldIgnore($model)
{
$dirty = $model->getDirty();
if (count($dirty) == 1 && array_key_exists('due_amount', $dirty)) {
return true;
}
if (count($dirty) == 2 && array_key_exists('due_amount', $dirty) && array_key_exists('updated_at', $dirty)) {
return true;
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment