HasWallets::transactions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
ccs 2
cts 2
cp 1
crap 1
1
<?php
2
3
namespace Moecasts\Laravel\Wallet\Traits;
4
5
use Illuminate\Database\Eloquent\ModelNotFoundException;
6
use Illuminate\Database\Eloquent\Relations\MorphMany;
7
use Moecasts\Laravel\Wallet\Exceptions\CurrencyInvalid;
8
use Moecasts\Laravel\Wallet\Interfaces\Product;
9
use Moecasts\Laravel\Wallet\Interfaces\Refundable;
10
use Moecasts\Laravel\Wallet\Models\Transaction;
11
use Moecasts\Laravel\Wallet\Models\Transfer;
12
use Moecasts\Laravel\Wallet\Models\Wallet;
13
14
trait HasWallets {
15 28
    public function wallets(): MorphMany
16
    {
17 28
        return $this->morphMany(Wallet::class, 'holder');
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

17
        return $this->/** @scrutinizer ignore-call */ morphMany(Wallet::class, 'holder');
Loading history...
18
    }
19
20 29
    public function getWallet(string $currency): ?Wallet
21
    {
22 29
        if (! in_array($currency, config('wallet.currencies'))) {
23 1
            throw new CurrencyInvalid(trans('wallet::errors.currency_unsupported'));
0 ignored issues
show
Bug introduced by
It seems like trans('wallet::errors.currency_unsupported') can also be of type array; however, parameter $message of Moecasts\Laravel\Wallet\...yInvalid::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

23
            throw new CurrencyInvalid(/** @scrutinizer ignore-type */ trans('wallet::errors.currency_unsupported'));
Loading history...
24
        }
25
26 28
        $wallet = $this->wallets()->where('currency', $currency)->first();
27
28 28
        if (! $wallet) {
29 28
            $wallet = $this->wallets()->create([
30 28
                'currency' => $currency,
31 28
                'balance' => 0
32
            ]);
33
        }
34
35 28
        return $wallet;
36
    }
37
38 1
    public function transactions(): MorphMany
39
    {
40 1
        return $this->morphMany(Transaction::class, 'holder');
41
    }
42
43 6
    public function transfers(): MorphMany
44
    {
45 6
        return $this->morphMany(Transfer::class, 'from');
46
    }
47
48 2
    public function paid(Product $product, string $action = Transfer::ACTION_PAID)
49
    {
50 2
        $action = [$action];
51
52 2
        $query = $this->transfers();
53
54
        return $query
55 2
            ->where('refund', false)
56 2
            ->where('to_type', $product->getMorphClass())
0 ignored issues
show
Bug introduced by
The method getMorphClass() does not exist on Moecasts\Laravel\Wallet\Interfaces\Product. Since it exists in all sub-types, consider adding an abstract or default implementation to Moecasts\Laravel\Wallet\Interfaces\Product. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

56
            ->where('to_type', $product->/** @scrutinizer ignore-call */ getMorphClass())
Loading history...
57 2
            ->where('to_id', $product->getKey())
0 ignored issues
show
Bug introduced by
The method getKey() does not exist on Moecasts\Laravel\Wallet\Interfaces\Product. Since it exists in all sub-types, consider adding an abstract or default implementation to Moecasts\Laravel\Wallet\Interfaces\Product. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

57
            ->where('to_id', $product->/** @scrutinizer ignore-call */ getKey())
Loading history...
58 2
            ->whereIn('action', $action)
59 2
            ->orderBy('id', 'desc')
60 2
            ->first();
61
    }
62
63 2
    public function refund(Refundable $product, string $action = Transfer::ACTION_PAID)
64
    {
65 2
        $transfer = $this->paid($product, $action);
66
67 2
        if (! $transfer) {
68 2
            throw (new ModelNotFoundException())
69 2
                ->setModel(config('wallet.transfer.model'));
70
        }
71
72
        return \DB::transaction(function () use ($transfer) {
73 1
            $transfer->withdraw->update([
74 1
                'confirmed' => false,
75
            ]);
76
77 1
            $transfer->deposit->update([
78 1
                'confirmed' => false,
79
            ]);
80
81 1
            $transfer->update([
82 1
                'refund' => true,
83
            ]);
84
85 1
            return $transfer->fromWallet->refreshBalance() &&
86 1
                $transfer->toWallet->refreshBalance();
87 1
        });
88
    }
89
90 1
    public function safeRefund(Product $product, string $action = Transfer::ACTION_PAID)
91
    {
92
        try {
93 1
            return $this->refund($product, $action);
94 1
        } catch (\Throwable $throwable) {
95 1
            return false;
96
        }
97
    }
98
}
99