Passed
Push — master ( 158a9b...1877fd )
by Бабичев
17:27 queued 12s
created

Bring::getDiscount()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
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
     * @var int
51
     */
52
    protected $discount;
53
54
    /**
55
     * Bring constructor.
56
     * @throws
57
     */
58 50
    public function __construct()
59
    {
60 50
        $this->uuid = Uuid::uuid4()->toString();
61 50
    }
62
63
    /**
64
     * @return string
65
     */
66 49
    public function getStatus(): string
67
    {
68 49
        return $this->status;
69
    }
70
71
    /**
72
     * @param string $status
73
     * @return static
74
     */
75 49
    public function setStatus(string $status): self
76
    {
77 49
        $this->status = $status;
78 49
        return $this;
79
    }
80
81
    /**
82
     * @param int $discount
83
     * @return static
84
     */
85 49
    public function setDiscount(int $discount): self
86
    {
87 49
        $this->discount = $discount;
88 49
        return $this;
89
    }
90
91
    /**
92
     * @return Wallet
93
     */
94 49
    public function getFrom(): Wallet
95
    {
96 49
        return $this->from;
97
    }
98
99
    /**
100
     * @param Wallet $from
101
     * @return static
102
     */
103 49
    public function setFrom(Wallet $from): self
104
    {
105 49
        $this->from = $from;
106 49
        return $this;
107
    }
108
109
    /**
110
     * @return Wallet
111
     */
112 49
    public function getTo(): Wallet
113
    {
114 49
        return $this->to;
115
    }
116
117
    /**
118
     * @param Wallet $to
119
     * @return static
120
     */
121 49
    public function setTo(Wallet $to): self
122
    {
123 49
        $this->to = $to;
124 49
        return $this;
125
    }
126
127
    /**
128
     * @return Transaction
129
     */
130 49
    public function getDeposit(): Transaction
131
    {
132 49
        return $this->deposit;
133
    }
134
135
    /**
136
     * @param Transaction $deposit
137
     * @return static
138
     */
139 49
    public function setDeposit(Transaction $deposit): self
140
    {
141 49
        $this->deposit = $deposit;
142 49
        return $this;
143
    }
144
145
    /**
146
     * @return Transaction
147
     */
148 49
    public function getWithdraw(): Transaction
149
    {
150 49
        return $this->withdraw;
151
    }
152
153
    /**
154
     * @param Transaction $withdraw
155
     * @return static
156
     */
157 49
    public function setWithdraw(Transaction $withdraw): self
158
    {
159 49
        $this->withdraw = $withdraw;
160 49
        return $this;
161
    }
162
163
    /**
164
     * @return string
165
     */
166 49
    public function getUuid(): string
167
    {
168 49
        return $this->uuid;
169
    }
170
171
    /**
172
     * @return string
173
     */
174 49
    public function getDiscount(): int
175
    {
176 49
        return $this->discount;
177
    }
178
179
    /**
180
     * @return int
181
     */
182 49
    public function getFee(): int
183
    {
184 49
        if ($this->fee === null) {
185 48
            return abs($this->getWithdraw()->amount) - abs($this->getDeposit()->amount);
186
        }
187
188 1
        return $this->fee;
189
    }
190
191
    /**
192
     * @param int $fee
193
     * @return Bring
194
     */
195 1
    public function setFee(int $fee): self
196
    {
197 1
        $this->fee = $fee;
198 1
        return $this;
199
    }
200
201
    /**
202
     * @return Transfer
203
     * @throws
204
     */
205 49
    public function create(): Transfer
206
    {
207 49
        return app(Transfer::class)
208 49
            ->create($this->toArray());
209
    }
210
211
    /**
212
     * @return array
213
     * @throws
214
     */
215 49
    public function toArray(): array
216
    {
217
        return [
218 49
            'status' => $this->getStatus(),
219 49
            'deposit_id' => $this->getDeposit()->getKey(),
220 49
            'withdraw_id' => $this->getWithdraw()->getKey(),
221 49
            '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 or Bavix\Wallet\Interfaces\Discount. 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

221
            'from_type' => $this->getFrom()->/** @scrutinizer ignore-call */ getMorphClass(),
Loading history...
222 49
            '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 or Bavix\Wallet\Interfaces\Discount. 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

222
            'from_id' => $this->getFrom()->/** @scrutinizer ignore-call */ getKey(),
Loading history...
223 49
            'to_type' => $this->getTo()->getMorphClass(),
224 49
            'to_id' => $this->getTo()->getKey(),
225 49
            'discount' => $this->getDiscount(),
226 49
            'fee' => $this->getFee(),
227 49
            'uuid' => $this->getUuid(),
228
        ];
229
    }
230
231
}
232