Completed
Push — master ( 831ccc...fee157 )
by Бабичев
03:22
created

HasGift::gift()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 2
dl 0
loc 13
ccs 0
cts 7
cp 0
crap 2
rs 10
c 0
b 0
f 0
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\Tax;
9
use Illuminate\Support\Facades\DB;
10
11
/**
12
 * Trait HasGift
13
 * @package Bavix\Wallet\Traits
14
 *
15
 * This trait should be used with the trait HasWallet.
16
 */
17
trait HasGift
18
{
19
20
    /**
21
     * From this moment on, each user (wallet) can give
22
     * the goods to another user (wallet).
23
     * This functionality can be organized for gifts.
24
     *
25
     * @param Wallet $to
26
     * @param Product $product
27
     * @return Transfer
28
     */
29
    public function gift(Wallet $to, Product $product): Transfer
30
    {
31
        /**
32
         * this comment is needed for syntax highlighting )
33
         * @var \Bavix\Wallet\Models\Wallet $to
34
         */
35
        return DB::transaction(function () use ($to, $product) {
36
            $amount = $product->getAmountProduct();
37
            $meta = $product->getMetaProduct();
38
            $fee = Tax::fee($product, $amount);
39
            $withdraw = $this->withdraw($amount + $fee, $meta);
0 ignored issues
show
Bug introduced by
It seems like withdraw() 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

39
            /** @scrutinizer ignore-call */ 
40
            $withdraw = $this->withdraw($amount + $fee, $meta);
Loading history...
40
            $deposit = $product->deposit($amount, $meta);
41
            return $to->assemble($product, $withdraw, $deposit);
42
        });
43
    }
44
45
    /**
46
     * Give the goods safely.
47
     *
48
     * @param Wallet $to
49
     * @param Product $product
50
     * @return Transfer|null
51
     */
52
    public function safeGift(Wallet $to, Product $product): ?Transfer
53
    {
54
        try {
55
            return $this->gift($to, $product);
56
        } catch (\Throwable $throwable) {
57
            return null;
58
        }
59
    }
60
61
}
62