Completed
Push — master ( 56dae8...2928d3 )
by Бабичев
12:38 queued 05:37
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 Bavix\Wallet\Services\CommonService;
10
use Illuminate\Database\Eloquent\ModelNotFoundException;
11
use Illuminate\Support\Facades\DB;
12
use Throwable;
13
use function array_unique;
14
use function count;
15
16
trait CartPay
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 2
        app(CommonService::class)
32 2
            ->verifyWithdraw($this, 0);
0 ignored issues
show
Bug introduced by
$this of type Bavix\Wallet\Traits\CartPay is incompatible with the type Bavix\Wallet\Interfaces\Wallet expected by parameter $wallet of Bavix\Wallet\Services\Co...rvice::verifyWithdraw(). ( Ignorable by Annotation )

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

32
            ->verifyWithdraw(/** @scrutinizer ignore-type */ $this, 0);
Loading history...
33
34
        return DB::transaction(function () use ($cart) {
35 2
            $results = [];
36 2
            foreach ($cart->getItems() as $product) {
37 2
                $results[] = app(CommonService::class)->forceTransfer(
38 2
                    $this,
0 ignored issues
show
Bug introduced by
$this of type Bavix\Wallet\Traits\CartPay is incompatible with the type Bavix\Wallet\Interfaces\Wallet expected by parameter $from of Bavix\Wallet\Services\Co...ervice::forceTransfer(). ( Ignorable by Annotation )

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

38
                    /** @scrutinizer ignore-type */ $this,
Loading history...
39 2
                    $product,
40 2
                    0,
41 2
                    $product->getMetaProduct(),
42 2
                    Transfer::STATUS_PAID
43
                );
44
            }
45
46 2
            return $results;
47 2
        });
48
    }
49
50
    /**
51
     * @param Cart $cart
52
     * @param bool $force
53
     * @return Transfer[]
54
     */
55 1
    public function safePayCart(Cart $cart, bool $force = null): array
56
    {
57
        try {
58 1
            return $this->payCart($cart, $force);
59 1
        } catch (Throwable $throwable) {
60 1
            return [];
61
        }
62
    }
63
64
    /**
65
     * @param Cart $cart
66
     * @param bool $force
67
     * @return Transfer[]
68
     * @throws
69
     */
70 10
    public function payCart(Cart $cart, bool $force = null): array
71
    {
72 10
        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

72
        if (!$cart->canBuy(/** @scrutinizer ignore-type */ $this, $force)) {
Loading history...
73 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

73
            throw new ProductEnded(/** @scrutinizer ignore-type */ trans('wallet::errors.product_stock'));
Loading history...
74
        }
75
76
        return DB::transaction(function () use ($cart, $force) {
77 10
            $results = [];
78 10
            foreach ($cart->getItems() as $product) {
79 10
                if ($force) {
80 1
                    $results[] = app(CommonService::class)->forceTransfer(
81 1
                        $this,
0 ignored issues
show
Bug introduced by
$this of type Bavix\Wallet\Traits\CartPay is incompatible with the type Bavix\Wallet\Interfaces\Wallet expected by parameter $from of Bavix\Wallet\Services\Co...ervice::forceTransfer(). ( Ignorable by Annotation )

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

81
                        /** @scrutinizer ignore-type */ $this,
Loading history...
82 1
                        $product,
83 1
                        $product->getAmountProduct(),
84 1
                        $product->getMetaProduct(),
85 1
                        Transfer::STATUS_PAID
86
                    );
87
88 1
                    continue;
89
                }
90
91 9
                $results[] = app(CommonService::class)->transfer(
92 9
                    $this,
0 ignored issues
show
Bug introduced by
$this of type Bavix\Wallet\Traits\CartPay is incompatible with the type Bavix\Wallet\Interfaces\Wallet expected by parameter $from of Bavix\Wallet\Services\CommonService::transfer(). ( Ignorable by Annotation )

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

92
                    /** @scrutinizer ignore-type */ $this,
Loading history...
93 9
                    $product,
94 9
                    $product->getAmountProduct(),
95 9
                    $product->getMetaProduct(),
96 9
                    Transfer::STATUS_PAID
97
                );
98
            }
99
100 10
            return $results;
101 10
        });
102
    }
103
104
    /**
105
     * @param Cart $cart
106
     * @return Transfer[]
107
     * @throws
108
     */
109 1
    public function forcePayCart(Cart $cart): array
110
    {
111 1
        return $this->payCart($cart, true);
112
    }
113
114
    /**
115
     * @param Cart $cart
116
     * @param bool $force
117
     * @param bool $gifts
118
     * @return bool
119
     */
120 3
    public function safeRefundCart(Cart $cart, bool $force = null, bool $gifts = null): bool
121
    {
122
        try {
123 3
            return $this->refundCart($cart, $force, $gifts);
124 3
        } catch (Throwable $throwable) {
125 3
            return false;
126
        }
127
    }
128
129
    /**
130
     * @param Cart $cart
131
     * @param bool $force
132
     * @param bool $gifts
133
     * @return bool
134
     * @throws
135
     */
136 9
    public function refundCart(Cart $cart, bool $force = null, bool $gifts = null): bool
137
    {
138
        return DB::transaction(function () use ($cart, $force, $gifts) {
139 9
            $results = [];
140 9
            $transfers = $cart->alreadyBuy($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::alreadyBuy(). ( Ignorable by Annotation )

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

140
            $transfers = $cart->alreadyBuy(/** @scrutinizer ignore-type */ $this, $gifts);
Loading history...
141 9
            if (count($transfers) !== count($cart)) {
142 3
                throw (new ModelNotFoundException())
143 3
                    ->setModel($this->transfers()->getMorphClass());
144
            }
145
146 8
            foreach ($cart->getItems() as $key => $product) {
147 8
                $transfer = $transfers[$key];
148 8
                $transfer->load('withdraw.wallet');
149
150 8
                if (!$force) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $force of type boolean|null is loosely compared to false; this is ambiguous if the boolean can be false. You might want to explicitly use !== null instead.

If an expression can have both false, and null as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.

$a = canBeFalseAndNull();

// Instead of
if ( ! $a) { }

// Better use one of the explicit versions:
if ($a !== null) { }
if ($a !== false) { }
if ($a !== null && $a !== false) { }
Loading history...
151 8
                    app(CommonService::class)->verifyWithdraw(
152 8
                        $product,
153 8
                        $transfer->deposit->amount
154
                    );
155
                }
156
157 8
                app(CommonService::class)->forceTransfer(
158 8
                    $product,
0 ignored issues
show
Bug introduced by
$product of type Bavix\Wallet\Traits\HasWallet is incompatible with the type Bavix\Wallet\Interfaces\Wallet expected by parameter $from of Bavix\Wallet\Services\Co...ervice::forceTransfer(). ( Ignorable by Annotation )

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

158
                    /** @scrutinizer ignore-type */ $product,
Loading history...
159 8
                    $transfer->withdraw->wallet,
160 8
                    $transfer->deposit->amount,
161 8
                    $product->getMetaProduct()
0 ignored issues
show
Bug introduced by
It seems like getMetaProduct() 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

161
                    $product->/** @scrutinizer ignore-call */ 
162
                              getMetaProduct()
Loading history...
162
                );
163
164 8
                $results[] = $transfer->update([
165 8
                    'status' => Transfer::STATUS_REFUND,
166 8
                    'status_last' => $transfer->status,
167
                ]);
168
            }
169
170 8
            return count(array_unique($results)) === 1;
171 9
        });
172
    }
173
174
    /**
175
     * @param Cart $cart
176
     * @param bool $gifts
177
     * @return bool
178
     * @throws
179
     */
180 1
    public function forceRefundCart(Cart $cart, bool $gifts = null): bool
181
    {
182 1
        return $this->refundCart($cart, true, $gifts);
183
    }
184
185
    /**
186
     * @param Cart $cart
187
     * @param bool $force
188
     * @return bool
189
     */
190 1
    public function safeRefundGiftCart(Cart $cart, bool $force = null): bool
191
    {
192
        try {
193 1
            return $this->refundGiftCart($cart, $force);
194 1
        } catch (Throwable $throwable) {
195 1
            return false;
196
        }
197
    }
198
199
    /**
200
     * @param Cart $cart
201
     * @param bool $force
202
     * @return bool
203
     * @throws
204
     */
205 1
    public function refundGiftCart(Cart $cart, bool $force = null): bool
206
    {
207 1
        return $this->refundCart($cart, $force, true);
208
    }
209
210
    /**
211
     * @param Cart $cart
212
     * @return bool
213
     * @throws
214
     */
215 1
    public function forceRefundGiftCart(Cart $cart): bool
216
    {
217 1
        return $this->refundGiftCart($cart, true);
218
    }
219
220
    /**
221
     * Checks acquired product your wallet.
222
     *
223
     * @param Product $product
224
     * @param bool $gifts
225
     * @return null|Transfer
226
     */
227 12
    public function paid(Product $product, bool $gifts = null): ?Transfer
228
    {
229 12
        return current(Cart::make()->addItem($product)->alreadyBuy($this, $gifts)) ?: null;
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::alreadyBuy(). ( Ignorable by Annotation )

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

229
        return current(Cart::make()->addItem($product)->alreadyBuy(/** @scrutinizer ignore-type */ $this, $gifts)) ?: null;
Loading history...
230
    }
231
232
}
233