Skip to content

Commit

Permalink
feat: create listeners and events (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
asbiin authored Feb 24, 2024
1 parent acbed8e commit d285a7a
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 6 deletions.
22 changes: 22 additions & 0 deletions app/Events/Favorited.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace App\Events;

use App\Models\Name;
use App\Models\User;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class Favorited
{
use Dispatchable, SerializesModels;

/**
* Create a new event instance.
*/
public function __construct(
public User $user,
public Name $favorited
) {
}
}
5 changes: 0 additions & 5 deletions app/Http/Controllers/Auth/RegisteredUserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\Rule;
use Pirsch\Facades\Pirsch;

class RegisteredUserController extends Controller
{
Expand All @@ -33,10 +32,6 @@ public function create(): View
*/
public function store(Request $request): RedirectResponse
{
Pirsch::track('Action', [
'Label' => 'register',
]);

$request->validate([
'email' => [
'required',
Expand Down
3 changes: 3 additions & 0 deletions app/Http/Controllers/FavoriteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Controllers;

use App\Events\Favorited;
use App\Http\ViewModels\Names\NameViewModel;
use App\Http\ViewModels\User\UserViewModel;
use App\Services\ToggleNameToFavorites;
Expand All @@ -28,6 +29,8 @@ public function update(Request $request): View
nameId: $name->id,
))->execute();

Favorited::dispatch($request->user(), $name);

Cache::forget('user-favorites-' . auth()->id());
Cache::forget('user-favorites-details-' . auth()->id());

Expand Down
31 changes: 31 additions & 0 deletions app/Listeners/Authenticated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace App\Listeners;

use App\Models\User;
use Illuminate\Auth\Events\Authenticated as AuthenticatedEvent;
use Illuminate\Contracts\Queue\ShouldQueue;
use Pirsch\Facades\Pirsch;

class Authenticated implements ShouldQueue
{
/**
* Handle the event.
*/
public function handle(AuthenticatedEvent $event): void
{
if ($event->user instanceof User) {
Pirsch::track('Authenticated', [
'UserId' => $event->user->id,
]);
}
}

/**
* Determine whether the listener should be queued.
*/
public function shouldQueue(AuthenticatedEvent $event): bool
{
return $event->user instanceof User && ! $event->user->is_administrator;
}
}
21 changes: 21 additions & 0 deletions app/Listeners/Favorited.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace App\Listeners;

use App\Events\Favorited as FavoritedEvent;
use Pirsch\Facades\Pirsch;

class Favorited
{
/**
* Handle the event.
*/
public function handle(FavoritedEvent $event): void
{
Pirsch::track('Favorited', [
'UserId' => $event->user->id,
'Name' => $event->favorited->name,
'Id' => $event->favorited->id,
]);
}
}
24 changes: 24 additions & 0 deletions app/Listeners/Registered.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace App\Listeners;

use App\Models\User;
use Illuminate\Auth\Events\Registered as RegisteredEvent;
use Pirsch\Facades\Pirsch;

class Registered
{
/**
* Handle the event.
*
* @return void
*/
public function handle(RegisteredEvent $event)
{
if ($event->user instanceof User) {
Pirsch::track('Registered', [
'UserId' => $event->user->id,
]);
}
}
}
13 changes: 12 additions & 1 deletion app/Providers/EventServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

namespace App\Providers;

use App\Events\Favorited;
use App\Listeners\Authenticated as AuthenticatedListener;
use App\Listeners\Favorited as FavoritedListener;
use App\Listeners\Registered as RegisteredListener;
use Illuminate\Auth\Events\Authenticated;
use Illuminate\Auth\Events\Registered;
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Event;

class EventServiceProvider extends ServiceProvider
{
Expand All @@ -17,6 +21,13 @@ class EventServiceProvider extends ServiceProvider
protected $listen = [
Registered::class => [
SendEmailVerificationNotification::class,
RegisteredListener::class,
],
Favorited::class => [
FavoritedListener::class,
],
Authenticated::class => [
AuthenticatedListener::class,
],
];

Expand Down

0 comments on commit d285a7a

Please sign in to comment.