Browse Source

Move model events from eloquent closures to dedicated event classes

Bubka 3 năm trước cách đây
mục cha
commit
0a22fb4cf1

+ 26 - 0
app/Events/GroupDeleting.php

@@ -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;
+    }
+}

+ 26 - 0
app/Events/TwoFAccountDeleted.php

@@ -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;
+    }
+}

+ 11 - 7
app/Group.php

@@ -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)));

+ 32 - 0
app/Listeners/CleanIconStorage.php

@@ -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));
+    }
+}

+ 36 - 0
app/Listeners/DissociateTwofaccountFromGroup.php

@@ -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));
+    }
+}

+ 6 - 0
app/Providers/EventServiceProvider.php

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

+ 15 - 32
app/TwoFAccount.php

@@ -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 @@ class TwoFAccount extends Model implements Sortable
     ];
 
 
-    /**
-     * 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
      *