Test Failed
Pull Request — master (#83)
by Бабичев
04:54
created

HasGift   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 6
Bugs 1 Features 1
Metric Value
eloc 26
c 6
b 1
f 1
dl 0
loc 88
ccs 0
cts 28
cp 0
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A safeGift() 0 6 2
A gift() 0 45 2
A forceGift() 0 3 1
1
<?php
2
3
namespace Bavix\Wallet\Traits;
4
5
use Bavix\Wallet\Interfaces\Product;
6
use Bavix\Wallet\Interfaces\Wallet;
7
use Bavix\Wallet\Models\Transfer;
8
use Bavix\Wallet\Objects\Bring;
9
use Bavix\Wallet\Services\CommonService;
10
use Bavix\Wallet\Services\WalletService;
11
use Illuminate\Support\Facades\DB;
12
use Throwable;
13
use function app;
14
15
/**
16
 * Trait HasGift
17
 * @package Bavix\Wallet\Traits
18
 *
19
 * This trait should be used with the trait HasWallet.
20
 */
21
trait HasGift
22
{
23
24
    /**
25
     * Give the goods safely.
26
     *
27
     * @param Wallet $to
28
     * @param Product $product
29
     * @param bool $force
30
     * @return Transfer|null
31
     */
32
    public function safeGift(Wallet $to, Product $product, bool $force = null): ?Transfer
33
    {
34
        try {
35
            return $this->gift($to, $product, $force);
36
        } catch (Throwable $throwable) {
37
            return null;
38
        }
39
    }
40
41
    /**
42
     * From this moment on, each user (wallet) can give
43
     * the goods to another user (wallet).
44
     * This functionality can be organized for gifts.
45
     *
46
     * @param Wallet $to
47
     * @param Product $product
48
     * @param bool $force
49
     * @return Transfer
50
     */
51
    public function gift(Wallet $to, Product $product, bool $force = null): Transfer
52
    {
53
        /**
54
         * Who's giving? Let's call him Santa Claus
55
         * @var Wallet $santa
56
         */
57
        $santa = $this;
58
59
        /**
60
         * Unfortunately,
61
         * I think it is wrong to make the "assemble" method public.
62
         * That's why I address him like this!
63
         */
64
        return DB::transaction(static function() use ($santa, $to, $product, $force) {
65
            $amount = $product->getAmountProduct();
66
            $meta = $product->getMetaProduct();
67
            $fee = app(WalletService::class)
68
                ->fee($product, $amount);
69
70
            $commonService = app(CommonService::class);
71
72
            /**
73
             * Santa pays taxes
74
             */
75
            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...
76
                $commonService->verifyWithdraw($santa, $amount);
77
            }
78
79
            $withdraw = $commonService->forceWithdraw($santa, $amount + $fee, $meta);
0 ignored issues
show
Bug introduced by
It seems like $santa can also be of type Bavix\Wallet\Traits\HasWallet; however, parameter $wallet of Bavix\Wallet\Services\Co...ervice::forceWithdraw() does only seem to accept Bavix\Wallet\Interfaces\Wallet, 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

79
            $withdraw = $commonService->forceWithdraw(/** @scrutinizer ignore-type */ $santa, $amount + $fee, $meta);
Loading history...
80
81
            $deposit = $commonService->deposit($product, $amount, $meta);
82
83
            $from = app(WalletService::class)
84
                ->getWallet($to);
85
86
            $transfers = $commonService->assemble([
87
                (new Bring())
88
                    ->setStatus(Transfer::STATUS_GIFT)
89
                    ->setDeposit($deposit)
90
                    ->setWithdraw($withdraw)
91
                    ->setFrom($from)
92
                    ->setTo($product)
93
            ]);
94
95
            return current($transfers);
96
        });
97
    }
98
99
    /**
100
     * to give force)
101
     *
102
     * @param Wallet $to
103
     * @param Product $product
104
     * @return Transfer
105
     */
106
    public function forceGift(Wallet $to, Product $product): Transfer
107
    {
108
        return $this->gift($to, $product, true);
109
    }
110
111
}
112