1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 21:22:58 +01:00
invoiceninja/app/Services/PushService.php

164 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 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
/**
2017-01-30 20:40:43 +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
{
//check user has registered for push notifications
2017-01-30 20:40:43 +01:00
if (! $this->checkDeviceExists($invoice->account)) {
2016-02-29 12:21:15 +01:00
return;
2017-01-30 17:05:31 +01:00
}
2016-02-29 12:21:15 +01:00
//Harvest an array of devices that are registered for this notification type
2017-01-30 17:05:31 +01:00
$devices = json_decode($invoice->account->devices, true);
2016-03-01 01:31:16 +01:00
2017-01-30 17:05:31 +01:00
foreach ($devices as $device) {
if (($device["notify_{$type}"] == true) && ($device['device'] == 'ios') && IOS_DEVICE) {
$this->pushMessage($invoice, $device['token'], $type, IOS_DEVICE);
2017-01-30 17:05:31 +01:00
} elseif (($device["notify_{$type}"] == true) && ($device['device'] == 'fcm') && ANDROID_DEVICE) {
$this->pushMessage($invoice, $device['token'], $type, ANDROID_DEVICE);
2017-01-30 17:05:31 +01:00
}
2016-03-01 01:31:16 +01:00
}
2016-02-29 12:21:15 +01:00
}
2016-03-01 03:57:59 +01:00
/**
2017-01-30 20:40:43 +01:00
* pushMessage function.
2016-03-01 04:00:11 +01:00
*
* method to dispatch iOS notifications
*
* @param Invoice $invoice
2016-03-01 03:57:59 +01:00
* @param $token
* @param $type
2017-01-30 20:49:42 +01:00
* @param mixed $device
2016-03-01 03:57:59 +01:00
*/
private function pushMessage(Invoice $invoice, $token, $type, $device)
2016-03-01 01:31:16 +01:00
{
$this->pushFactory->message($token, $this->messageType($invoice, $type), $device);
2016-03-01 01:31:16 +01:00
}
/**
2017-01-30 20:40:43 +01:00
* checkDeviceExists function.
2016-03-01 01:31:16 +01:00
*
* 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
{
2017-01-30 17:05:31 +01:00
$devices = json_decode($account->devices, true);
2016-03-01 01:31:16 +01:00
2017-01-30 17:05:31 +01:00
if (count($devices) >= 1) {
return true;
} else {
return false;
}
2016-03-01 01:31:16 +01:00
}
2016-03-01 03:57:59 +01:00
/**
2017-01-30 20:40:43 +01:00
* messageType function.
2016-03-01 04:00:11 +01:00
*
* 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
{
2017-01-30 17:05:31 +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
2017-01-30 20:40:43 +01:00
*
2016-03-01 03:57:59 +01:00
* @return string
*/
private function entitySentMessage(Invoice $invoice)
2016-03-01 01:31:16 +01:00
{
2017-01-30 17:05:31 +01:00
if ($invoice->isType(INVOICE_TYPE_QUOTE)) {
return trans('texts.notification_quote_sent_subject', ['invoice' => $invoice->invoice_number, 'client' => $invoice->client->name]);
2017-01-30 17:05:31 +01:00
} else {
return trans('texts.notification_invoice_sent_subject', ['invoice' => $invoice->invoice_number, 'client' => $invoice->client->name]);
2017-01-30 17:05:31 +01:00
}
2016-03-01 01:31:16 +01:00
}
2016-03-01 03:57:59 +01:00
/**
* @param Invoice $invoice
2017-01-30 20:40:43 +01:00
*
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
2017-01-30 20:40:43 +01:00
*
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
2017-01-30 20:40:43 +01:00
*
2016-03-01 03:57:59 +01:00
* @return string
*/
private function entityViewedMessage(Invoice $invoice)
2016-03-01 01:31:16 +01:00
{
2017-01-30 17:05:31 +01:00
if ($invoice->isType(INVOICE_TYPE_QUOTE)) {
return trans('texts.notification_quote_viewed_subject', ['invoice' => $invoice->invoice_number, 'client' => $invoice->client->name]);
2017-01-30 17:05:31 +01:00
} else {
return trans('texts.notification_invoice_viewed_subject', ['invoice' => $invoice->invoice_number, 'client' => $invoice->client->name]);
2017-01-30 17:05:31 +01:00
}
2016-03-01 01:31:16 +01:00
}
2017-01-30 17:05:31 +01:00
}