From 528d3b4caf30347a85d8f3641cf01747f122265d Mon Sep 17 00:00:00 2001 From: Bruno Fontes Date: Sun, 30 Sep 2018 21:32:33 -0300 Subject: [PATCH] Refactoring: TakeController returned item Mail moved to a listener The TakeController was manually sending the email message as well as dealing with item return, so I moved the email message to it's own listener, I created an event for it and moved the return part to the item model. --- app/Events/ReturnItem.php | 38 +++++++++++++++++++++++ app/Http/Controllers/TakeController.php | 17 ++--------- app/Item.php | 12 ++++++++ app/Listeners/AlertReturnedItem.php | 40 +++++++++++++++++++++++++ app/Providers/EventServiceProvider.php | 6 ++++ 5 files changed, 99 insertions(+), 14 deletions(-) create mode 100644 app/Events/ReturnItem.php create mode 100644 app/Listeners/AlertReturnedItem.php diff --git a/app/Events/ReturnItem.php b/app/Events/ReturnItem.php new file mode 100644 index 0000000..e580c7e --- /dev/null +++ b/app/Events/ReturnItem.php @@ -0,0 +1,38 @@ +item = $item; + } + + /** + * Get the channels the event should broadcast on. + * + * @return \Illuminate\Broadcasting\Channel|array + */ + public function broadcastOn() + { + return new PrivateChannel('channel-name'); + } +} diff --git a/app/Http/Controllers/TakeController.php b/app/Http/Controllers/TakeController.php index 2333783..2822684 100644 --- a/app/Http/Controllers/TakeController.php +++ b/app/Http/Controllers/TakeController.php @@ -4,8 +4,8 @@ namespace App\Http\Controllers; use \App\Item; use \App\User; -use App\Mail\ItemAvailable; use Illuminate\Http\Request; +use App\Events\ReturnItem; class TakeController extends Controller { @@ -27,19 +27,8 @@ class TakeController extends Controller public function delete(Request $request) { $item = User::loggedIn()->items()->find(request('item')); - $waiting_id = $item->waiting_user_id; - $item->used_by = null; - $item->waiting_user_id = null; - $item->save(); - - //Send e-mail to waiting user - if ($waiting_id) { - $user = User::find($waiting_id); - \Mail::to($user)->send( - new ItemAvailable($user->name, $item) - ); - } - + event(new ReturnItem($item)); + $item->returnItem(); return redirect('home'); } } diff --git a/app/Item.php b/app/Item.php index 5101437..64f2bba 100644 --- a/app/Item.php +++ b/app/Item.php @@ -38,4 +38,16 @@ class Item extends Model { return (new static)->where('user_id', \Auth::id()); } + + /** + * Return a specified item + * + * @return void + */ + public function returnItem() + { + $this->used_by = null; + $this->waiting_user_id = null; + $this->save(); + } } diff --git a/app/Listeners/AlertReturnedItem.php b/app/Listeners/AlertReturnedItem.php new file mode 100644 index 0000000..f3630dc --- /dev/null +++ b/app/Listeners/AlertReturnedItem.php @@ -0,0 +1,40 @@ +item->waiting_user_id) { + $user = User::find($event->item->waiting_user_id); + Mail::to($user)->send( + new ItemAvailable($user->name, $event->item) + ); + } + } +} diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php index 6c64e52..3d41953 100644 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -6,6 +6,8 @@ use Illuminate\Support\Facades\Event; use Illuminate\Auth\Events\Registered; use Illuminate\Auth\Listeners\SendEmailVerificationNotification; use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider; +use App\Events\ReturnItem; +use App\Listeners\AlertReturnedItem; class EventServiceProvider extends ServiceProvider { @@ -18,6 +20,10 @@ class EventServiceProvider extends ServiceProvider Registered::class => [ SendEmailVerificationNotification::class, ], + + ReturnItem::class => [ + AlertReturnedItem::class, + ], ]; /**