Passed
Push — master ( c67fb3...32a3ca )
by Бабичев
08:31 queued 11s
created

Transaction::getCasts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 1
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 Illuminate\Database\Eloquent\Model;
8
use Illuminate\Database\Eloquent\Relations\BelongsTo;
9
use Illuminate\Database\Eloquent\Relations\MorphTo;
10
use function config;
11
12
/**
13
 * Class Transaction
14
 * @package Bavix\Wallet\Models
15
 *
16
 * @property string $payable_type
17
 * @property int $payable_id
18
 * @property string $uuid
19
 * @property string $type
20
 * @property int $amount
21
 * @property bool $confirmed
22
 * @property array $meta
23
 * @property Wallet $payable
24
 * @property WalletModel $wallet
25
 */
26
class Transaction extends Model
27
{
28
29
    public const TYPE_DEPOSIT = 'deposit';
30
    public const TYPE_WITHDRAW = 'withdraw';
31
32
    /**
33
     * @var array
34
     */
35
    protected $fillable = [
36
        'payable_type',
37
        'payable_id',
38
        'wallet_id',
39
        'uuid',
40
        'type',
41
        'amount',
42
        'confirmed',
43
        'meta',
44
    ];
45
46
    /**
47
     * @var array
48
     */
49
    protected $casts = [
50
        'wallet_id' => 'int',
51
        'amount' => 'int',
52
        'confirmed' => 'bool',
53
        'meta' => 'json'
54
    ];
55
56
    /**
57
     * @inheritDoc
58
     */
59 92
    public function getCasts(): array
60
    {
61 92
        $this->casts = array_merge(
62 92
            $this->casts,
63 92
            config('wallet.transaction.casts', [])
64
        );
65
66 92
        return parent::getCasts();
67
    }
68
69
    /**
70
     * @return string
71
     */
72 132
    public function getTable(): string
73
    {
74 132
        if (!$this->table) {
75 132
            $this->table = config('wallet.transaction.table', 'transactions');
76
        }
77
78 132
        return parent::getTable();
79
    }
80
81
    /**
82
     * @return MorphTo
83
     */
84 7
    public function payable(): MorphTo
85
    {
86 7
        return $this->morphTo();
87
    }
88
89
    /**
90
     * @return BelongsTo
91
     */
92 27
    public function wallet(): BelongsTo
93
    {
94 27
        return $this->belongsTo(config('wallet.wallet.model', WalletModel::class));
95
    }
96
97
}
98