GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 2b9c43...6e1047 )
by Chris
14s queued 13s
created

Wallet::walletLedgers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace CoreProc\WalletPlus\Models;
4
5
use CoreProc\WalletPlus\Contracts\WalletTransaction;
6
use Exception;
7
use Illuminate\Database\Eloquent\Model;
8
9
class Wallet extends Model
10
{
11
    protected $fillable = [
12
        'user_id',
13
        'user_type',
14
        'wallet_type_id',
15
        'raw_balance',
16
    ];
17
18
    public function user()
19
    {
20
        return $this->morphTo('user');
21
    }
22
23
    public function walletType()
24
    {
25
        return $this->belongsTo(WalletType::class);
26
    }
27
    
28
    public function walletLedgers()
29
    {
30
        return $this->hasMany(WalletLedger::class);
31
    }
32
33
    public function getBalanceAttribute()
34
    {
35
        if (empty($this->walletType->decimals)) {
36
            return $this->raw_balance;
37
        }
38
39
        return $this->raw_balance / pow(10, $this->walletType->decimals);
40
    }
41
42
    /**
43
     * @param $transaction WalletTransaction|integer|float|double
44
     * @return Wallet
45
     * @throws Exception
46
     */
47 View Code Duplication
    public function incrementBalance($transaction)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
48
    {
49
        if (is_numeric($transaction)) {
50
            $amount = $this->convertToWalletTypeInteger($transaction);
51
            $this->increment('raw_balance', $amount);
52
            $this->createWalletLedgerEntry($amount, $this->raw_balance);
53
54
            return $this;
55
        }
56
57
        if (! $transaction instanceof WalletTransaction) {
58
            throw new Exception('Increment balance expects parameter to be a float or a WalletTransaction object.');
59
        }
60
61
        $this->increment('raw_balance', $transaction->getAmount());
62
63
        // Record in ledger
64
        $this->createWalletLedgerEntry($transaction, $this->raw_balance);
65
66
        return $this;
67
    }
68
69
    /**
70
     * @param $transaction WalletTransaction|integer|float|double
71
     * @return Wallet
72
     * @throws Exception
73
     */
74 View Code Duplication
    public function decrementBalance($transaction)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
75
    {
76
        if (is_numeric($transaction)) {
77
            $amount = $this->convertToWalletTypeInteger($transaction);
78
            $this->decrement('raw_balance', $amount);
79
            $this->createWalletLedgerEntry($amount, $this->raw_balance, 'decrement');
80
81
            return $this;
82
        }
83
84
        if (! $transaction instanceof WalletTransaction) {
85
            throw new Exception('Decrement balance expects parameter to be a number or a WalletTransaction object.');
86
        }
87
88
        $this->decrement('raw_balance', $transaction->getAmount());
89
90
        // Record in ledger
91
        $this->createWalletLedgerEntry($transaction, $this->raw_balance, 'decrement');
92
93
        return $this;
94
    }
95
96
    /**
97
     * @param $transaction
98
     * @param $newRunningRawBalance
99
     * @param string $type
100
     * @return mixed
101
     * @throws Exception
102
     */
103
    private function createWalletLedgerEntry($transaction, $newRunningRawBalance, $type = 'increment')
104
    {
105
        if (is_numeric($transaction)) {
106
            if ($type === 'decrement') {
107
                $transaction = -$transaction;
108
            }
109
110
            return WalletLedger::query()->create([
111
                'wallet_id' => $this->id,
112
                'amount' => $transaction,
113
                'running_raw_balance' => $newRunningRawBalance,
114
            ]);
115
        }
116
117
        if (! $transaction instanceof WalletTransaction) {
118
            throw new Exception('Wallet ledger entries expect first parameter to be numeric or a WalletTransaction ' .
119
                'instance');
120
        }
121
122
        $amount = $this->convertToWalletTypeInteger($transaction->getAmount());
123
124
        if ($type === 'decrement') {
125
            $amount = -$amount;
126
        }
127
128
        return WalletLedger::query()->create([
129
            'wallet_id' => $this->id,
130
            'transaction_id' => $transaction->id,
0 ignored issues
show
Bug introduced by
Accessing id on the interface CoreProc\WalletPlus\Contracts\WalletTransaction suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
131
            'transaction_type' => get_class($transaction),
132
            'amount' => $amount,
133
            'running_raw_balance' => $newRunningRawBalance,
134
        ]);
135
    }
136
137
    /**
138
     * Converts the given value to an integer that is compatible with this wallet's type.
139
     *
140
     * @param int $value
141
     * @return float|int
142
     */
143
    private function convertToWalletTypeInteger($value)
144
    {
145
        if (empty($this->walletType) || $this->walletType->decimals === 0) {
146
            return $value;
147
        }
148
149
        return (int)($value * pow(10, $this->walletType->decimals));
150
    }
151
}
152