Completed
Pull Request — master (#107)
by Бабичев
177:43 queued 73:46
created

Bring::getFee()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 10
1
<?php
2
3
namespace Bavix\Wallet\Objects;
4
5
use Bavix\Wallet\Interfaces\Wallet;
6
use Bavix\Wallet\Models\Transaction;
7
use Bavix\Wallet\Models\Transfer;
8
use Ramsey\Uuid\Uuid;
9
use function abs;
10
11
class Bring
12
{
13
14
    /**
15
     * @var string
16
     */
17
    protected $status;
18
19
    /**
20
     * @var Wallet
21
     */
22
    protected $from;
23
24
    /**
25
     * @var Wallet
26
     */
27
    protected $to;
28
29
    /**
30
     * @var Transaction
31
     */
32
    protected $deposit;
33
34
    /**
35
     * @var Transaction
36
     */
37
    protected $withdraw;
38
39
    /**
40
     * @var string
41
     */
42
    protected $uuid;
43
44
    /**
45
     * @var int
46
     */
47
    protected $fee;
48
49
    /**
50
     * Bring constructor.
51
     * @throws
52
     */
53 28
    public function __construct()
54
    {
55 28
        $this->uuid = Uuid::uuid4()->toString();
56 28
    }
57
58
    /**
59
     * @return string
60
     */
61 28
    public function getStatus(): string
62
    {
63 28
        return $this->status;
64
    }
65
66
    /**
67
     * @param string $status
68
     * @return static
69
     */
70 28
    public function setStatus(string $status): self
71
    {
72 28
        $this->status = $status;
73 28
        return $this;
74
    }
75
76
    /**
77
     * @return Wallet
78
     */
79 28
    public function getFrom(): Wallet
80
    {
81 28
        return $this->from;
82
    }
83
84
    /**
85
     * @param Wallet $from
86
     * @return static
87
     */
88 28
    public function setFrom(Wallet $from): self
89
    {
90 28
        $this->from = $from;
91 28
        return $this;
92
    }
93
94
    /**
95
     * @return Wallet
96
     */
97 28
    public function getTo(): Wallet
98
    {
99 28
        return $this->to;
100
    }
101
102
    /**
103
     * @param Wallet $to
104
     * @return static
105
     */
106 28
    public function setTo(Wallet $to): self
107
    {
108 28
        $this->to = $to;
109 28
        return $this;
110
    }
111
112
    /**
113
     * @return Transaction
114
     */
115 28
    public function getDeposit(): Transaction
116
    {
117 28
        return $this->deposit;
118
    }
119
120
    /**
121
     * @param Transaction $deposit
122
     * @return static
123
     */
124 28
    public function setDeposit(Transaction $deposit): self
125
    {
126 28
        $this->deposit = $deposit;
127 28
        return $this;
128
    }
129
130
    /**
131
     * @return Transaction
132
     */
133 28
    public function getWithdraw(): Transaction
134
    {
135 28
        return $this->withdraw;
136
    }
137
138
    /**
139
     * @param Transaction $withdraw
140
     * @return static
141
     */
142 28
    public function setWithdraw(Transaction $withdraw): self
143
    {
144 28
        $this->withdraw = $withdraw;
145 28
        return $this;
146
    }
147
148
    /**
149
     * @return string
150
     */
151 28
    public function getUuid(): string
152
    {
153 28
        return $this->uuid;
154
    }
155
156
    /**
157
     * @return int
158
     */
159 28
    public function getFee(): int
160
    {
161 28
        if ($this->fee === null) {
162 27
            return abs($this->getWithdraw()->amount) - abs($this->getDeposit()->amount);
163
        }
164
165 1
        return $this->fee;
166
    }
167
168
    /**
169
     * @param int $fee
170
     * @return Bring
171
     */
172 1
    public function setFee(int $fee): self
173
    {
174 1
        $this->fee = $fee;
175 1
        return $this;
176
    }
177
178
    /**
179
     * @return Transfer
180
     * @throws
181
     */
182 28
    public function create(): Transfer
183
    {
184 28
        return app(Transfer::class)
185 28
            ->create($this->toArray());
186
    }
187
188
    /**
189
     * @return array
190
     * @throws
191
     */
192 28
    public function toArray(): array
193
    {
194
        return [
195 28
            'status' => $this->getStatus(),
196 28
            'deposit_id' => $this->getDeposit()->getKey(),
197 28
            'withdraw_id' => $this->getWithdraw()->getKey(),
198 28
            'from_type' => $this->getFrom()->getMorphClass(),
0 ignored issues
show
Bug introduced by
The method getMorphClass() does not exist on Bavix\Wallet\Interfaces\Wallet. It seems like you code against a sub-type of said class. However, the method does not exist in Bavix\Wallet\Interfaces\Customer or Bavix\Wallet\Interfaces\Product. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

198
            'from_type' => $this->getFrom()->/** @scrutinizer ignore-call */ getMorphClass(),
Loading history...
199 28
            'from_id' => $this->getFrom()->getKey(),
0 ignored issues
show
Bug introduced by
The method getKey() does not exist on Bavix\Wallet\Interfaces\Wallet. It seems like you code against a sub-type of said class. However, the method does not exist in Bavix\Wallet\Interfaces\Customer or Bavix\Wallet\Interfaces\Product. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

199
            'from_id' => $this->getFrom()->/** @scrutinizer ignore-call */ getKey(),
Loading history...
200 28
            'to_type' => $this->getTo()->getMorphClass(),
201 28
            'to_id' => $this->getTo()->getKey(),
202 28
            'fee' => $this->getFee(),
203 28
            'uuid' => $this->getUuid(),
204
        ];
205
    }
206
207
}
208