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

CartPay::forceRefundGiftCart()   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 Bavix\Wallet\Objects\Cart;
9
use Illuminate\Database\Eloquent\Model;
10
use Illuminate\Support\Facades\DB;
11
use Throwable;
12
use function array_unique;
13
use function count;
14
15
trait CartPay
16
{
17
18
    use HasWallet;
19
20
    /**
21
     * @param Cart $cart
22
     * @return Transfer[]
23
     * @throws
24
     */
25 2
    public function payFreeCart(Cart $cart): array
26
    {
27 2
        if (!$cart->canBuy($this)) {
0 ignored issues
show
Bug introduced by
$this of type Bavix\Wallet\Traits\CartPay is incompatible with the type Bavix\Wallet\Interfaces\Customer expected by parameter $customer of Bavix\Wallet\Objects\Cart::canBuy(). ( Ignorable by Annotation )

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

27
        if (!$cart->canBuy(/** @scrutinizer ignore-type */ $this)) {
Loading history...
28 1
            throw new ProductEnded(trans('wallet::errors.product_stock'));
0 ignored issues
show
Bug introduced by
It seems like trans('wallet::errors.product_stock') can also be of type array; however, parameter $message of Bavix\Wallet\Exceptions\...uctEnded::__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

28
            throw new ProductEnded(/** @scrutinizer ignore-type */ trans('wallet::errors.product_stock'));
Loading history...
29
        }
30
31
        return DB::transaction(function () use ($cart) {
32 2
            $results = [];
33 2
            foreach ($cart->getItems() as $product) {
34 2
                $results[] = $this->transfer(
35 2
                    $product,
36 2
                    0,
37 2
                    $product->getMetaProduct(),
38 2
                    Transfer::STATUS_PAID
39
                );
40
            }
41
42 2
            return $results;
43 2
        });
44
    }
45
46
    /**
47
     * @param Cart $cart
48
     * @param bool $force
49
     * @return Transfer[]
50
     */
51 1
    public function safePayCart(Cart $cart, bool $force = null): array
52
    {
53
        try {
54 1
            return $this->payCart($cart, $force);
55 1
        } catch (Throwable $throwable) {
56 1
            return [];
57
        }
58
    }
59
60
    /**
61
     * @param Cart $cart
62
     * @param bool $force
63
     * @return Transfer[]
64
     * @throws
65
     */
66 8
    public function payCart(Cart $cart, bool $force = null): array
67
    {
68 8
        if (!$cart->canBuy($this, $force)) {
0 ignored issues
show
Bug introduced by
$this of type Bavix\Wallet\Traits\CartPay is incompatible with the type Bavix\Wallet\Interfaces\Customer expected by parameter $customer of Bavix\Wallet\Objects\Cart::canBuy(). ( Ignorable by Annotation )

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

68
        if (!$cart->canBuy(/** @scrutinizer ignore-type */ $this, $force)) {
Loading history...
69 2
            throw new ProductEnded(trans('wallet::errors.product_stock'));
0 ignored issues
show
Bug introduced by
It seems like trans('wallet::errors.product_stock') can also be of type array; however, parameter $message of Bavix\Wallet\Exceptions\...uctEnded::__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

69
            throw new ProductEnded(/** @scrutinizer ignore-type */ trans('wallet::errors.product_stock'));
Loading history...
70
        }
71
72
        return DB::transaction(function () use ($cart, $force) {
73
74 8
            $results = [];
75 8
            foreach ($cart->getItems() as $product) {
76 8
                if ($force) {
77 1
                    $results[] = $this->forceTransfer(
78 1
                        $product,
79 1
                        $product->getAmountProduct(),
80 1
                        $product->getMetaProduct(),
81 1
                        Transfer::STATUS_PAID
82
                    );
83
84 1
                    continue;
85
                }
86
87 7
                $results[] = $this->transfer(
88 7
                    $product,
89 7
                    $product->getAmountProduct(),
90 7
                    $product->getMetaProduct(),
91 7
                    Transfer::STATUS_PAID
92
                );
93
            }
94
95 8
            return $results;
96
97 8
        });
98
    }
99
100
    /**
101
     * @param Cart $cart
102
     * @return Transfer[]
103
     * @throws
104
     */
105 1
    public function forcePayCart(Cart $cart): array
106
    {
107 1
        return $this->payCart($cart, true);
108
    }
109
110
    /**
111
     * @param Cart $cart
112
     * @param bool $force
113
     * @param bool $gifts
114
     * @return bool
115
     */
116 3
    public function safeRefundCart(Cart $cart, bool $force = null, bool $gifts = null): bool
117
    {
118
        try {
119 3
            return $this->refundCart($cart, $force, $gifts);
120 3
        } catch (Throwable $throwable) {
121 3
            return false;
122
        }
123
    }
124
125
    /**
126
     * @param Cart $cart
127
     * @param bool $force
128
     * @param bool $gifts
129
     * @return bool
130
     * @throws
131
     */
132 7
    public function refundCart(Cart $cart, bool $force = null, bool $gifts = null): bool
133
    {
134
        return DB::transaction(function () use ($cart, $force, $gifts) {
135
136 7
            $results = [];
137 7
            $transfers = $cart->hasPaid($this, $gifts);
0 ignored issues
show
Bug introduced by
$this of type Bavix\Wallet\Traits\CartPay is incompatible with the type Bavix\Wallet\Interfaces\Customer expected by parameter $customer of Bavix\Wallet\Objects\Cart::hasPaid(). ( Ignorable by Annotation )

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

137
            $transfers = $cart->hasPaid(/** @scrutinizer ignore-type */ $this, $gifts);
Loading history...
138 7
            foreach ($cart->getItems() as $key => $product) {
139 7
                $transfer = $transfers[$key];
140 7
                $transfer->load('withdraw.wallet');
141
142 7
                if ($force) {
143 2
                    $product->forceTransfer(
144 2
                        $transfer->withdraw->wallet,
145 2
                        $transfer->deposit->amount,
146 2
                        $product->getMetaProduct()
147
                    );
148
                } else {
149 7
                    $product->transfer(
150 7
                        $transfer->withdraw->wallet,
151 7
                        $transfer->deposit->amount,
152 7
                        $product->getMetaProduct()
153
                    );
154
                }
155
156 7
                $results[] = $transfer->update([
157 7
                    'status' => Transfer::STATUS_REFUND,
158 7
                    'status_last' => $transfer->status,
159
                ]);
160
            }
161
162 7
            return count(array_unique($results)) === 1;
163
164 7
        });
165
    }
166
167
    /**
168
     * @param Cart $cart
169
     * @param bool $gifts
170
     * @return bool
171
     * @throws
172
     */
173 1
    public function forceRefundCart(Cart $cart, bool $gifts = null): bool
174
    {
175 1
        return $this->refundCart($cart, true, $gifts);
176
    }
177
178
    /**
179
     * @param Cart $cart
180
     * @param bool $force
181
     * @return bool
182
     */
183 1
    public function safeRefundGiftCart(Cart $cart, bool $force = null): bool
184
    {
185
        try {
186 1
            return $this->refundGiftCart($cart, $force);
187 1
        } catch (Throwable $throwable) {
188 1
            return false;
189
        }
190
    }
191
192
    /**
193
     * @param Cart $cart
194
     * @param bool $force
195
     * @return bool
196
     * @throws
197
     */
198 1
    public function refundGiftCart(Cart $cart, bool $force = null): bool
199
    {
200 1
        return $this->refundCart($cart, $force, true);
201
    }
202
203
    /**
204
     * @param Cart $cart
205
     * @return bool
206
     * @throws
207
     */
208 1
    public function forceRefundGiftCart(Cart $cart): bool
209
    {
210 1
        return $this->refundGiftCart($cart, true);
211
    }
212
213
    /**
214
     * Checks acquired product your wallet.
215
     *
216
     * @param Product $product
217
     * @param bool $gifts
218
     * @return null|Transfer
219
     */
220 11
    public function paid(Product $product, bool $gifts = null): ?Transfer
221
    {
222 11
        $status = [Transfer::STATUS_PAID];
223 11
        if ($gifts) {
224 2
            $status[] = Transfer::STATUS_GIFT;
225
        }
226
227
        /**
228
         * @var Model $product
229
         * @var Transfer $query
230
         */
231 11
        $query = $this->transfers();
232
        return $query
233 11
            ->where('to_type', $product->getMorphClass())
234 11
            ->where('to_id', $product->getKey())
235 11
            ->whereIn('status', $status)
236 11
            ->orderBy('id', 'desc')
237 11
            ->first();
238
    }
239
240
}
241