Passed
Push — master ( d3fe5e...a81e51 )
by Alex
02:04
created

Transaction::affordable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Nxmad\Larapay\Models;
4
5
use Illuminate\Database\Eloquent\Model;
6
7
class Transaction extends Model
8
{
9
    /**
10
     * The state means user failed payment for Transaction.
11
     */
12
    const STATE_FAILED = 'failed';
13
14
    /**
15
     * The state means Transaction in pending.
16
     */
17
    const STATE_PENDING = 'pending';
18
19
    /**
20
     * The state means Transaction was canceled by user or admin.
21
     */
22
    const STATE_CANCELED = 'canceled';
23
24
    /**
25
     * The state means Transaction succeed.
26
     */
27
    const STATE_SUCCESSFUL = 'successful';
28
29
    /**
30
     * The allowed Transaction states.
31
     *
32
     * @var array
33
     */
34
    static $allowedStates = [
35
        self::STATE_FAILED,
36
        self::STATE_PENDING,
37
        self::STATE_CANCELED,
38
        self::STATE_SUCCESSFUL,
39
    ];
40
41
    /**
42
     * The attributes that are mass assignable.
43
     *
44
     * @var array
45
     */
46
    protected $fillable = ['amount', 'meta', 'state', 'subject_id', 'subject_type'];
47
48
    /**
49
     * The attributes that should be cast to native types.
50
     *
51
     * @var array
52
     */
53
    protected $casts = [
54
        'meta' => 'object'
55
    ];
56
57
    /**
58
     * Determine if Subjects's balance is enough for transaction.
59
     *
60
     * @return bool
61
     */
62
    public function affordable(): bool {
63
        return $this->subject->canAfford($this);
64
    }
65
66
    /**
67
     * Set subject of transaction.
68
     *
69
     * @param $instance
70
     *
71
     * @return self
72
     */
73
    public function setSubject($instance): self {
74
        $this->subject_id = $instance->id;
0 ignored issues
show
Bug introduced by
The property subject_id does not seem to exist on Nxmad\Larapay\Models\Transaction. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
75
        $this->subject_type = get_class($instance);
0 ignored issues
show
Bug introduced by
The property subject_type does not seem to exist on Nxmad\Larapay\Models\Transaction. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
76
77
        return $this;
78
    }
79
80
    /**
81
     * Get polymorphic relation.
82
     *
83
     * @return \Illuminate\Database\Eloquent\Relations\MorphTo
84
     */
85
    public function subject()
86
    {
87
        return $this->morphTo();
88
    }
89
90
    /**
91
     * Get primary value.
92
     *
93
     * @return mixed
94
     */
95
    public function getPrimaryValue()
96
    {
97
        return $this->{$this->primaryKey};
98
    }
99
100
    /**
101
     * Get Transaction amount.
102
     *
103
     * @return mixed
104
     */
105
    public function getAmount()
106
    {
107
        return $this->amount;
0 ignored issues
show
Bug introduced by
The property amount does not seem to exist on Nxmad\Larapay\Models\Transaction. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
108
    }
109
110
    /**
111
     * Get default payment description.
112
     * You have to override this method.
113
     *
114
     * @return string
115
     */
116
    public function getDescription(): string
117
    {
118
        return 'Payment';
119
    }
120
121
    /**
122
     * Update state, without respecting balance.
123
     *
124
     * @param string|null $state
125
     *
126
     * @return $this
127
     */
128
    public function __invoke(string $state = null)
129
    {
130
        if (! is_null($state)) {
131
            $this->state = $state;
0 ignored issues
show
Bug introduced by
The property state does not seem to exist on Nxmad\Larapay\Models\Transaction. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
132
        }
133
134
        if ($this->state == self::STATE_SUCCESSFUL) {
135
            $this->subject->increment(($this->subject_type)::KEEP, $this->getAttributeFromArray('amount'));
136
        }
137
138
        $this->save();
139
140
        return $this;
141
    }
142
143
    /**
144
     * Catch make* methods.
145
     * E.g. ->makeCanceled();
146
     *
147
     * @param string $method
148
     * @param array $parameters
149
     *
150
     * @return self|mixed
151
     */
152
    public function __call($method, $parameters)
153
    {
154
        if (strpos($method, 'make') !== false) {
155
            $state = mb_strtolower(str_replace('make', '', $method));
156
157
            if (in_array($state, $this->allowedStates)) {
158
                return $this($state);
159
            }
160
        }
161
162
        return parent::__call($method, $parameters);
163
    }
164
}
165