2018-07-21 04:54:30 -07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Listeners;
|
|
|
|
|
2018-07-28 03:45:33 -07:00
|
|
|
use App\Models\CheckoutAcceptance;
|
2018-07-21 05:03:44 -07:00
|
|
|
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;
|
2018-07-28 03:45:33 -07:00
|
|
|
use App\Notifications\CheckoutLicenseSeatNotification;
|
2018-07-27 15:27:19 -07:00
|
|
|
use Illuminate\Support\Facades\Notification;
|
2018-07-21 05:03:44 -07:00
|
|
|
|
2018-07-21 04:54:30 -07:00
|
|
|
class SendingCheckOutNotificationsListener
|
|
|
|
{
|
2018-07-21 05:03:44 -07:00
|
|
|
/**
|
2018-07-27 15:27:19 -07:00
|
|
|
* Notify the user about the checked out consumable
|
2018-07-21 05:03:44 -07:00
|
|
|
*/
|
|
|
|
public function onConsumableCheckedOut($event) {
|
|
|
|
/**
|
2018-07-27 15:27:19 -07:00
|
|
|
* When the item wasn't checked out to a user, we can't send notifications
|
2018-07-21 05:03:44 -07:00
|
|
|
*/
|
2018-07-27 15:27:19 -07:00
|
|
|
if(! $event->checkedOutTo instanceof User) {
|
|
|
|
return;
|
|
|
|
}
|
2018-07-21 05:03:44 -07:00
|
|
|
|
2018-07-28 03:45:33 -07:00
|
|
|
/**
|
|
|
|
* 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();
|
|
|
|
}
|
|
|
|
|
2018-07-27 15:27:19 -07:00
|
|
|
Notification::send(
|
|
|
|
$this->getNotifiables($event),
|
2018-07-28 03:45:33 -07:00
|
|
|
new CheckoutConsumableNotification($event->consumable, $event->checkedOutTo, $event->checkedOutBy, $acceptance, $event->note)
|
2018-07-27 15:27:19 -07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Notify the user about the checked out accessory
|
|
|
|
*/
|
2018-07-21 05:03:44 -07:00
|
|
|
public function onAccessoryCheckedOut($event) {
|
|
|
|
/**
|
2018-07-27 15:27:19 -07:00
|
|
|
* When the item wasn't checked out to a user, we can't send notifications
|
2018-07-21 05:03:44 -07:00
|
|
|
*/
|
2018-07-27 15:27:19 -07:00
|
|
|
if(! $event->checkedOutTo instanceof User) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-28 03:45:33 -07:00
|
|
|
/**
|
|
|
|
* 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();
|
|
|
|
}
|
|
|
|
|
2018-07-27 15:27:19 -07:00
|
|
|
Notification::send(
|
|
|
|
$this->getNotifiables($event),
|
2018-07-28 03:45:33 -07:00
|
|
|
new CheckoutAccessoryNotification($event->accessory, $event->checkedOutTo, $event->checkedOutBy, $acceptance, $event->note)
|
2018-07-27 15:27:19 -07:00
|
|
|
);
|
2018-07-21 05:03:44 -07:00
|
|
|
}
|
|
|
|
|
2018-07-27 15:27:19 -07:00
|
|
|
/**
|
|
|
|
* Notify the user about the checked out license
|
|
|
|
*/
|
2018-07-28 03:45:33 -07:00
|
|
|
public function onLicenseSeatCheckedOut($event) {
|
2018-07-21 05:03:44 -07:00
|
|
|
/**
|
2018-07-27 15:27:19 -07:00
|
|
|
* When the item wasn't checked out to a user, we can't send notifications
|
2018-07-21 05:03:44 -07:00
|
|
|
*/
|
2018-07-27 15:27:19 -07:00
|
|
|
if(! $event->checkedOutTo instanceof User) {
|
|
|
|
return;
|
|
|
|
}
|
2018-07-21 05:03:44 -07:00
|
|
|
|
2018-07-28 03:45:33 -07:00
|
|
|
/**
|
|
|
|
* 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();
|
|
|
|
}
|
|
|
|
|
2018-07-27 15:27:19 -07:00
|
|
|
Notification::send(
|
|
|
|
$this->getNotifiables($event),
|
2018-07-28 03:45:33 -07:00
|
|
|
new CheckoutLicenseSeatNotification($event->licenseSeat, $event->checkedOutTo, $event->checkedOutBy, $acceptance, $event->note)
|
2018-07-27 15:27:19 -07:00
|
|
|
);
|
2018-07-21 05:03:44 -07:00
|
|
|
}
|
2018-07-27 15:27:19 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Notify the user about the checked out asset
|
|
|
|
*/
|
|
|
|
public function onAssetCheckedOut($event) {
|
2018-07-21 05:03:44 -07:00
|
|
|
/**
|
2018-07-27 15:27:19 -07:00
|
|
|
* When the item wasn't checked out to a user, we can't send notifications
|
2018-07-21 05:03:44 -07:00
|
|
|
*/
|
2018-07-27 15:27:19 -07:00
|
|
|
if(! $event->checkedOutTo instanceof User) {
|
2018-07-21 05:03:44 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-28 03:45:33 -07:00
|
|
|
/**
|
|
|
|
* 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();
|
|
|
|
}
|
|
|
|
|
2018-07-27 15:27:19 -07:00
|
|
|
Notification::send(
|
|
|
|
$this->getNotifiables($event),
|
2018-07-28 03:45:33 -07:00
|
|
|
new CheckoutAssetNotification($event->asset, $event->checkedOutTo, $event->checkedOutBy, $acceptance, $event->note)
|
2018-07-27 15:27:19 -07:00
|
|
|
);
|
|
|
|
}
|
2018-07-21 05:03:44 -07:00
|
|
|
|
2018-07-27 15:27:19 -07:00
|
|
|
/**
|
|
|
|
* Gets the entities to be notified of the passed event
|
|
|
|
*
|
|
|
|
* @param Event $event
|
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
private function getNotifiables($event) {
|
|
|
|
$notifiables = collect();
|
2018-07-21 05:03:44 -07:00
|
|
|
|
2018-07-27 15:27:19 -07:00
|
|
|
/**
|
|
|
|
* Notify the user who checked out the item
|
|
|
|
*/
|
|
|
|
$notifiables->push($event->checkedOutTo);
|
2018-07-21 05:03:44 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Notify Admin users if the settings is activated
|
|
|
|
*/
|
|
|
|
if (Setting::getSettings()->admin_cc_email != '') {
|
2018-07-27 15:27:19 -07:00
|
|
|
$notifiables->push(new AdminRecipient());
|
|
|
|
}
|
2018-07-21 05:03:44 -07:00
|
|
|
|
2018-07-27 15:27:19 -07:00
|
|
|
return $notifiables;
|
2018-07-21 05:03:44 -07:00
|
|
|
}
|
|
|
|
|
2018-07-21 04:54:30 -07:00
|
|
|
/**
|
|
|
|
* Register the listeners for the subscriber.
|
|
|
|
*
|
|
|
|
* @param Illuminate\Events\Dispatcher $events
|
|
|
|
*/
|
|
|
|
public function subscribe($events)
|
|
|
|
{
|
2018-07-21 05:03:44 -07:00
|
|
|
$events->listen(
|
|
|
|
'App\Events\ConsumableCheckedOut',
|
|
|
|
'App\Listeners\SendingCheckOutNotificationsListener@onConsumableCheckedOut'
|
|
|
|
);
|
|
|
|
|
|
|
|
$events->listen(
|
|
|
|
'App\Events\AccessoryCheckedOut',
|
|
|
|
'App\Listeners\SendingCheckOutNotificationsListener@onAccessoryCheckedOut'
|
|
|
|
);
|
|
|
|
|
|
|
|
$events->listen(
|
2018-07-28 03:45:33 -07:00
|
|
|
'App\Events\LicenseSeatCheckedOut',
|
|
|
|
'App\Listeners\SendingCheckOutNotificationsListener@onLicenseSeatCheckedOut'
|
2018-07-21 05:03:44 -07:00
|
|
|
);
|
|
|
|
|
|
|
|
$events->listen(
|
|
|
|
'App\Events\AssetCheckedOut',
|
|
|
|
'App\Listeners\SendingCheckOutNotificationsListener@onAssetCheckedOut'
|
|
|
|
);
|
2018-07-21 04:54:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|