Move the model callbacks into Observables; reduce caching since it held balances incorrectly
This commit is contained in:
20
app/Models/Observers/AircraftObserver.php
Normal file
20
app/Models/Observers/AircraftObserver.php
Normal file
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Observers;
|
||||
|
||||
use App\Models\Aircraft;
|
||||
use App\Support\ICAO;
|
||||
|
||||
/**
|
||||
* Class AircraftObserver
|
||||
* @package App\Models\Observers
|
||||
*/
|
||||
class AircraftObserver
|
||||
{
|
||||
public function creating(Aircraft $aircraft): void
|
||||
{
|
||||
if (empty($aircraft->hex_code)) {
|
||||
$aircraft->hex_code = ICAO::createHexCode();
|
||||
}
|
||||
}
|
||||
}
|
||||
38
app/Models/Observers/AirportObserver.php
Normal file
38
app/Models/Observers/AirportObserver.php
Normal file
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Observers;
|
||||
|
||||
use App\Models\Airport;
|
||||
|
||||
/**
|
||||
* Make sure that the fields are properly capitalized
|
||||
* @package App\Models\Observers
|
||||
*/
|
||||
class AirportObserver
|
||||
{
|
||||
/**
|
||||
* @param Airport $airport
|
||||
*/
|
||||
public function creating(Airport $airport): void
|
||||
{
|
||||
if (filled($airport->iata)) {
|
||||
$airport->iata = strtoupper(trim($airport->iata));
|
||||
}
|
||||
|
||||
$airport->icao = strtoupper(trim($airport->icao));
|
||||
$airport->id = $airport->icao;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Airport $airport
|
||||
*/
|
||||
public function updating(Airport $airport): void
|
||||
{
|
||||
if (filled($airport->iata)) {
|
||||
$airport->iata = strtoupper(trim($airport->iata));
|
||||
}
|
||||
|
||||
$airport->icao = strtoupper(trim($airport->icao));
|
||||
$airport->id = $airport->icao;
|
||||
}
|
||||
}
|
||||
22
app/Models/Observers/JournalObserver.php
Normal file
22
app/Models/Observers/JournalObserver.php
Normal file
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Observers;
|
||||
|
||||
use App\Models\Journal;
|
||||
|
||||
/**
|
||||
* Class JournalObserver
|
||||
* @package App\Models\Observers
|
||||
*/
|
||||
class JournalObserver
|
||||
{
|
||||
/**
|
||||
* @param Journal $journal
|
||||
* @throws \UnexpectedValueException
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public function creating(Journal $journal): void
|
||||
{
|
||||
$journal->balance = 0;
|
||||
}
|
||||
}
|
||||
60
app/Models/Observers/JournalTransactionObserver.php
Normal file
60
app/Models/Observers/JournalTransactionObserver.php
Normal file
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Observers;
|
||||
|
||||
use App\Models\JournalTransaction;
|
||||
|
||||
class JournalTransactionObserver
|
||||
{
|
||||
/**
|
||||
* Set the ID to a UUID
|
||||
* @param JournalTransaction $transaction
|
||||
*/
|
||||
public function creating(JournalTransaction $transaction): void
|
||||
{
|
||||
if (!$transaction->id) {
|
||||
$transaction->id = \Ramsey\Uuid\Uuid::uuid4()->toString();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* After transaction is saved, adjust the journal balance
|
||||
* @param JournalTransaction $transaction
|
||||
*/
|
||||
public function saved(JournalTransaction $transaction): void
|
||||
{
|
||||
$journal = $transaction->journal;
|
||||
if ($transaction['credit']) {
|
||||
$balance = $journal->balance->toAmount();
|
||||
$journal->balance = (int) $balance + $transaction->credit;
|
||||
}
|
||||
|
||||
if ($transaction['debit']) {
|
||||
$balance = $journal->balance->toAmount();
|
||||
#$journal->balance = $journal->balance->subtract($transaction['debit']);
|
||||
$journal->balance = (int) $balance - $transaction->debit;
|
||||
}
|
||||
|
||||
$journal->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* After transaction is deleted, adjust the balance on the journal
|
||||
* @param JournalTransaction $transaction
|
||||
*/
|
||||
public function deleted(JournalTransaction $transaction): void
|
||||
{
|
||||
$journal = $transaction->journal;
|
||||
if ($transaction['credit']) {
|
||||
$balance = $journal->balance->toAmount();
|
||||
$journal->balance = $balance - $transaction['credit'];
|
||||
}
|
||||
|
||||
if ($transaction['debit']) {
|
||||
$balance = $journal->balance->toAmount();
|
||||
$journal->balance = $balance + $transaction['debit'];
|
||||
}
|
||||
|
||||
$journal->save();
|
||||
}
|
||||
}
|
||||
28
app/Models/Observers/PirepFieldObserver.php
Normal file
28
app/Models/Observers/PirepFieldObserver.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Observers;
|
||||
|
||||
use App\Models\PirepField;
|
||||
|
||||
/**
|
||||
* Class PirepFieldObserver
|
||||
* @package App\Models\Observers
|
||||
*/
|
||||
class PirepFieldObserver
|
||||
{
|
||||
/**
|
||||
* @param PirepField $model
|
||||
*/
|
||||
public function creating(PirepField $model): void
|
||||
{
|
||||
$model->slug = str_slug($model->name);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param PirepField $model
|
||||
*/
|
||||
public function updating(PirepField $model): void
|
||||
{
|
||||
$model->slug = str_slug($model->name);
|
||||
}
|
||||
}
|
||||
18
app/Models/Observers/SettingObserver.php
Normal file
18
app/Models/Observers/SettingObserver.php
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Observers;
|
||||
|
||||
use App\Models\Setting;
|
||||
|
||||
class SettingObserver
|
||||
{
|
||||
/**
|
||||
* @param Setting $model
|
||||
*/
|
||||
public function creating(Setting $model): void
|
||||
{
|
||||
if (!empty($model->id)) {
|
||||
$model->id = Setting::formatKey($model->id);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user