1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Transmissor\Factory; |
4
|
|
|
|
5
|
|
|
use Transmissor\Models\Debt; |
6
|
|
|
use Transmissor\Models\Group; |
7
|
|
|
use Transmissor\Models\User; |
8
|
|
|
|
9
|
|
|
class TransmissorFactory |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
private $payer; |
13
|
|
|
private $amount; |
14
|
|
|
private $receiver; |
15
|
|
|
private $group; |
16
|
|
|
|
17
|
|
|
public function __construct(User $payer, $amount) |
18
|
|
|
{ |
19
|
|
|
$this->payer = $payer; |
20
|
|
|
$this->amount = $amount; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function to(User $receiver) |
24
|
|
|
{ |
25
|
|
|
$this->receiver = $receiver; |
26
|
|
|
|
27
|
|
|
return $this; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function in(Group $group) |
31
|
|
|
{ |
32
|
|
|
$this->group = $group; |
33
|
|
|
|
34
|
|
|
return $this; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function save() |
38
|
|
|
{ |
39
|
|
|
|
40
|
|
|
$this->payer->debts_to_pay() |
41
|
|
|
->where('to_id', $this->receiver->id) |
42
|
|
|
->where('group_id', $this->group->id) |
|
|
|
|
43
|
|
|
->get() |
44
|
|
|
->each( |
45
|
|
|
function (Debt $debt) { |
46
|
|
|
|
47
|
|
|
$debtAmount = $debt->amount; |
48
|
|
|
|
49
|
|
|
if ($this->amount < $debtAmount) { |
50
|
|
|
$debt->amount -= $this->amount; |
51
|
|
|
$debt->save(); |
52
|
|
|
|
53
|
|
|
} else { |
54
|
|
|
$debt->delete(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$this->amount -= $debtAmount; |
58
|
|
|
|
59
|
|
|
if ($this->amount <= 0) { |
60
|
|
|
return false; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
); |
64
|
|
|
|
65
|
|
|
if ($this->amount <= 0) { |
66
|
|
|
return; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$previousDebt = $this->payer->debts_to_receive() |
70
|
|
|
->where('from_id', $this->receiver->id) |
71
|
|
|
->where('group_id', $this->group->id) |
|
|
|
|
72
|
|
|
->first(); |
73
|
|
|
|
74
|
|
|
if (! $previousDebt) { |
75
|
|
|
$previousDebt = new Debt(); |
76
|
|
|
$previousDebt->from_id = $this->receiver->id; |
77
|
|
|
$previousDebt->to_id = $this->payer->id; |
78
|
|
|
$previousDebt->amount = 0; |
79
|
|
|
$previousDebt->group_id = $this->group->id; |
|
|
|
|
80
|
|
|
$previousDebt->currency = $this->group->currency; |
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$previousDebt->amount += $this->amount; |
84
|
|
|
$previousDebt->save(); |
85
|
|
|
} |
86
|
|
|
} |
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.