-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
265 additions
and
337 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
namespace App\Events\Activity; | ||
|
||
use Illuminate\Broadcasting\InteractsWithSockets; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Foundation\Events\Dispatchable; | ||
use Illuminate\Queue\SerializesModels; | ||
|
||
class ActivityCreated | ||
{ | ||
use Dispatchable, InteractsWithSockets, SerializesModels; | ||
|
||
public Model $model; | ||
|
||
/** | ||
* Create a new event instance. | ||
* | ||
* @return void | ||
*/ | ||
public function __construct(Model $model) | ||
{ | ||
$this->model = $model; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
namespace App\Events\Activity; | ||
|
||
use Illuminate\Broadcasting\InteractsWithSockets; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Foundation\Events\Dispatchable; | ||
use Illuminate\Queue\SerializesModels; | ||
|
||
class ActivityDeleted | ||
{ | ||
use Dispatchable, InteractsWithSockets, SerializesModels; | ||
|
||
public Model $model; | ||
|
||
/** | ||
* Create a new event instance. | ||
* | ||
* @return void | ||
*/ | ||
public function __construct(Model $model) | ||
{ | ||
$this->model = $model; | ||
} | ||
} |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
namespace App\Listeners; | ||
|
||
use App\Events\Activity\ActivityCreated; | ||
use App\Events\Activity\ActivityDeleted; | ||
use App\Models\Activity; | ||
use Illuminate\Events\Dispatcher; | ||
use Illuminate\Support\Facades\Auth; | ||
|
||
class ActivityEventSubscriber | ||
{ | ||
/** | ||
* Handle user have new activity event. | ||
*/ | ||
public function saveActivity(ActivityCreated $event): void { | ||
Auth::user()->activity()->create([ | ||
'subject_type' => get_class($event->model), | ||
'subject_id' => $event->model->id | ||
]); | ||
} | ||
|
||
/** | ||
* Handle user delete activity event. | ||
*/ | ||
public function deleteActivity(ActivityDeleted $event): void { | ||
Activity::query() | ||
->where('subject_id', $event->model->id) | ||
->delete(); | ||
} | ||
|
||
/** | ||
* Register the listeners for the subscriber. | ||
* | ||
* @return array<string, string> | ||
*/ | ||
public function subscribe(Dispatcher $events): array | ||
{ | ||
return [ | ||
ActivityCreated::class => 'saveActivity', | ||
ActivityDeleted::class => 'deleteActivity', | ||
]; | ||
} | ||
} |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace App\Models\Traits; | ||
|
||
use App\Events\Activity\ActivityCreated; | ||
use App\Events\Activity\ActivityDeleted; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
trait HasActivity | ||
{ | ||
/** | ||
* The "booted" method of the model. | ||
*/ | ||
protected static function booted(): void | ||
{ | ||
static::created(function (Model $model) { | ||
ActivityCreated::dispatch($model); | ||
}); | ||
|
||
static::deleted(function (Model $model) { | ||
ActivityDeleted::dispatch($model); | ||
}); | ||
} | ||
} |
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
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
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.