From 007e8fbdf988e0722103a3ac47ea78928d3a628f Mon Sep 17 00:00:00 2001 From: Till Deeke Date: Mon, 10 Sep 2018 16:40:26 +0200 Subject: [PATCH] =?UTF-8?q?simplified=20checkout=20event=20handling=20per?= =?UTF-8?q?=20@uberbrady=E2=80=99s=20suggestion?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This generalizes the checkout events into the CheckoutableCheckedOut and CheckoutableCheckedIn events. --- app/Events/AccessoryCheckedOut.php | 32 --- app/Events/AssetCheckedIn.php | 33 ---- app/Events/AssetCheckedOut.php | 32 --- ...heckedIn.php => CheckoutableCheckedIn.php} | 13 +- ...ckedOut.php => CheckoutableCheckedOut.php} | 13 +- app/Events/ComponentCheckedIn.php | 35 ---- app/Events/ConsumableCheckedOut.php | 32 --- app/Events/LicenseSeatCheckedIn.php | 34 ---- app/Events/LicenseSeatCheckedOut.php | 33 ---- .../AccessoryCheckinController.php | 4 +- .../AccessoryCheckoutController.php | 4 +- .../Assets/AssetCheckinController.php | 4 +- .../Components/ComponentCheckinController.php | 5 +- .../ComponentCheckoutController.php | 3 +- .../ConsumableCheckoutController.php | 4 +- .../Licenses/LicenseCheckinController.php | 4 +- .../Licenses/LicenseCheckoutController.php | 9 +- app/Listeners/CheckoutableListener.php | 184 ++++++++++++++++++ app/Listeners/LogListener.php | 59 +----- .../SendingCheckInNotificationsListener.php | 117 ----------- .../SendingCheckOutNotificationsListener.php | 182 ----------------- app/Models/Asset.php | 3 +- app/Providers/EventServiceProvider.php | 8 +- 23 files changed, 229 insertions(+), 618 deletions(-) delete mode 100644 app/Events/AccessoryCheckedOut.php delete mode 100644 app/Events/AssetCheckedIn.php delete mode 100644 app/Events/AssetCheckedOut.php rename app/Events/{AccessoryCheckedIn.php => CheckoutableCheckedIn.php} (62%) rename app/Events/{ComponentCheckedOut.php => CheckoutableCheckedOut.php} (59%) delete mode 100644 app/Events/ComponentCheckedIn.php delete mode 100644 app/Events/ConsumableCheckedOut.php delete mode 100644 app/Events/LicenseSeatCheckedIn.php delete mode 100644 app/Events/LicenseSeatCheckedOut.php create mode 100644 app/Listeners/CheckoutableListener.php delete mode 100644 app/Listeners/SendingCheckInNotificationsListener.php delete mode 100644 app/Listeners/SendingCheckOutNotificationsListener.php diff --git a/app/Events/AccessoryCheckedOut.php b/app/Events/AccessoryCheckedOut.php deleted file mode 100644 index faf30adb36..0000000000 --- a/app/Events/AccessoryCheckedOut.php +++ /dev/null @@ -1,32 +0,0 @@ -accessory = $accessory; - $this->checkedOutTo = $checkedOutTo; - $this->checkedOutBy = $checkedOutBy; - $this->note = $note; - } -} diff --git a/app/Events/AssetCheckedIn.php b/app/Events/AssetCheckedIn.php deleted file mode 100644 index 455b2b5d4e..0000000000 --- a/app/Events/AssetCheckedIn.php +++ /dev/null @@ -1,33 +0,0 @@ -asset = $asset; - $this->checkedOutTo = $checkedOutTo; - $this->checkedInBy = $checkedInBy; - $this->note = $note; - } -} diff --git a/app/Events/AssetCheckedOut.php b/app/Events/AssetCheckedOut.php deleted file mode 100644 index 1cd0669ad8..0000000000 --- a/app/Events/AssetCheckedOut.php +++ /dev/null @@ -1,32 +0,0 @@ -asset = $asset; - $this->checkedOutTo = $checkedOutTo; - $this->checkedOutBy = $checkedOutBy; - $this->note = $note; - } -} diff --git a/app/Events/AccessoryCheckedIn.php b/app/Events/CheckoutableCheckedIn.php similarity index 62% rename from app/Events/AccessoryCheckedIn.php rename to app/Events/CheckoutableCheckedIn.php index 812e072b28..0f6a96491a 100644 --- a/app/Events/AccessoryCheckedIn.php +++ b/app/Events/CheckoutableCheckedIn.php @@ -2,30 +2,27 @@ namespace App\Events; -use App\Models\Accessory; -use App\Models\Actionlog; use App\Models\User; -use Illuminate\Broadcasting\Channel; use Illuminate\Foundation\Events\Dispatchable; use Illuminate\Queue\SerializesModels; -class AccessoryCheckedIn +class CheckoutableCheckedIn { use Dispatchable, SerializesModels; - public $accessory; + public $checkoutable; public $checkedOutTo; public $checkedInBy; public $note; - + /** * Create a new event instance. * * @return void */ - public function __construct(Accessory $accessory, $checkedOutTo, User $checkedInBy, $note) + public function __construct($checkoutable, $checkedOutTo, User $checkedInBy, $note) { - $this->accessory = $accessory; + $this->checkoutable = $checkoutable; $this->checkedOutTo = $checkedOutTo; $this->checkedInBy = $checkedInBy; $this->note = $note; diff --git a/app/Events/ComponentCheckedOut.php b/app/Events/CheckoutableCheckedOut.php similarity index 59% rename from app/Events/ComponentCheckedOut.php rename to app/Events/CheckoutableCheckedOut.php index f74e7d70f4..5e6ffd243a 100644 --- a/app/Events/ComponentCheckedOut.php +++ b/app/Events/CheckoutableCheckedOut.php @@ -2,30 +2,29 @@ namespace App\Events; -use App\Models\Component; use App\Models\User; -use Illuminate\Broadcasting\Channel; use Illuminate\Foundation\Events\Dispatchable; use Illuminate\Queue\SerializesModels; -class ComponentCheckedOut +class CheckoutableCheckedOut { use Dispatchable, SerializesModels; - public $component; + public $checkoutable; public $checkedOutTo; public $checkedOutBy; + public $note; /** * Create a new event instance. * * @return void */ - public function __construct(Component $component, $checkedOutTo, $quantity, User $checkedOutBy) + public function __construct($checkoutable, $checkedOutTo, User $checkedOutBy, $note) { - $this->component = $component; + $this->checkoutable = $checkoutable; $this->checkedOutTo = $checkedOutTo; - $this->quantity = $quantity; $this->checkedOutBy = $checkedOutBy; + $this->note = $note; } } diff --git a/app/Events/ComponentCheckedIn.php b/app/Events/ComponentCheckedIn.php deleted file mode 100644 index 283ea6e358..0000000000 --- a/app/Events/ComponentCheckedIn.php +++ /dev/null @@ -1,35 +0,0 @@ -component = $component; - $this->checkedOutTo = $checkedOutTo; - $this->checkedInBy = $checkedInBy; - $this->quantity = $quantity; - $this->note = $note; - } -} diff --git a/app/Events/ConsumableCheckedOut.php b/app/Events/ConsumableCheckedOut.php deleted file mode 100644 index ca002cc2f8..0000000000 --- a/app/Events/ConsumableCheckedOut.php +++ /dev/null @@ -1,32 +0,0 @@ -consumable = $consumable; - $this->checkedOutTo = $checkedOutTo; - $this->checkedOutBy = $checkedOutBy; - $this->note = $note; - } -} diff --git a/app/Events/LicenseSeatCheckedIn.php b/app/Events/LicenseSeatCheckedIn.php deleted file mode 100644 index 80e7ecbaf4..0000000000 --- a/app/Events/LicenseSeatCheckedIn.php +++ /dev/null @@ -1,34 +0,0 @@ -licenseSeat = $licenseSeat; - $this->checkedOutTo = $checkedOutTo; - $this->checkedInBy = $checkedInBy; - $this->note = $note; - } -} diff --git a/app/Events/LicenseSeatCheckedOut.php b/app/Events/LicenseSeatCheckedOut.php deleted file mode 100644 index 6b057be43e..0000000000 --- a/app/Events/LicenseSeatCheckedOut.php +++ /dev/null @@ -1,33 +0,0 @@ -licenseSeat = $licenseSeat; - $this->checkedOutTo = $checkedOutTo; - $this->checkedOutBy = $checkedOutBy; - $this->note = $note; - } -} diff --git a/app/Http/Controllers/Accessories/AccessoryCheckinController.php b/app/Http/Controllers/Accessories/AccessoryCheckinController.php index e64d24f169..930e9b6619 100644 --- a/app/Http/Controllers/Accessories/AccessoryCheckinController.php +++ b/app/Http/Controllers/Accessories/AccessoryCheckinController.php @@ -2,7 +2,7 @@ namespace App\Http\Controllers\Accessories; -use App\Events\AccessoryCheckedIn; +use App\Events\CheckoutableCheckedIn; use App\Http\Controllers\Controller; use App\Models\Accessory; use App\Models\User; @@ -64,7 +64,7 @@ class AccessoryCheckinController extends Controller if (DB::table('accessories_users')->where('id', '=', $accessory_user->id)->delete()) { $return_to = e($accessory_user->assigned_to); - event(new AccessoryCheckedIn($accessory, User::find($return_to), Auth::user(), $request->input('note'))); + event(new CheckoutableCheckedIn($accessory, User::find($return_to), Auth::user(), $request->input('note'))); return redirect()->route("accessories.show", $accessory->id)->with('success', trans('admin/accessories/message.checkin.success')); } diff --git a/app/Http/Controllers/Accessories/AccessoryCheckoutController.php b/app/Http/Controllers/Accessories/AccessoryCheckoutController.php index 13bc9e8fd2..5ec8a8704f 100644 --- a/app/Http/Controllers/Accessories/AccessoryCheckoutController.php +++ b/app/Http/Controllers/Accessories/AccessoryCheckoutController.php @@ -2,7 +2,7 @@ namespace App\Http\Controllers\Accessories; -use App\Events\AccessoryCheckedOut; +use App\Events\CheckoutableCheckedOut; use App\Http\Controllers\Controller; use App\Models\Accessory; use App\Models\User; @@ -80,7 +80,7 @@ class AccessoryCheckoutController extends Controller DB::table('accessories_users')->where('assigned_to', '=', $accessory->assigned_to)->where('accessory_id', '=', $accessory->id)->first(); - event(new AccessoryCheckedOut($accessory, $user, Auth::user(), $request->input('note'))); + event(new CheckoutableCheckedOut($accessory, $user, Auth::user(), $request->input('note'))); // Redirect to the new accessory page return redirect()->route('accessories.index')->with('success', trans('admin/accessories/message.checkout.success')); diff --git a/app/Http/Controllers/Assets/AssetCheckinController.php b/app/Http/Controllers/Assets/AssetCheckinController.php index da141f8b3d..e87a1df23c 100644 --- a/app/Http/Controllers/Assets/AssetCheckinController.php +++ b/app/Http/Controllers/Assets/AssetCheckinController.php @@ -2,7 +2,7 @@ namespace App\Http\Controllers\Assets; -use App\Events\AssetCheckedIn; +use App\Events\CheckoutableCheckedIn; use App\Helpers\Helper; use App\Http\Controllers\Controller; use App\Http\Requests\AssetCheckinRequest; @@ -85,7 +85,7 @@ class AssetCheckinController extends Controller // Was the asset updated? if ($asset->save()) { - event(new AssetCheckedIn($asset, $target, Auth::user(), $request->input('note'))); + event(new CheckoutableCheckedIn($asset, $target, Auth::user(), $request->input('note'))); if ($backto=='user') { return redirect()->route("users.show", $user->id)->with('success', trans('admin/hardware/message.checkin.success')); diff --git a/app/Http/Controllers/Components/ComponentCheckinController.php b/app/Http/Controllers/Components/ComponentCheckinController.php index fd91daa841..dfba1dcf84 100644 --- a/app/Http/Controllers/Components/ComponentCheckinController.php +++ b/app/Http/Controllers/Components/ComponentCheckinController.php @@ -2,6 +2,7 @@ namespace App\Http\Controllers\Components; +use App\Events\CheckoutableCheckedIn; use App\Events\ComponentCheckedIn; use App\Http\Controllers\Controller; use App\Models\Actionlog; @@ -94,7 +95,9 @@ class ComponentCheckinController extends Controller DB::table('components_assets')->where('id', '=', $component_asset_id)->delete(); } - event(new ComponentCheckedIn($component, $component_assets, Auth::user(), $request->input('checkin_qty'), $request->input('note'))); + $asset = Asset::find($component_assets->asset_id); + + event(new CheckoutableCheckedIn($component, $asset, Auth::user(), $request->input('note'))); return redirect()->route('components.index')->with('success', trans('admin/components/message.checkout.success')); diff --git a/app/Http/Controllers/Components/ComponentCheckoutController.php b/app/Http/Controllers/Components/ComponentCheckoutController.php index c7e7fe3db5..bfa9e8967b 100644 --- a/app/Http/Controllers/Components/ComponentCheckoutController.php +++ b/app/Http/Controllers/Components/ComponentCheckoutController.php @@ -2,6 +2,7 @@ namespace App\Http\Controllers\Components; +use App\Events\CheckoutableCheckedOut; use App\Events\ComponentCheckedOut; use App\Http\Controllers\Controller; use App\Models\Asset; @@ -87,7 +88,7 @@ class ComponentCheckoutController extends Controller 'asset_id' => $asset_id ]); - event(new ComponentCheckedOut($component, $asset, $request->input('assigned_qty'), Auth::user())); + event(new CheckoutableCheckedOut($component, $asset, Auth::user(), $request->input('note'))); return redirect()->route('components.index')->with('success', trans('admin/components/message.checkout.success')); } diff --git a/app/Http/Controllers/Consumables/ConsumableCheckoutController.php b/app/Http/Controllers/Consumables/ConsumableCheckoutController.php index 0b3d8c6c91..e3e3e11b03 100644 --- a/app/Http/Controllers/Consumables/ConsumableCheckoutController.php +++ b/app/Http/Controllers/Consumables/ConsumableCheckoutController.php @@ -2,7 +2,7 @@ namespace App\Http\Controllers\Consumables; -use App\Events\ConsumableCheckedOut; +use App\Events\CheckoutableCheckedOut; use App\Http\Controllers\Controller; use App\Models\Consumable; use App\Models\User; @@ -68,7 +68,7 @@ class ConsumableCheckoutController extends Controller 'assigned_to' => e(Input::get('assigned_to')) ]); - event(new ConsumableCheckedOut($consumable, $user, Auth::user(), $request->input('note'))); + event(new CheckoutableCheckedOut($consumable, $user, Auth::user(), $request->input('note'))); // Redirect to the new consumable page return redirect()->route('consumables.index')->with('success', trans('admin/consumables/message.checkout.success')); diff --git a/app/Http/Controllers/Licenses/LicenseCheckinController.php b/app/Http/Controllers/Licenses/LicenseCheckinController.php index 9612028465..4bddbd5753 100644 --- a/app/Http/Controllers/Licenses/LicenseCheckinController.php +++ b/app/Http/Controllers/Licenses/LicenseCheckinController.php @@ -2,7 +2,7 @@ namespace App\Http\Controllers\Licenses; -use App\Events\LicenseCheckedIn; +use App\Events\CheckoutableCheckedIn; use App\Models\Asset; use App\Models\License; use App\Models\LicenseSeat; @@ -91,7 +91,7 @@ class LicenseCheckinController extends Controller // Was the asset updated? if ($licenseSeat->save()) { - event(new LicenseCheckedIn($license, $return_to, Auth::user(), $request->input('note'))); + event(new CheckoutableCheckedIn($license, $return_to, Auth::user(), $request->input('note'))); if ($backTo=='user') { return redirect()->route("users.show", $return_to->id)->with('success', trans('admin/licenses/message.checkin.success')); diff --git a/app/Http/Controllers/Licenses/LicenseCheckoutController.php b/app/Http/Controllers/Licenses/LicenseCheckoutController.php index a1a02c823b..5d878a1506 100644 --- a/app/Http/Controllers/Licenses/LicenseCheckoutController.php +++ b/app/Http/Controllers/Licenses/LicenseCheckoutController.php @@ -2,17 +2,16 @@ namespace App\Http\Controllers\Licenses; -use App\Events\LicenseCheckedOut; -use App\Events\LicenseSeatCheckedOut; +use App\Events\CheckoutableCheckedOut; use App\Http\Requests\LicenseCheckoutRequest; use App\Models\Asset; use App\Models\License; use App\Models\LicenseSeat; use App\Models\User; use Illuminate\Http\Request; -use App\Http\Controllers\Controller; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Input; +use App\Http\Controllers\Controller; use Illuminate\Support\Facades\Validator; class LicenseCheckoutController extends Controller @@ -106,7 +105,7 @@ class LicenseCheckoutController extends Controller } if ($licenseSeat->save()) { - event(new LicenseSeatCheckedOut($licenseSeat, $target, Auth::user(), request('note'))); + event(new CheckoutableCheckedOut($licenseSeat, $target, Auth::user(), request('note'))); return true; } @@ -123,7 +122,7 @@ class LicenseCheckoutController extends Controller if ($licenseSeat->save()) { - event(new LicenseSeatCheckedOut($licenseSeat, $target, Auth::user(), request('note'))); + event(new CheckoutableCheckedOut($licenseSeat, $target, Auth::user(), request('note'))); return true; } diff --git a/app/Listeners/CheckoutableListener.php b/app/Listeners/CheckoutableListener.php new file mode 100644 index 0000000000..e06c6e1e40 --- /dev/null +++ b/app/Listeners/CheckoutableListener.php @@ -0,0 +1,184 @@ +checkedOutTo instanceof User) { + return; + } + + /** + * Make a checkout acceptance and attach it in the notification + */ + $acceptance = $this->getCheckoutAcceptance($event); + + Notification::send( + $this->getNotifiables($event), + $this->getCheckoutNotification($event, $acceptance) + ); + } + + /** + * Notify the user about the checked in checkoutable + */ + public function onCheckedIn($event) { + /** + * When the item wasn't checked out to a user, we can't send notifications + */ + if(!$event->checkedOutTo instanceof User) { + return; + } + + /** + * Send the appropriate notification + */ + Notification::send( + $this->getNotifiables($event), + $this->getCheckinNotification($event) + ); + } + + /** + * Generates a checkout acceptance + * @param Event $event + * @return mixed + */ + private function getCheckoutAcceptance($event) { + if (!$event->checkoutable->requireAcceptance()) { + return null; + } + + $acceptance = new CheckoutAcceptance; + $acceptance->checkoutable()->associate($event->checkoutable); + $acceptance->assignedTo()->associate($event->checkedOutTo); + $acceptance->save(); + + return $acceptance; + } + + /** + * Gets the entities to be notified of the passed event + * + * @param Event $event + * @return Collection + */ + private function getNotifiables($event) { + $notifiables = collect(); + + /** + * Notify the user who checked out the item + */ + $notifiables->push($event->checkedOutTo); + + /** + * Notify Admin users if the settings is activated + */ + if (Setting::getSettings()->admin_cc_email != '') { + $notifiables->push(new AdminRecipient()); + } + + return $notifiables; + } + + /** + * Get the appropriate notification for the event + * + * @param CheckoutableCheckedIn $event + * @return Notification + */ + private function getCheckinNotification($event) { + + $model = get_class($event->checkoutable); + + $notificationClass = null; + + switch (get_class($event->checkoutable)) { + case Accessory::class: + $notificationClass = CheckinAccessoryNotification::class; + break; + case Asset::class: + $notificationClass = CheckinAssetNotification::class; + break; + case LicenseSeat::class: + $notificationClass = CheckinLicenseSeatNotification::class; + break; + } + + return new $notificationClass($event->checkoutable, $event->checkedOutTo, $event->checkedInBy, $event->note); + } + + /** + * Get the appropriate notification for the event + * + * @param CheckoutableCheckedIn $event + * @param CheckoutAcceptance $acceptance + * @return Notification + */ + private function getCheckoutNotification($event, $acceptance) { + $notificationClass = null; + + switch (get_class($event->checkoutable)) { + case Accessory::class: + $notificationClass = CheckoutAccessoryNotification::class; + break; + case Asset::class: + $notificationClass = CheckoutAssetNotification::class; + break; + case Consumable::class: + $notificationClass = CheckoutConsumableNotification::class; + break; + case LicenseSeat::class: + $notificationClass = CheckoutLicenseSeatNotification::class; + break; + } + + return new $notificationClass($event->checkoutable, $event->checkedOutTo, $event->checkedOutBy, $acceptance, $event->note); + } + + /** + * Register the listeners for the subscriber. + * + * @param Illuminate\Events\Dispatcher $events + */ + public function subscribe($events) + { + $events->listen( + 'App\Events\CheckoutableCheckedIn', + 'App\Listeners\CheckoutableListener@onCheckedIn' + ); + + $events->listen( + 'App\Events\CheckoutableCheckedOut', + 'App\Listeners\CheckoutableListener@onCheckedOut' + ); + } + +} \ No newline at end of file diff --git a/app/Listeners/LogListener.php b/app/Listeners/LogListener.php index 9cd8ec2717..cdb4fda1fc 100644 --- a/app/Listeners/LogListener.php +++ b/app/Listeners/LogListener.php @@ -8,6 +8,8 @@ use App\Events\AssetCheckedIn; use App\Events\AssetCheckedOut; use App\Events\CheckoutAccepted; use App\Events\CheckoutDeclined; +use App\Events\CheckoutableCheckedIn; +use App\Events\CheckoutableCheckedOut; use App\Events\ComponentCheckedIn; use App\Events\ComponentCheckedOut; use App\Events\ConsumableCheckedOut; @@ -23,51 +25,15 @@ use App\Models\LicenseSeat; class LogListener { - public function onAccessoryCheckedIn(AccessoryCheckedIn $event) { - $event->accessory->logCheckin($event->checkedOutTo, $event->note); + + public function onCheckoutableCheckedIn(CheckoutableCheckedIn $event) { + $event->checkoutable->logCheckin($event->checkedOutTo, $event->note); } - public function onAccessoryCheckedOut(AccessoryCheckedOut $event) { - $event->accessory->logCheckout($event->note, $event->checkedOutTo); + public function onCheckoutableCheckedOut(CheckoutableCheckedOut $event) { + $event->checkoutable->logCheckout($event->note, $event->checkedOutTo); } - public function onAssetCheckedIn(AssetCheckedIn $event) { - $event->asset->logCheckin($event->checkedOutTo, $event->note); - } - - public function onAssetCheckedOut(AssetCheckedOut $event) { - $event->asset->logCheckout($event->note, $event->checkedOutTo); - } - - public function onComponentCheckedIn(ComponentCheckedIn $event) { - $log = new Actionlog(); - $log->user_id = $event->checkedInBy->id; - $log->action_type = 'checkin from'; - $log->target_type = Asset::class; - $log->target_id = $event->checkedOutTo->asset_id; - $log->item_id = $event->checkedOutTo->component_id; - $log->item_type = Component::class; - $log->note = $event->note; - $log->save(); - } - - public function onComponentCheckedOut(ComponentCheckedOut $event) { - // Since components don't have a "note" field, submit empty note - $event->component->logCheckout(null, $event->checkedOutTo); - } - - public function onConsumableCheckedOut(ConsumableCheckedOut $event) { - $event->consumable->logCheckout($event->note, $event->checkedOutTo); - } - - public function onLicenseCheckedIn(LicenseCheckedIn $event) { - $event->license->logCheckin($event->checkedOutTo, $event->note); - } - - public function onLicenseCheckedOut(LicenseCheckedOut $event) { - $event->license->logCheckout($event->note, $event->checkedOutTo); - } - public function onCheckoutAccepted(CheckoutAccepted $event) { $logaction = new Actionlog(); @@ -107,15 +73,8 @@ class LogListener public function subscribe($events) { $list = [ - 'AccessoryCheckedIn', - 'AccessoryCheckedOut', - 'AssetCheckedIn', - 'AssetCheckedOut', - 'ComponentCheckedIn', - 'ComponentCheckedOut', - 'ConsumableCheckedOut', - 'LicenseCheckedIn', - 'LicenseCheckedOut', + 'CheckoutableCheckedIn', + 'CheckoutableCheckedOut', 'CheckoutAccepted', 'CheckoutDeclined', ]; diff --git a/app/Listeners/SendingCheckInNotificationsListener.php b/app/Listeners/SendingCheckInNotificationsListener.php deleted file mode 100644 index aa176e19fd..0000000000 --- a/app/Listeners/SendingCheckInNotificationsListener.php +++ /dev/null @@ -1,117 +0,0 @@ -checkedOutTo instanceof User) { - return; - } - - Notification::send( - $this->getNotifiables($event), - new CheckinAccessoryNotification($event->accessory, $event->checkedOutTo, $event->checkedInBy, $event->note) - ); - } - - /** - * Notify the user about the checked in asset - */ - public function onAssetCheckedIn($event) { - /** - * When the item wasn't checked out to a user, we can't send notifications - */ - if(! $event->checkedOutTo instanceof User) { - return; - } - - Notification::send( - $this->getNotifiables($event), - new CheckinAssetNotification($event->asset, $event->checkedOutTo, $event->checkedInBy, $event->note) - ); - } - - /** - * Notify the user about the checked in license - */ - public function onLicenseSeatCheckedIn($event) { - /** - * When the item wasn't checked out to a user, we can't send notifications - */ - if(! $event->checkedOutTo instanceof User) { - return; - } - - Notification::send( - $this->getNotifiables($event), - new CheckinLicenseSeatNotification($event->licenseSeat, $event->checkedOutTo, $event->checkedInBy, $event->note) - ); - } - - /** - * Gets the entities to be notified of the passed event - * - * @param Event $event - * @return Collection - */ - private function getNotifiables($event) { - $notifiables = collect(); - - /** - * Notify the user who checked out the item - */ - $notifiables->push($event->checkedOutTo); - - /** - * Notify Admin users if the settings is activated - */ - if (Setting::getSettings()->admin_cc_email != '') { - $notifiables->push(new AdminRecipient()); - } - - return $notifiables; - } - - /** - * Register the listeners for the subscriber. - * - * @param Illuminate\Events\Dispatcher $events - */ - public function subscribe($events) - { - $events->listen( - 'App\Events\AccessoryCheckedIn', - 'App\Listeners\SendingCheckInNotificationsListener@onAccessoryCheckedIn' - ); - - $events->listen( - 'App\Events\AssetCheckedIn', - 'App\Listeners\SendingCheckInNotificationsListener@onAssetCheckedIn' - ); - - $events->listen( - 'App\Events\LicenseSeatCheckedIn', - 'App\Listeners\SendingCheckInNotificationsListener@onLicenseSeatCheckedIn' - ); - } - -} \ No newline at end of file diff --git a/app/Listeners/SendingCheckOutNotificationsListener.php b/app/Listeners/SendingCheckOutNotificationsListener.php deleted file mode 100644 index ed459c5a59..0000000000 --- a/app/Listeners/SendingCheckOutNotificationsListener.php +++ /dev/null @@ -1,182 +0,0 @@ -checkedOutTo instanceof User) { - return; - } - - /** - * Make a checkout acceptance and attach it in the notification - */ - $acceptance = null; - if ($event->consumable->requireAcceptance()) { - $acceptance = new CheckoutAcceptance; - $acceptance->checkoutable()->associate($event->consumable); - $acceptance->assignedTo()->associate($event->checkedOutTo); - $acceptance->save(); - } - - Notification::send( - $this->getNotifiables($event), - new CheckoutConsumableNotification($event->consumable, $event->checkedOutTo, $event->checkedOutBy, $acceptance, $event->note) - ); - } - - /** - * Notify the user about the checked out accessory - */ - public function onAccessoryCheckedOut($event) { - /** - * When the item wasn't checked out to a user, we can't send notifications - */ - if(! $event->checkedOutTo instanceof User) { - return; - } - - /** - * Make a checkout acceptance and attach it in the notification - */ - $acceptance = null; - if ($event->accessory->requireAcceptance()) { - $acceptance = new CheckoutAcceptance; - $acceptance->checkoutable()->associate($event->accessory); - $acceptance->assignedTo()->associate($event->checkedOutTo); - $acceptance->save(); - } - - Notification::send( - $this->getNotifiables($event), - new CheckoutAccessoryNotification($event->accessory, $event->checkedOutTo, $event->checkedOutBy, $acceptance, $event->note) - ); - } - - /** - * Notify the user about the checked out license - */ - public function onLicenseSeatCheckedOut($event) { - /** - * When the item wasn't checked out to a user, we can't send notifications - */ - if(! $event->checkedOutTo instanceof User) { - return; - } - - /** - * Make a checkout acceptance and attach it in the notification - */ - $acceptance = null; - if ($event->licenseSeat->license->requireAcceptance()) { - $acceptance = new CheckoutAcceptance; - $acceptance->checkoutable()->associate($event->licenseSeat); - $acceptance->assignedTo()->associate($event->checkedOutTo); - $acceptance->save(); - } - - Notification::send( - $this->getNotifiables($event), - new CheckoutLicenseSeatNotification($event->licenseSeat, $event->checkedOutTo, $event->checkedOutBy, $acceptance, $event->note) - ); - } - - /** - * Notify the user about the checked out asset - */ - public function onAssetCheckedOut($event) { - /** - * When the item wasn't checked out to a user, we can't send notifications - */ - if(! $event->checkedOutTo instanceof User) { - return; - } - - /** - * Make a checkout acceptance and attach it in the notification - */ - $acceptance = null; - if ($event->asset->requireAcceptance()) { - $acceptance = new CheckoutAcceptance; - $acceptance->checkoutable()->associate($event->asset); - $acceptance->assignedTo()->associate($event->checkedOutTo); - $acceptance->save(); - } - - Notification::send( - $this->getNotifiables($event), - new CheckoutAssetNotification($event->asset, $event->checkedOutTo, $event->checkedOutBy, $acceptance, $event->note) - ); - } - - /** - * Gets the entities to be notified of the passed event - * - * @param Event $event - * @return Collection - */ - private function getNotifiables($event) { - $notifiables = collect(); - - /** - * Notify the user who checked out the item - */ - $notifiables->push($event->checkedOutTo); - - /** - * Notify Admin users if the settings is activated - */ - if (Setting::getSettings()->admin_cc_email != '') { - $notifiables->push(new AdminRecipient()); - } - - return $notifiables; - } - - /** - * Register the listeners for the subscriber. - * - * @param Illuminate\Events\Dispatcher $events - */ - public function subscribe($events) - { - $events->listen( - 'App\Events\ConsumableCheckedOut', - 'App\Listeners\SendingCheckOutNotificationsListener@onConsumableCheckedOut' - ); - - $events->listen( - 'App\Events\AccessoryCheckedOut', - 'App\Listeners\SendingCheckOutNotificationsListener@onAccessoryCheckedOut' - ); - - $events->listen( - 'App\Events\LicenseSeatCheckedOut', - 'App\Listeners\SendingCheckOutNotificationsListener@onLicenseSeatCheckedOut' - ); - - $events->listen( - 'App\Events\AssetCheckedOut', - 'App\Listeners\SendingCheckOutNotificationsListener@onAssetCheckedOut' - ); - } - -} \ No newline at end of file diff --git a/app/Models/Asset.php b/app/Models/Asset.php index 35206b41ea..ceb6ee6962 100644 --- a/app/Models/Asset.php +++ b/app/Models/Asset.php @@ -2,6 +2,7 @@ namespace App\Models; use App\Events\AssetCheckedOut; +use App\Events\CheckoutableCheckedOut; use App\Exceptions\CheckoutNotAllowed; use App\Http\Traits\UniqueSerialTrait; use App\Http\Traits\UniqueUndeletedTrait; @@ -265,7 +266,7 @@ class Asset extends Depreciable if ($this->save()) { - event(new AssetCheckedOut($this, $target, Auth::user(), $note)); + event(new CheckoutableCheckedOut($this, $target, Auth::user(), $note)); $this->increment('checkout_counter', 1); return true; diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php index df3c643b24..28c3aecf06 100644 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -3,9 +3,8 @@ namespace App\Providers; use Illuminate\Support\Facades\Event; +use App\Listeners\CheckoutableListener; use App\Listeners\LogListener; -use App\Listeners\SendingCheckInNotificationsListener; -use App\Listeners\SendingCheckOutNotificationsListener; use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider; class EventServiceProvider extends ServiceProvider @@ -32,9 +31,8 @@ class EventServiceProvider extends ServiceProvider * @var array */ protected $subscribe = [ - SendingCheckOutNotificationsListener::class, - SendingCheckInNotificationsListener::class, - LogListener::class + LogListener::class, + CheckoutableListener::class ]; /**