Completed
Pull Request — master (#40)
by Бабичев
03:53
created

CanPay::paid()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 2
nop 2
dl 0
loc 18
ccs 10
cts 10
cp 1
crap 2
rs 9.9332
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 Bavix\Wallet\Objects\Cart;
9
use Illuminate\Database\Eloquent\Model;
10
use Illuminate\Database\Eloquent\ModelNotFoundException;
11
use Illuminate\Support\Facades\DB;
12
13
trait CanPay
14
{
15
16
    use CartPay;
17
18
    /**
19
     * @param Product $product
20
     * @return Transfer
21
     */
22 2
    public function payFree(Product $product): Transfer
23
    {
24 2
        return \current($this->payFreeCart(Cart::make()->addItem($product)));
25
    }
26
27
    /**
28
     * @param Product $product
29
     * @param bool $force
30
     * @return Transfer|null
31
     */
32 1
    public function safePay(Product $product, bool $force = null): ?Transfer
33
    {
34
        try {
35 1
            return $this->pay($product, $force);
36 1
        } catch (\Throwable $throwable) {
37 1
            return null;
38
        }
39
    }
40
41
    /**
42
     * @param Product $product
43
     * @param bool $force
44
     * @return Transfer
45
     * @throws
46
     */
47 6
    public function pay(Product $product, bool $force = null): Transfer
48
    {
49 6
        return \current($this->payCart(Cart::make()->addItem($product), $force));
50
    }
51
52
    /**
53
     * @param Product $product
54
     * @return Transfer
55
     * @throws
56
     */
57 1
    public function forcePay(Product $product): Transfer
58
    {
59 1
        return \current($this->forcePayCart(Cart::make()->addItem($product)));
60
    }
61
62
    /**
63
     * @param Product $product
64
     * @param bool $force
65
     * @param bool $gifts
66
     * @return bool
67
     */
68 3
    public function safeRefund(Product $product, bool $force = null, bool $gifts = null): bool
69
    {
70 3
        return $this->safeRefundCart(Cart::make()->addItem($product), $force, $gifts);
71
    }
72
73
    /**
74
     * @param Product $product
75
     * @param bool $force
76
     * @param bool $gifts
77
     * @return bool
78
     * @throws
79
     */
80 4
    public function refund(Product $product, bool $force = null, bool $gifts = null): bool
81
    {
82 4
        return $this->refundCart(Cart::make()->addItem($product), $force, $gifts);
83
    }
84
85
    /**
86
     * @param Product $product
87
     * @param bool $gifts
88
     * @return bool
89
     * @throws
90
     */
91 1
    public function forceRefund(Product $product, bool $gifts = null): bool
92
    {
93 1
        return $this->forceRefundCart(Cart::make()->addItem($product), $gifts);
94
    }
95
96
    /**
97
     * @param Product $product
98
     * @param bool $force
99
     * @return bool
100
     */
101 1
    public function safeRefundGift(Product $product, bool $force = null): bool
102
    {
103 1
        return $this->safeRefundGiftCart(Cart::make()->addItem($product), $force);
104
    }
105
106
    /**
107
     * @param Product $product
108
     * @param bool $force
109
     * @return bool
110
     * @throws
111
     */
112 1
    public function refundGift(Product $product, bool $force = null): bool
113
    {
114 1
        return $this->refundGiftCart(Cart::make()->addItem($product), $force);
115
    }
116
117
    /**
118
     * @param Product $product
119
     * @return bool
120
     * @throws
121
     */
122 1
    public function forceRefundGift(Product $product): bool
123
    {
124 1
        return $this->forceRefundGiftCart(Cart::make()->addItem($product));
125
    }
126
127
}
128