1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 15:13:29 +01:00
invoiceninja/app/Services/PushService.php

176 lines
4.4 KiB
PHP
Raw Normal View History

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
use Illuminate\Http\Request;
2016-03-01 03:57:59 +01:00
use App\Ninja\Notifications\PushFactory;
2016-02-29 12:21:15 +01:00
/**
* Class PushService
* @package App\Ninja\Notifications
*/
2016-03-01 01:31:16 +01:00
/**
* $account->devices Definition
*
2016-03-01 03:57:59 +01:00
* @param string token (push notification device token)
* @param string email (user email address - required for use as key)
* @param string device (ios, gcm etc etc)
2016-03-01 01:31:16 +01:00
* @param bool notify_sent
* @param bool notify_paid
* @param bool notify_approved
* @param bool notify_viewed
*/
2016-02-29 12:21:15 +01:00
class PushService
{
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;
}
/**
* @param $invoice - Invoice object
2016-03-01 01:31:16 +01:00
* @param $type - Type of notification, ie. Quote APPROVED, Invoice PAID, Invoice/Quote SENT, Invoice/Quote VIEWED
2016-02-29 12:21:15 +01:00
*/
public function sendNotification($invoice, $type)
{
if (! IOS_PUSH_CERTIFICATE) {
return;
}
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)
{
2016-03-01 03:57:59 +01:00
if(($device["notify_{$type}"] == TRUE) && ($device['device'] == 'ios'))
2016-03-02 12:00:08 +01:00
$this->pushMessage($invoice, $device['token'], $type);
2016-03-01 01:31:16 +01:00
}
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-03-01 04:00:11 +01:00
* pushMessage function
*
* method to dispatch iOS notifications
*
2016-03-01 03:57:59 +01:00
* @param $invoice
* @param $token
* @param $type
*/
2016-03-01 01:31:16 +01:00
private function pushMessage($invoice, $token, $type)
{
$this->pushFactory->message($token, $this->messageType($invoice, $type));
}
/**
* checkDeviceExists function
*
* Returns a boolean if this account has devices registered for PUSH notifications
*
* @param $account
* @return bool
*/
2016-02-29 12:21:15 +01:00
private function checkDeviceExists($account)
{
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-03-01 03:57:59 +01:00
* @param $invoice
* @param $type
* @return string
*/
2016-03-01 01:31:16 +01:00
private function messageType($invoice, $type)
{
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
/**
* @param $invoice
* @return string
*/
2016-03-01 01:31:16 +01:00
private function entitySentMessage($invoice)
{
2016-05-26 16:56:54 +02:00
if($invoice->isType(INVOICE_TYPE_QUOTE))
2016-03-03 03:28:16 +01: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-03-03 03:28:16 +01: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
/**
* @param $invoice
* @return string
*/
2016-03-01 01:31:16 +01:00
private function invoicePaidMessage($invoice)
{
2016-03-03 03:28:16 +01: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
/**
* @param $invoice
* @return string
*/
2016-03-01 01:31:16 +01:00
private function quoteApprovedMessage($invoice)
{
2016-03-03 03:28:16 +01: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
/**
* @param $invoice
* @return string
*/
2016-03-01 01:31:16 +01:00
private function entityViewedMessage($invoice)
{
2016-05-26 16:56:54 +02:00
if($invoice->isType(INVOICE_TYPE_QUOTE))
2016-03-03 03:28:16 +01: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-03-03 03:28:16 +01:00
return trans("texts.notification_invoice_viewed_subject", ['invoice' => $invoice->invoice_number, 'client' => $invoice->client->name]);
2016-03-01 01:31:16 +01:00
}
}