Test Failed
Push — master ( f57de9...39f3ea )
by Бабичев
04:11
created

Transaction::getAmountFloatAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 6
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Bavix\Wallet\Models;
4
5
use Bavix\Wallet\Interfaces\Wallet;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Bavix\Wallet\Models\Wallet. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
6
use Bavix\Wallet\Models\Wallet as WalletModel;
7
use Bavix\Wallet\Services\WalletService;
8
use Illuminate\Database\Eloquent\Model;
9
use Illuminate\Database\Eloquent\Relations\BelongsTo;
10
use Illuminate\Database\Eloquent\Relations\MorphTo;
11
use function config;
12
13
/**
14
 * Class Transaction
15
 * @package Bavix\Wallet\Models
16
 *
17
 * @property string $payable_type
18
 * @property int $payable_id
19
 * @property string $uuid
20
 * @property string $type
21
 * @property int $amount
22
 * @property float $amountFloat
23
 * @property bool $confirmed
24
 * @property array $meta
25
 * @property Wallet $payable
26
 * @property WalletModel $wallet
27
 */
28
class Transaction extends Model
29
{
30
31
    public const TYPE_DEPOSIT = 'deposit';
32
    public const TYPE_WITHDRAW = 'withdraw';
33
34
    /**
35
     * @var array
36
     */
37
    protected $fillable = [
38
        'payable_type',
39
        'payable_id',
40
        'wallet_id',
41
        'uuid',
42
        'type',
43
        'amount',
44
        'confirmed',
45
        'meta',
46
    ];
47
48
    /**
49
     * @var array
50
     */
51
    protected $casts = [
52
        'wallet_id' => 'int',
53
        'amount' => 'int',
54
        'confirmed' => 'bool',
55
        'meta' => 'json'
56
    ];
57
58
    /**
59 92
     * @inheritDoc
60
     */
61 92
    public function getCasts(): array
62 92
    {
63 92
        $this->casts = array_merge(
64
            $this->casts,
65
            config('wallet.transaction.casts', [])
66 92
        );
67
68
        return parent::getCasts();
69
    }
70
71
    /**
72 132
     * @return string
73
     */
74 132
    public function getTable(): string
75 132
    {
76
        if (!$this->table) {
77
            $this->table = config('wallet.transaction.table', 'transactions');
78 132
        }
79
80
        return parent::getTable();
81
    }
82
83
    /**
84 7
     * @return MorphTo
85
     */
86 7
    public function payable(): MorphTo
87
    {
88
        return $this->morphTo();
89
    }
90
91
    /**
92 27
     * @return BelongsTo
93
     */
94 27
    public function wallet(): BelongsTo
95
    {
96
        return $this->belongsTo(config('wallet.wallet.model', WalletModel::class));
97
    }
98
99
    /**
100
     * @return float
101
     */
102
    public function getAmountFloatAttribute(): float
103
    {
104
        $decimalPlaces = app(WalletService::class)
105
            ->decimalPlaces($this->wallet);
106
107
        return $this->amount / $decimalPlaces;
108
    }
109
110
    /**
111
     * @param float $amount
112
     * @return float
113
     */
114
    public function setAmountFloatAttribute(float $amount): void
115
    {
116
        $decimalPlaces = app(WalletService::class)
117
            ->decimalPlaces($this->wallet);
118
119
        $this->amount = $amount * $decimalPlaces;
120
    }
121
122
}
123