Passed
Push — master ( 2a11a1...9edf84 )
by Бабичев
56s
created

CanBePaid::forceRefund()   A

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 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Bavix\Wallet\Traits;
4
5
use Bavix\Wallet\Exceptions\ProductEnded;
6
use Bavix\Wallet\Interfaces\Product;
7
use Bavix\Wallet\Models\Transfer;
8
use Illuminate\Database\Eloquent\Model;
9
use Illuminate\Database\Eloquent\ModelNotFoundException;
10
use Illuminate\Support\Facades\DB;
11
12
trait CanBePaid
13
{
14
15
    use HasWallet;
16
17
    /**
18
     * @param Product $product
19
     * @param bool $force
20
     * @return Transfer
21
     * @throws
22
     */
23 5
    public function pay(Product $product, bool $force = false): Transfer
24
    {
25 5
        if (!$product->canBuy($this, $force)) {
0 ignored issues
show
Bug introduced by
$this of type Bavix\Wallet\Traits\CanBePaid is incompatible with the type Bavix\Wallet\Interfaces\Customer expected by parameter $customer of Bavix\Wallet\Interfaces\Product::canBuy(). ( Ignorable by Annotation )

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

25
        if (!$product->canBuy(/** @scrutinizer ignore-type */ $this, $force)) {
Loading history...
26 2
            throw new ProductEnded('The product is out of stock');
27
        }
28
29 5
        if ($force) {
30 1
            return $this->forceTransfer($product, $product->getAmountProduct(), $product->getMetaProduct());
31
        }
32
33 4
        return $this->transfer($product, $product->getAmountProduct(), $product->getMetaProduct());
34
    }
35
36
    /**
37
     * @param Product $product
38
     * @param bool $force
39
     * @return Transfer|null
40
     */
41 1
    public function safePay(Product $product, bool $force = false): ?Transfer
42
    {
43
        try {
44 1
            return $this->pay($product, $force);
45 1
        } catch (\Throwable $throwable) {
46 1
            return null;
47
        }
48
    }
49
50
    /**
51
     * @param Product $product
52
     * @return Transfer
53
     * @throws
54
     */
55 1
    public function forcePay(Product $product): Transfer
56
    {
57 1
        return $this->pay($product, true);
58
    }
59
60
    /**
61
     * @param Product $product
62
     * @return null|Transfer
63
     */
64 4
    public function paid(Product $product): ?Transfer
65
    {
66
        /**
67
         * @var Model $product
68
         */
69 4
        return $this->transfers()
70 4
            ->where('to_type', $product->getMorphClass())
71 4
            ->where('to_id', $product->getKey())
72 4
            ->where('refund', 0)
73 4
            ->orderBy('id', 'desc')
74 4
            ->first();
75
    }
76
77
    /**
78
     * @param Product $product
79
     * @param bool $force
80
     * @return bool
81
     * @throws
82
     */
83 2
    public function refund(Product $product, bool $force = false): bool
84
    {
85 2
        $transfer = $this->paid($product);
86
87 2
        if (!$transfer) {
88 1
            throw (new ModelNotFoundException())
89 1
                ->setModel($this->transfers()->getMorphClass());
90
        }
91
92
        return DB::transaction(function() use ($product, $transfer, $force) {
93 2
            if ($force) {
94 1
                $product->forceTransfer($this, $product->getAmountProduct(), $product->getMetaProduct());
0 ignored issues
show
Bug introduced by
$this of type Bavix\Wallet\Traits\CanBePaid is incompatible with the type Bavix\Wallet\Interfaces\Wallet expected by parameter $wallet of Bavix\Wallet\Interfaces\Wallet::forceTransfer(). ( Ignorable by Annotation )

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

94
                $product->forceTransfer(/** @scrutinizer ignore-type */ $this, $product->getAmountProduct(), $product->getMetaProduct());
Loading history...
95
            } else {
96 2
                $product->transfer($this, $product->getAmountProduct(), $product->getMetaProduct());
0 ignored issues
show
Bug introduced by
$this of type Bavix\Wallet\Traits\CanBePaid is incompatible with the type Bavix\Wallet\Interfaces\Wallet expected by parameter $wallet of Bavix\Wallet\Interfaces\Wallet::transfer(). ( Ignorable by Annotation )

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

96
                $product->transfer(/** @scrutinizer ignore-type */ $this, $product->getAmountProduct(), $product->getMetaProduct());
Loading history...
97
            }
98
99 2
            return $transfer->update(['refund' => 1]);
100 2
        });
101
    }
102
103
    /**
104
     * @param Product $product
105
     * @param bool $force
106
     * @return bool
107
     */
108 2
    public function safeRefund(Product $product, bool $force = false): bool
109
    {
110
        try {
111 2
            return $this->refund($product, $force);
112 2
        } catch (\Throwable $throwable) {
113 2
            return false;
114
        }
115
    }
116
117
    /**
118
     * @param Product $product
119
     * @return bool
120
     * @throws
121
     */
122 1
    public function forceRefund(Product $product): bool
123
    {
124 1
        return $this->refund($product, true);
125
    }
126
127
}
128