simplified checkout event handling per @uberbrady’s suggestion

This generalizes the checkout events into the CheckoutableCheckedOut and CheckoutableCheckedIn events.
This commit is contained in:
Till Deeke 2018-09-10 16:40:26 +02:00
parent 62195a805a
commit 007e8fbdf9
23 changed files with 229 additions and 618 deletions

View file

@ -1,32 +0,0 @@
<?php
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 AccessoryCheckedOut
{
use Dispatchable, SerializesModels;
public $accessory;
public $checkedOutTo;
public $logEntry;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct(Accessory $accessory, $checkedOutTo, User $checkedOutBy, $note)
{
$this->accessory = $accessory;
$this->checkedOutTo = $checkedOutTo;
$this->checkedOutBy = $checkedOutBy;
$this->note = $note;
}
}

View file

@ -1,33 +0,0 @@
<?php
namespace App\Events;
use App\Models\Actionlog;
use App\Models\Asset;
use App\Models\User;
use Illuminate\Broadcasting\Channel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class AssetCheckedIn
{
use Dispatchable, SerializesModels;
public $asset;
public $checkedOutTo;
public $checkedInBy;
public $note;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct(Asset $asset, $checkedOutTo, User $checkedInBy, $note)
{
$this->asset = $asset;
$this->checkedOutTo = $checkedOutTo;
$this->checkedInBy = $checkedInBy;
$this->note = $note;
}
}

View file

@ -1,32 +0,0 @@
<?php
namespace App\Events;
use App\Models\Actionlog;
use App\Models\Asset;
use App\Models\User;
use Illuminate\Broadcasting\Channel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class AssetCheckedOut
{
use Dispatchable, SerializesModels;
public $asset;
public $checkedOutTo;
public $logEntry;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct(Asset $asset, $checkedOutTo, User $checkedOutBy, $note)
{
$this->asset = $asset;
$this->checkedOutTo = $checkedOutTo;
$this->checkedOutBy = $checkedOutBy;
$this->note = $note;
}
}

View file

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

View file

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

View file

@ -1,35 +0,0 @@
<?php
namespace App\Events;
use App\Models\Actionlog;
use App\Models\Component;
use App\Models\User;
use Illuminate\Broadcasting\Channel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class ComponentCheckedIn
{
use Dispatchable, SerializesModels;
public $component;
public $checkedOutTo;
public $checkedInBy;
public $quantity;
public $note;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct(Component $component, $checkedOutTo, User $checkedInBy, $quantity, $note)
{
$this->component = $component;
$this->checkedOutTo = $checkedOutTo;
$this->checkedInBy = $checkedInBy;
$this->quantity = $quantity;
$this->note = $note;
}
}

View file

@ -1,32 +0,0 @@
<?php
namespace App\Events;
use App\Models\Actionlog;
use App\Models\Consumable;
use App\Models\User;
use Illuminate\Broadcasting\Channel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class ConsumableCheckedOut
{
use Dispatchable, SerializesModels;
public $consumable;
public $checkedOutTo;
public $logEntry;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct(Consumable $consumable, $checkedOutTo, User $checkedOutBy, $note)
{
$this->consumable = $consumable;
$this->checkedOutTo = $checkedOutTo;
$this->checkedOutBy = $checkedOutBy;
$this->note = $note;
}
}

View file

@ -1,34 +0,0 @@
<?php
namespace App\Events;
use App\Models\Actionlog;
use App\Models\License;
use App\Models\LicenseSeat;
use App\Models\User;
use Illuminate\Broadcasting\Channel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class LicenseCheckedIn
{
use Dispatchable, SerializesModels;
public $licenseSeat;
public $checkedOutTo;
public $checkedInBy;
public $note;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct(LicenseSeat $licenseSeat, $checkedOutTo, User $checkedInBy, $note)
{
$this->licenseSeat = $licenseSeat;
$this->checkedOutTo = $checkedOutTo;
$this->checkedInBy = $checkedInBy;
$this->note = $note;
}
}

View file

@ -1,33 +0,0 @@
<?php
namespace App\Events;
use App\Models\Actionlog;
use App\Models\License;
use App\Models\LicenseSeat;
use App\Models\User;
use Illuminate\Broadcasting\Channel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class LicenseSeatCheckedOut
{
use Dispatchable, SerializesModels;
public $licenseSeat;
public $checkedOutTo;
public $logEntry;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct(LicenseSeat $licenseSeat, $checkedOutTo, User $checkedOutBy, $note)
{
$this->licenseSeat = $licenseSeat;
$this->checkedOutTo = $checkedOutTo;
$this->checkedOutBy = $checkedOutBy;
$this->note = $note;
}
}

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -0,0 +1,184 @@
<?php
namespace App\Listeners;
use App\Models\Accessory;
use App\Models\Asset;
use App\Models\CheckoutAcceptance;
use App\Models\Consumable;
use App\Models\LicenseSeat;
use App\Models\Recipients\AdminRecipient;
use App\Models\Setting;
use App\Models\User;
use App\Notifications\CheckinAccessoryNotification;
use App\Notifications\CheckinAssetNotification;
use App\Notifications\CheckinLicenseNotification;
use App\Notifications\CheckinLicenseSeatNotification;
use App\Notifications\CheckoutAccessoryNotification;
use App\Notifications\CheckoutAssetNotification;
use App\Notifications\CheckoutConsumableNotification;
use App\Notifications\CheckoutLicenseNotification;
use App\Notifications\CheckoutLicenseSeatNotification;
use Illuminate\Support\Facades\Notification;
class CheckoutableListener
{
/**
* Notify the user about the checked out checkoutable
*/
public function onCheckedOut($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 = $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'
);
}
}

View file

@ -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',
];

View file

@ -1,117 +0,0 @@
<?php
namespace App\Listeners;
use App\Models\Recipients\AdminRecipient;
use App\Models\Setting;
use App\Models\User;
use App\Notifications\CheckinAccessoryNotification;
use App\Notifications\CheckinAssetNotification;
use App\Notifications\CheckinLicenseNotification;
use App\Notifications\CheckoutAccessoryNotification;
use App\Notifications\CheckoutAssetNotification;
use App\Notifications\CheckoutConsumableNotification;
use App\Notifications\CheckoutLicenseNotification;
use Illuminate\Support\Facades\Notification;
class SendingCheckInNotificationsListener
{
/**
* Notify the user about the checked in accessory
*/
public function onAccessoryCheckedIn($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 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'
);
}
}

View file

@ -1,182 +0,0 @@
<?php
namespace App\Listeners;
use App\Models\CheckoutAcceptance;
use App\Models\Recipients\AdminRecipient;
use App\Models\Setting;
use App\Models\User;
use App\Notifications\CheckoutAccessoryNotification;
use App\Notifications\CheckoutAssetNotification;
use App\Notifications\CheckoutConsumableNotification;
use App\Notifications\CheckoutLicenseNotification;
use App\Notifications\CheckoutLicenseSeatNotification;
use Illuminate\Support\Facades\Notification;
class SendingCheckOutNotificationsListener
{
/**
* Notify the user about the checked out consumable
*/
public function onConsumableCheckedOut($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->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'
);
}
}

View file

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

View file

@ -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
];
/**