Passed
Push — master ( b8b2bd...fcde1f )
by Alex
02:15
created

Transactions::setup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 3
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Nxmad\Larapay\Traits;
4
5
use Nxmad\Larapay\Models\Transaction;
0 ignored issues
show
Bug introduced by
The type Nxmad\Larapay\Models\Transaction was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
7
trait Transactions
8
{
9
    /**
10
     * Setup transaction for subject without saving to database.
11
     *
12
     * @param float  $amount
13
     * @param array  $meta
14
     * @param string $state
15
     *
16
     * @return mixed
17
     */
18
    public function setup(float $amount, array $meta = [], string $state = Transaction::STATE_PENDING)
19
    {
20
        $subject_type = __CLASS__;
21
        $subject_id   = $this->getPrimaryValue();
22
        $class        = $this->getTransactionClass();
23
24
        return (new $class)->fill(compact('subject_id', 'subject_type', 'amount', 'meta', 'state'));
25
    }
26
27
    /**
28
     * Get new transaction record in database.
29
     *
30
     * @param float  $amount
31
     * @param array  $meta
32
     * @param string $state
33
     *
34
     * @return Transaction
35
     */
36
    public function transaction(float $amount, array $meta = [], string $state = Transaction::STATE_PENDING): Transaction
37
    {
38
        $transaction = $this->setup(...func_get_args());
0 ignored issues
show
Bug introduced by
func_get_args() is expanded, but the parameter $amount of Nxmad\Larapay\Traits\Transactions::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 ignore-type  annotation

38
        $transaction = $this->setup(/** @scrutinizer ignore-type */ ...func_get_args());
Loading history...
39
40
        return $transaction($state);
41
    }
42
43
    /**
44
     * Determine if Model's balance is enough for transaction
45
     *
46
     * @param Transaction $transaction
47
     *
48
     * @return bool
49
     */
50
    public function canAfford(Transaction $transaction): bool
51
    {
52
        $field = self::KEEP;
0 ignored issues
show
Bug introduced by
The constant Nxmad\Larapay\Traits\Transactions::KEEP was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
53
54
        if (! $field) {
55
            return true;
56
        }
57
58
        return $this->attributes[$field] >= abs($transaction->getAttributeFromArray('amount'));
59
    }
60
61
    /**
62
     * Recalculate subject's balance.
63
     *
64
     * @return mixed
65
     */
66
    public function recalculateBalance()
67
    {
68
        return $this->transactions()
69
            ->where('state', Transaction::STATE_SUCCESSFUL)
70
            ->get()
71
            ->sum('amount');
72
    }
73
74
    /**
75
     * Get internal Transaction class name
76
     *
77
     * @return string
78
     */
79
    protected function getTransactionClass(): string
80
    {
81
        return app()->config->get('larapay.transaction');
82
    }
83
84
    /**
85
     * Get primary value.
86
     *
87
     * @return mixed
88
     */
89
    public function getPrimaryValue()
90
    {
91
        return $this->{$this->primaryKey};
92
    }
93
94
    /**
95
     * Get polymorphic relation.
96
     *
97
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
98
     */
99
    public function transactions()
100
    {
101
        return $this->morphMany($this->getTransactionClass(), 'subject');
0 ignored issues
show
Bug introduced by
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 ignore-call  annotation

101
        return $this->/** @scrutinizer ignore-call */ morphMany($this->getTransactionClass(), 'subject');
Loading history...
102
    }
103
}
104