1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-22 17:31:35 +02:00
invoiceninja/app/Services/PushService.php
Hillel Coren b233aa0744 Merge branch 'release-2.6'
Conflicts:
	.travis.yml
	CONTRIBUTING.md
	README.md
	app/Http/Controllers/AccountController.php
	app/Http/Controllers/AppController.php
	app/Http/Controllers/BaseAPIController.php
	app/Http/Controllers/CreditController.php
	app/Http/Controllers/DashboardController.php
	app/Http/Controllers/DocumentController.php
	app/Http/Controllers/PaymentController.php
	app/Http/Controllers/QuoteApiController.php
	app/Http/Requests/EntityRequest.php
	app/Http/routes.php
	app/Listeners/AnalyticsListener.php
	app/Models/Account.php
	app/Models/Credit.php
	app/Models/Document.php
	app/Models/EntityModel.php
	app/Models/Expense.php
	app/Models/Invoice.php
	app/Models/Task.php
	app/Models/VendorContact.php
	app/Ninja/Notifications/PushFactory.php
	app/Ninja/Repositories/DocumentRepository.php
	app/Ninja/Repositories/ExpenseRepository.php
	app/Ninja/Repositories/InvoiceRepository.php
	app/Services/PushService.php
	composer.json
	composer.lock
	database/migrations/2014_05_17_175626_add_quotes.php
	database/seeds/UserTableSeeder.php
	resources/lang/en/texts.php
	resources/views/accounts/customize_design.blade.php
	resources/views/accounts/invoice_design.blade.php
	resources/views/accounts/management.blade.php
	resources/views/expenses/edit.blade.php
	resources/views/header.blade.php
	resources/views/invoices/edit.blade.php
	resources/views/master.blade.php
	resources/views/payments/payment.blade.php
	resources/views/users/edit.blade.php
2016-07-11 23:33:58 +03:00

158 lines
4.1 KiB
PHP

<?php
namespace App\Services;
use App\Models\Account;
use App\Models\Invoice;
use App\Ninja\Notifications\PushFactory;
/**
* Class PushService
*/
class PushService
{
/**
* @var PushFactory
*/
protected $pushFactory;
/**
* @param PushFactory $pushFactory
*/
public function __construct(PushFactory $pushFactory)
{
$this->pushFactory = $pushFactory;
}
/**
* @param Invoice $invoice
* @param $type
*/
public function sendNotification(Invoice $invoice, $type)
{
if (! IOS_PUSH_CERTIFICATE) {
return;
}
//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
$devices = json_decode($invoice->account->devices, TRUE);
foreach($devices as $device)
{
if(($device["notify_{$type}"] == TRUE) && ($device['device'] == 'ios'))
$this->pushMessage($invoice, $device['token'], $type);
}
}
/**
* pushMessage function
*
* method to dispatch iOS notifications
*
* @param Invoice $invoice
* @param $token
* @param $type
*/
private function pushMessage(Invoice $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 $account
*
* @return bool
*/
private function checkDeviceExists(Account $account)
{
$devices = json_decode($account->devices, TRUE);
if(count($devices) >= 1)
return TRUE;
else
return FALSE;
}
/**
* messageType function
*
* method which formats an appropriate message depending on message type
*
* @param Invoice $invoice
* @param $type
*
* @return string
*/
private function messageType(Invoice $invoice, $type)
{
switch($type)
{
case 'sent':
return $this->entitySentMessage($invoice);
break;
case 'paid':
return $this->invoicePaidMessage($invoice);
break;
case 'approved':
return $this->quoteApprovedMessage($invoice);
break;
case 'viewed':
return $this->entityViewedMessage($invoice);
break;
}
}
/**
* @param Invoice $invoice
* @return string
*/
private function entitySentMessage(Invoice $invoice)
{
if($invoice->isType(INVOICE_TYPE_QUOTE))
return trans('texts.notification_quote_sent_subject', ['invoice' => $invoice->invoice_number, 'client' => $invoice->client->name]);
else
return trans('texts.notification_invoice_sent_subject', ['invoice' => $invoice->invoice_number, 'client' => $invoice->client->name]);
}
/**
* @param Invoice $invoice
* @return string
*/
private function invoicePaidMessage(Invoice $invoice)
{
return trans('texts.notification_invoice_paid_subject', ['invoice' => $invoice->invoice_number, 'client' => $invoice->client->name]);
}
/**
* @param Invoice $invoice
* @return string
*/
private function quoteApprovedMessage(Invoice $invoice)
{
return trans('texts.notification_quote_approved_subject', ['invoice' => $invoice->invoice_number, 'client' => $invoice->client->name]);
}
/**
* @param Invoice $invoice
* @return string
*/
private function entityViewedMessage(Invoice $invoice)
{
if($invoice->isType(INVOICE_TYPE_QUOTE))
return trans('texts.notification_quote_viewed_subject', ['invoice' => $invoice->invoice_number, 'client' => $invoice->client->name]);
else
return trans('texts.notification_invoice_viewed_subject', ['invoice' => $invoice->invoice_number, 'client' => $invoice->client->name]);
}
}