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

100 lines
2.5 KiB
PHP
Raw Normal View History

2022-03-09 08:40:45 +01:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Jobs\Ninja;
2022-03-09 10:32:46 +01:00
use App\DataMapper\Transactions\MarkPaidTransaction;
2022-03-09 08:40:45 +01:00
use App\Libraries\MultiDB;
use App\Models\TransactionEvent;
use App\Utils\Ninja;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class TransactionLog implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
private array $payload;
private string $db;
2022-03-09 10:32:46 +01:00
private array $data;
private $event;
private $event_transformer;
private array $transformer_array = [
TransactionEvent::INVOICE_MARK_PAID => MarkPaidTransaction::class,
TransactionEvent::INVOICE_UPDATED => MarkPaidTransaction::class,
TransactionEvent::INVOICE_DELETED => MarkPaidTransaction::class,
TransactionEvent::INVOICE_PAYMENT_APPLIED => MarkPaidTransaction::class,
TransactionEvent::INVOICE_CANCELLED => MarkPaidTransaction::class,
TransactionEvent::INVOICE_FEE_APPLIED => MarkPaidTransaction::class,
TransactionEvent::PAYMENT_MADE => MarkPaidTransaction::class,
TransactionEvent::PAYMENT_APPLIED => MarkPaidTransaction::class,
TransactionEvent::PAYMENT_REFUND => MarkPaidTransaction::class,
TransactionEvent::PAYMENT_FAILED => MarkPaidTransaction::class,
];
2022-03-09 08:40:45 +01:00
/**
* Create a new job instance.
*
* @return void
*/
2022-03-09 10:32:46 +01:00
public function __construct($event, $data, $db)
2022-03-09 08:40:45 +01:00
{
$this->db = $db;
2022-03-09 10:32:46 +01:00
$this->event = $event;
$this->data = $data;
2022-03-09 08:40:45 +01:00
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
if(!Ninja::isHosted())
return;
2022-03-09 10:32:46 +01:00
$this->setTransformer();
$this->payload = $this->event_transformer->transform($this->data);
$this->persist();
}
private function setTransformer()
{
$class = $this->transformer_array[$this->event];
$this->event_transformer = new $class();
return $this;
2022-03-09 08:40:45 +01:00
}
private function persist()
{
MultiDB::setDB($this->db);
TransactionEvent::create($this->payload);
}
}