2016-02-29 12:21:15 +01:00
|
|
|
<?php
|
|
|
|
|
2016-03-01 03:57:59 +01:00
|
|
|
namespace App\Services;
|
2016-02-29 12:21:15 +01:00
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
use App\Models\Account;
|
|
|
|
use App\Models\Invoice;
|
2016-03-01 03:57:59 +01:00
|
|
|
use App\Ninja\Notifications\PushFactory;
|
2016-03-01 01:31:16 +01:00
|
|
|
|
|
|
|
/**
|
2016-07-03 18:11:58 +02:00
|
|
|
* Class PushService
|
2016-03-01 01:31:16 +01:00
|
|
|
*/
|
2016-02-29 12:21:15 +01:00
|
|
|
class PushService
|
|
|
|
{
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @var PushFactory
|
|
|
|
*/
|
2016-02-29 12:21:15 +01:00
|
|
|
protected $pushFactory;
|
|
|
|
|
2016-03-01 03:57:59 +01:00
|
|
|
/**
|
|
|
|
* @param PushFactory $pushFactory
|
|
|
|
*/
|
2016-02-29 12:21:15 +01:00
|
|
|
public function __construct(PushFactory $pushFactory)
|
|
|
|
{
|
|
|
|
$this->pushFactory = $pushFactory;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-07-03 18:11:58 +02:00
|
|
|
* @param Invoice $invoice
|
|
|
|
* @param $type
|
2016-02-29 12:21:15 +01:00
|
|
|
*/
|
2016-07-03 18:11:58 +02:00
|
|
|
public function sendNotification(Invoice $invoice, $type)
|
2016-02-29 12:21:15 +01:00
|
|
|
{
|
|
|
|
//check user has registered for push notifications
|
|
|
|
if(!$this->checkDeviceExists($invoice->account))
|
|
|
|
return;
|
|
|
|
|
|
|
|
//Harvest an array of devices that are registered for this notification type
|
2016-03-01 01:31:16 +01:00
|
|
|
$devices = json_decode($invoice->account->devices, TRUE);
|
|
|
|
|
|
|
|
foreach($devices as $device)
|
|
|
|
{
|
2017-01-10 04:35:30 +01:00
|
|
|
if(($device["notify_{$type}"] == TRUE) && ($device['device'] == 'ios') && IOS_DEVICE)
|
|
|
|
$this->pushMessage($invoice, $device['token'], $type, IOS_DEVICE);
|
|
|
|
elseif(($device["notify_{$type}"] == TRUE) && ($device['device'] == 'fcm') && ANDROID_DEVICE)
|
|
|
|
$this->pushMessage($invoice, $device['token'], $type, ANDROID_DEVICE);
|
2016-03-01 01:31:16 +01:00
|
|
|
}
|
2016-02-29 12:21:15 +01:00
|
|
|
}
|
|
|
|
|
2016-03-01 03:57:59 +01:00
|
|
|
/**
|
2016-03-01 04:00:11 +01:00
|
|
|
* pushMessage function
|
|
|
|
*
|
|
|
|
* method to dispatch iOS notifications
|
|
|
|
*
|
2016-07-03 18:11:58 +02:00
|
|
|
* @param Invoice $invoice
|
2016-03-01 03:57:59 +01:00
|
|
|
* @param $token
|
|
|
|
* @param $type
|
|
|
|
*/
|
2017-01-10 04:35:30 +01:00
|
|
|
private function pushMessage(Invoice $invoice, $token, $type, $device)
|
2016-03-01 01:31:16 +01:00
|
|
|
{
|
2017-01-10 04:35:30 +01:00
|
|
|
$this->pushFactory->message($token, $this->messageType($invoice, $type), $device);
|
2016-03-01 01:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* checkDeviceExists function
|
|
|
|
*
|
|
|
|
* Returns a boolean if this account has devices registered for PUSH notifications
|
|
|
|
*
|
2016-07-03 18:11:58 +02:00
|
|
|
* @param Account $account
|
|
|
|
*
|
2016-03-01 01:31:16 +01:00
|
|
|
* @return bool
|
|
|
|
*/
|
2016-07-03 18:11:58 +02:00
|
|
|
private function checkDeviceExists(Account $account)
|
2016-02-29 12:21:15 +01:00
|
|
|
{
|
2016-03-01 01:31:16 +01:00
|
|
|
$devices = json_decode($account->devices, TRUE);
|
|
|
|
|
|
|
|
if(count($devices) >= 1)
|
|
|
|
return TRUE;
|
|
|
|
else
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2016-03-01 03:57:59 +01:00
|
|
|
/**
|
2016-03-01 04:00:11 +01:00
|
|
|
* messageType function
|
|
|
|
*
|
|
|
|
* method which formats an appropriate message depending on message type
|
|
|
|
*
|
2016-07-03 18:11:58 +02:00
|
|
|
* @param Invoice $invoice
|
2016-03-01 03:57:59 +01:00
|
|
|
* @param $type
|
2016-07-03 18:11:58 +02:00
|
|
|
*
|
2016-03-01 03:57:59 +01:00
|
|
|
* @return string
|
|
|
|
*/
|
2016-07-03 18:11:58 +02:00
|
|
|
private function messageType(Invoice $invoice, $type)
|
2016-03-01 01:31:16 +01:00
|
|
|
{
|
|
|
|
switch($type)
|
|
|
|
{
|
2016-03-02 12:00:08 +01:00
|
|
|
case 'sent':
|
2016-03-01 01:31:16 +01:00
|
|
|
return $this->entitySentMessage($invoice);
|
|
|
|
break;
|
|
|
|
|
2016-03-02 12:00:08 +01:00
|
|
|
case 'paid':
|
2016-03-01 01:31:16 +01:00
|
|
|
return $this->invoicePaidMessage($invoice);
|
|
|
|
break;
|
|
|
|
|
2016-03-02 12:00:08 +01:00
|
|
|
case 'approved':
|
2016-03-01 01:31:16 +01:00
|
|
|
return $this->quoteApprovedMessage($invoice);
|
|
|
|
break;
|
2016-02-29 12:21:15 +01:00
|
|
|
|
2016-03-02 12:00:08 +01:00
|
|
|
case 'viewed':
|
2016-03-01 01:31:16 +01:00
|
|
|
return $this->entityViewedMessage($invoice);
|
|
|
|
break;
|
|
|
|
}
|
2016-02-29 12:21:15 +01:00
|
|
|
}
|
2016-03-01 01:31:16 +01:00
|
|
|
|
2016-03-01 03:57:59 +01:00
|
|
|
/**
|
2016-07-03 18:11:58 +02:00
|
|
|
* @param Invoice $invoice
|
2016-03-01 03:57:59 +01:00
|
|
|
* @return string
|
|
|
|
*/
|
2016-07-03 18:11:58 +02:00
|
|
|
private function entitySentMessage(Invoice $invoice)
|
2016-03-01 01:31:16 +01:00
|
|
|
{
|
2016-05-26 16:56:54 +02:00
|
|
|
if($invoice->isType(INVOICE_TYPE_QUOTE))
|
2016-07-03 18:11:58 +02:00
|
|
|
return trans('texts.notification_quote_sent_subject', ['invoice' => $invoice->invoice_number, 'client' => $invoice->client->name]);
|
2016-03-01 01:31:16 +01:00
|
|
|
else
|
2016-07-03 18:11:58 +02:00
|
|
|
return trans('texts.notification_invoice_sent_subject', ['invoice' => $invoice->invoice_number, 'client' => $invoice->client->name]);
|
2016-03-01 01:31:16 +01:00
|
|
|
}
|
|
|
|
|
2016-03-01 03:57:59 +01:00
|
|
|
/**
|
2016-07-03 18:11:58 +02:00
|
|
|
* @param Invoice $invoice
|
2016-03-01 03:57:59 +01:00
|
|
|
* @return string
|
|
|
|
*/
|
2016-07-03 18:11:58 +02:00
|
|
|
private function invoicePaidMessage(Invoice $invoice)
|
2016-03-01 01:31:16 +01:00
|
|
|
{
|
2016-07-03 18:11:58 +02:00
|
|
|
return trans('texts.notification_invoice_paid_subject', ['invoice' => $invoice->invoice_number, 'client' => $invoice->client->name]);
|
2016-03-01 01:31:16 +01:00
|
|
|
}
|
|
|
|
|
2016-03-01 03:57:59 +01:00
|
|
|
/**
|
2016-07-03 18:11:58 +02:00
|
|
|
* @param Invoice $invoice
|
2016-03-01 03:57:59 +01:00
|
|
|
* @return string
|
|
|
|
*/
|
2016-07-03 18:11:58 +02:00
|
|
|
private function quoteApprovedMessage(Invoice $invoice)
|
2016-03-01 01:31:16 +01:00
|
|
|
{
|
2016-07-03 18:11:58 +02:00
|
|
|
return trans('texts.notification_quote_approved_subject', ['invoice' => $invoice->invoice_number, 'client' => $invoice->client->name]);
|
2016-03-01 01:31:16 +01:00
|
|
|
}
|
|
|
|
|
2016-03-01 03:57:59 +01:00
|
|
|
/**
|
2016-07-03 18:11:58 +02:00
|
|
|
* @param Invoice $invoice
|
2016-03-01 03:57:59 +01:00
|
|
|
* @return string
|
|
|
|
*/
|
2016-07-03 18:11:58 +02:00
|
|
|
private function entityViewedMessage(Invoice $invoice)
|
2016-03-01 01:31:16 +01:00
|
|
|
{
|
2016-05-26 16:56:54 +02:00
|
|
|
if($invoice->isType(INVOICE_TYPE_QUOTE))
|
2016-07-03 18:11:58 +02:00
|
|
|
return trans('texts.notification_quote_viewed_subject', ['invoice' => $invoice->invoice_number, 'client' => $invoice->client->name]);
|
2016-03-01 01:31:16 +01:00
|
|
|
else
|
2016-07-03 18:11:58 +02:00
|
|
|
return trans('texts.notification_invoice_viewed_subject', ['invoice' => $invoice->invoice_number, 'client' => $invoice->client->name]);
|
2016-03-01 01:31:16 +01:00
|
|
|
}
|
2016-07-21 14:35:23 +02:00
|
|
|
}
|