Move model events from eloquent closures to dedicated event classes

This commit is contained in:
Bubka 2021-11-30 17:34:35 +01:00
parent 97aefcbbe5
commit 0a22fb4cf1
7 changed files with 152 additions and 39 deletions

View File

@ -0,0 +1,26 @@
<?php
namespace App\Events;
use App\Group;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class GroupDeleting
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $group;
/**
* Create a new event instance.
*
* @param \App\Group $group
* @return void
*/
public function __construct(Group $group)
{
$this->group = $group;
}
}

View File

@ -0,0 +1,26 @@
<?php
namespace App\Events;
use App\TwoFAccount;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class TwoFAccountDeleted
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $twofaccount;
/**
* Create a new event instance.
*
* @param \App\TwoFAccount $twofaccount
* @return void
*/
public function __construct(TwoFAccount $twofaccount)
{
$this->twofaccount = $twofaccount;
}
}

View File

@ -2,6 +2,7 @@
namespace App;
use App\Events\GroupDeleting;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Log;
@ -42,6 +43,16 @@ class Group extends Model
];
/**
* The event map for the model.
*
* @var array
*/
protected $dispatchesEvents = [
'deleting' => GroupDeleting::class,
];
/**
* Override The "booting" method of the model
*
@ -50,13 +61,6 @@ class Group extends Model
protected static function boot()
{
parent::boot();
static::deleting(function ($model) {
TwoFAccount::where('group_id', $model->id)
->update(
['group_id' => NULL]
);
});
static::deleted(function ($model) {
Log::info(sprintf('Group %s deleted', var_export($model->name, true)));

View File

@ -0,0 +1,32 @@
<?php
namespace App\Listeners;
use App\Events\TwoFAccountDeleted;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
class CleanIconStorage
{
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* @param \App\Events\TwoFAccountDeleted $event
* @return void
*/
public function handle(TwoFAccountDeleted $event)
{
Storage::delete('public/icons/' . $event->twofaccount->icon);
Log::info(sprintf('Icon cleaned for deleted TwoFAccount #%d', $event->twofaccount->id));
}
}

View File

@ -0,0 +1,36 @@
<?php
namespace App\Listeners;
use App\TwoFAccount;
use App\Events\GroupDeleting;
use Illuminate\Support\Facades\Log;
class DissociateTwofaccountFromGroup
{
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* @param GroupDeleting $event
* @return void
*/
public function handle(GroupDeleting $event)
{
TwoFAccount::where('group_id', $event->group->id)
->update(
['group_id' => NULL]
);
Log::info(sprintf('TwoFAccounts dissociated from group #%d', $event->group->id));
}
}

View File

@ -18,6 +18,12 @@ class EventServiceProvider extends ServiceProvider
Registered::class => [
SendEmailVerificationNotification::class,
],
'App\Events\TwoFAccountDeleted' => [
'App\Listeners\CleanIconStorage',
],
'App\Events\GroupDeleting' => [
'App\Listeners\DissociateTwofaccountFromGroup',
],
];
/**

View File

@ -3,11 +3,11 @@
namespace App;
use Exception;
use App\Events\TwoFAccountDeleted;
use Facades\App\Services\SettingServiceInterface;
use Spatie\EloquentSortable\Sortable;
use Spatie\EloquentSortable\SortableTrait;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Log;
@ -57,6 +57,16 @@ class TwoFAccount extends Model implements Sortable
protected $casts = [];
/**
* The event map for the model.
*
* @var array
*/
protected $dispatchesEvents = [
'deleted' => TwoFAccountDeleted::class,
];
/**
* Override The "booting" method of the model
*
@ -65,11 +75,10 @@ class TwoFAccount extends Model implements Sortable
protected static function boot()
{
parent::boot();
static::deleted(function ($model) {
Log::info(sprintf('TwoFAccount #%d deleted', $model->id));
Storage::delete('public/icons/' . $model->icon);
});
// static::deleted(function ($model) {
// Log::info(sprintf('TwoFAccount #%d deleted', $model->id));
// });
}
@ -84,32 +93,6 @@ protected static function boot()
];
/**
* Increment the hotp counter by 1
* @return void
*/
// public function increaseHotpCounter() : void
// {
// if( $this->otpType === 'hotp' ) {
// $this->counter = $this->counter + 1;
// $this->refreshUri();
// }
// }
/**
* Get is_deciphered attribute
*
* @return bool
*
*/
// public function getIsDecipheredAttribute()
// {
// $this->attributes['is_deciphered'] = $this->legacy_uri === self::INDECIPHERABLE || $this->account === self::INDECIPHERABLE || $this->secret === self::INDECIPHERABLE ? false : true;
// // $this->attributes['is_deciphered'] = 'toto';
// }
/**
* Get legacy_uri attribute
*