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

CartPay::refundCart()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 36
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 22
nc 1
nop 3
dl 0
loc 36
ccs 22
cts 22
cp 1
crap 4
rs 9.568
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
19
    use HasWallet;
20
21
    /**
22
     * @param Cart $cart
23
     * @return Transfer[]
24
     * @throws
25
     */
26 2
    public function payFreeCart(Cart $cart): array
27
    {
28 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

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

29
            throw new ProductEnded(/** @scrutinizer ignore-type */ trans('wallet::errors.product_stock'));
Loading history...
30
        }
31
32 2
        app(CommonService::class)
33 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

33
            ->verifyWithdraw(/** @scrutinizer ignore-type */ $this, 0);
Loading history...
34
35
        return DB::transaction(function () use ($cart) {
36 2
            $results = [];
37 2
            foreach ($cart->getItems() as $product) {
38 2
                $results[] = app(CommonService::class)->forceTransfer(
39 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

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

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

74
            throw new ProductEnded(/** @scrutinizer ignore-type */ trans('wallet::errors.product_stock'));
Loading history...
75
        }
76
77
        return DB::transaction(function () use ($cart, $force) {
78
79 10
            $results = [];
80 10
            foreach ($cart->getItems() as $product) {
81 10
                if ($force) {
82 1
                    $results[] = app(CommonService::class)->forceTransfer(
83 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

83
                        /** @scrutinizer ignore-type */ $this,
Loading history...
84 1
                        $product,
85 1
                        $product->getAmountProduct(),
86 1
                        $product->getMetaProduct(),
87 1
                        Transfer::STATUS_PAID
88
                    );
89
90 1
                    continue;
91
                }
92
93 9
                $results[] = app(CommonService::class)->transfer(
94 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

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

144
            $transfers = $cart->alreadyBuy(/** @scrutinizer ignore-type */ $this, $gifts);
Loading history...
145 9
            if (count($transfers) !== count($cart)) {
146 3
                throw (new ModelNotFoundException())
147 3
                    ->setModel($this->transfers()->getMorphClass());
148
            }
149
150 8
            foreach ($cart->getItems() as $key => $product) {
151 8
                $transfer = $transfers[$key];
152 8
                $transfer->load('withdraw.wallet');
153
154 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...
155 8
                    app(CommonService::class)->verifyWithdraw(
156 8
                        $product,
157 8
                        $transfer->deposit->amount
158
                    );
159
                }
160
161 8
                app(CommonService::class)->forceTransfer(
162 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

162
                    /** @scrutinizer ignore-type */ $product,
Loading history...
163 8
                    $transfer->withdraw->wallet,
164 8
                    $transfer->deposit->amount,
165 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

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

234
        return current(Cart::make()->addItem($product)->alreadyBuy(/** @scrutinizer ignore-type */ $this, $gifts)) ?: null;
Loading history...
235
    }
236
237
}
238