1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-22 17:31:35 +02:00
invoiceninja/app/Services/PushService.php

157 lines
4.1 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 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
/**
* Class PushService
2016-03-01 01:31:16 +01:00
*/
2016-02-29 12:21:15 +01:00
class PushService
{
/**
* @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;
}
/**
* @param Invoice $invoice
* @param $type
2016-02-29 12:21:15 +01:00
*/
public function sendNotification(Invoice $invoice, $type)
2016-02-29 12:21:15 +01:00
{
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 03:57:59 +01:00
/**
2016-03-01 04:00:11 +01:00
* pushMessage function
*
* method to dispatch iOS notifications
*
* @param Invoice $invoice
2016-03-01 03:57:59 +01:00
* @param $token
* @param $type
*/
private function pushMessage(Invoice $invoice, $token, $type)
2016-03-01 01:31:16 +01:00
{
$this->pushFactory->message($token, $this->messageType($invoice, $type));
}
/**
* checkDeviceExists function
*
* Returns a boolean if this account has devices registered for PUSH notifications
*
* @param Account $account
*
2016-03-01 01:31:16 +01:00
* @return bool
*/
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
*
* @param Invoice $invoice
2016-03-01 03:57:59 +01:00
* @param $type
*
2016-03-01 03:57:59 +01:00
* @return string
*/
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
/**
* @param Invoice $invoice
2016-03-01 03:57:59 +01:00
* @return string
*/
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))
return trans('texts.notification_quote_sent_subject', ['invoice' => $invoice->invoice_number, 'client' => $invoice->client->name]);
2016-03-01 01:31:16 +01:00
else
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 $invoice
2016-03-01 03:57:59 +01:00
* @return string
*/
private function invoicePaidMessage(Invoice $invoice)
2016-03-01 01:31: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 $invoice
2016-03-01 03:57:59 +01:00
* @return string
*/
private function quoteApprovedMessage(Invoice $invoice)
2016-03-01 01:31: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 $invoice
2016-03-01 03:57:59 +01:00
* @return string
*/
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))
return trans('texts.notification_quote_viewed_subject', ['invoice' => $invoice->invoice_number, 'client' => $invoice->client->name]);
2016-03-01 01:31:16 +01:00
else
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
}