nxmad /
Larapay
| 1 | <?php |
||||
| 2 | |||||
| 3 | declare(strict_types=1); |
||||
| 4 | |||||
| 5 | namespace Nxmad\Larapay\Traits; |
||||
| 6 | |||||
| 7 | use Nxmad\Larapay\Models\Transaction; |
||||
| 8 | |||||
| 9 | trait HasTransactions |
||||
| 10 | { |
||||
| 11 | /** |
||||
| 12 | * Setup transaction for subject without saving to database. |
||||
| 13 | * |
||||
| 14 | * @param float $amount |
||||
| 15 | * @param array $meta |
||||
| 16 | * @param string $state |
||||
| 17 | * |
||||
| 18 | * @return mixed |
||||
| 19 | */ |
||||
| 20 | public function setup(float $amount, $meta = [], string $state = Transaction::PENDING): Transaction |
||||
| 21 | { |
||||
| 22 | $class = $this->getTransactionClass(); |
||||
| 23 | $meta = is_scalar($meta) ? ['description' => $meta] : $meta; |
||||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||||
| 24 | |||||
| 25 | /** |
||||
| 26 | * @var Transaction |
||||
| 27 | */ |
||||
| 28 | $transaction = (new $class) |
||||
| 29 | ->setSubject($this) |
||||
| 30 | ->fill(compact('amount', 'meta', 'state')); |
||||
| 31 | |||||
| 32 | return $transaction; |
||||
| 33 | } |
||||
| 34 | |||||
| 35 | /** |
||||
| 36 | * Setup transaction and save it immediately. |
||||
| 37 | * |
||||
| 38 | * @param float $amount |
||||
| 39 | * @param array $meta |
||||
| 40 | * @param string $state |
||||
| 41 | * |
||||
| 42 | * @return Transaction |
||||
| 43 | */ |
||||
| 44 | public function transaction(float $amount, array $meta = [], string $state = Transaction::PENDING): Transaction |
||||
| 45 | { |
||||
| 46 | $transaction = $this->setup(...func_get_args()); |
||||
|
0 ignored issues
–
show
func_get_args() is expanded, but the parameter $amount of Nxmad\Larapay\Traits\HasTransactions::setup() does not expect variable arguments.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 47 | |||||
| 48 | return $transaction($state); |
||||
| 49 | } |
||||
| 50 | |||||
| 51 | /** |
||||
| 52 | * Determine if Model's balance is enough for transaction |
||||
| 53 | * |
||||
| 54 | * @param Transaction $transaction |
||||
| 55 | * |
||||
| 56 | * @return bool |
||||
| 57 | */ |
||||
| 58 | public function canAfford(Transaction $transaction): bool |
||||
| 59 | { |
||||
| 60 | $balance = self::KEEP ? $this->attributes[self::KEEP] : $this->recalculateBalance(); |
||||
|
0 ignored issues
–
show
|
|||||
| 61 | |||||
| 62 | return $balance >= abs($transaction->amount); |
||||
|
0 ignored issues
–
show
|
|||||
| 63 | } |
||||
| 64 | |||||
| 65 | /** |
||||
| 66 | * Recalculate subject's balance. |
||||
| 67 | * |
||||
| 68 | * @return mixed |
||||
| 69 | */ |
||||
| 70 | public function recalculateBalance() |
||||
| 71 | { |
||||
| 72 | return $this->transactions() |
||||
| 73 | ->where('state', Transaction::SUCCESSFUL) |
||||
| 74 | ->get() |
||||
| 75 | ->sum('amount'); |
||||
| 76 | } |
||||
| 77 | |||||
| 78 | /** |
||||
| 79 | * Get internal Transaction class name |
||||
| 80 | * |
||||
| 81 | * @return string |
||||
| 82 | */ |
||||
| 83 | protected function getTransactionClass(): string |
||||
| 84 | { |
||||
| 85 | return app()->config->get('larapay.transaction'); |
||||
|
0 ignored issues
–
show
The function
app was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 86 | } |
||||
| 87 | |||||
| 88 | /** |
||||
| 89 | * Get primary value. |
||||
| 90 | * |
||||
| 91 | * @return mixed |
||||
| 92 | */ |
||||
| 93 | public function getPrimaryValue() |
||||
| 94 | { |
||||
| 95 | return $this->{$this->primaryKey}; |
||||
| 96 | } |
||||
| 97 | |||||
| 98 | /** |
||||
| 99 | * Get polymorphic relation. |
||||
| 100 | * |
||||
| 101 | * @return \Illuminate\Database\Eloquent\Relations\MorphMany |
||||
| 102 | */ |
||||
| 103 | public function transactions() |
||||
| 104 | { |
||||
| 105 | return $this->morphMany($this->getTransactionClass(), 'subject'); |
||||
|
0 ignored issues
–
show
It seems like
morphMany() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 106 | } |
||||
| 107 | } |
||||
| 108 |