-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCampaignFunding.php
More file actions
219 lines (193 loc) · 7.12 KB
/
CampaignFunding.php
File metadata and controls
219 lines (193 loc) · 7.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<?php
declare(strict_types=1);
namespace MatchBot\Domain;
use BcMath\Number;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* A specific allocation of money from a {@see Fund}, to one or more {@see Campaign}s.
*
* The Campaigns link is many-to-many, although the most common behaviour is to link to one Campaign.
* Linking to multiple creates funding that is not ringfenced and is assigned on a 'first come first
* served' basis to the Campaigns that receive donations first.
*
* Allocation order is currently set to a fixed value for each type of {@see Fund}, such that
* Pledges are used before Champion funds, which are used before topup funds.
* See {@see FundType::allocationOrder()}
*/
#[ORM\Index(name: 'available_fundings', columns: ['amountAvailable', 'id'])]
#[ORM\Entity(repositoryClass: CampaignFundingRepository::class)]
#[ORM\HasLifecycleCallbacks]
class CampaignFunding extends Model
{
use TimestampsTrait;
/**
* @var Collection<int, Campaign>
*/
#[ORM\JoinTable(name: 'Campaign_CampaignFunding')]
#[ORM\JoinColumn(name: 'campaignfunding_id', referencedColumnName: 'id')]
#[ORM\InverseJoinColumn(name: 'campaign_id', referencedColumnName: 'id')]
#[ORM\ManyToMany(targetEntity: Campaign::class, inversedBy: 'campaignFundings')]
protected Collection $campaigns;
#[ORM\ManyToOne(targetEntity: Fund::class, cascade: ['persist'], inversedBy: 'campaignFundings')]
#[ORM\JoinColumn(nullable: false)]
protected Fund $fund;
/**
* @var string ISO 4217 code for the currency in which all monetary values are denominated.
*/
#[ORM\Column(length: 3)]
protected string $currencyCode;
/**
* @var numeric-string Always use bcmath methods as in repository helpers to avoid doing float maths with decimals!
* @see CampaignFunding::$currencyCode
*/
#[ORM\Column(type: 'decimal', precision: 18, scale: 2)]
protected string $amount;
/**
* The amount of this funding allocation not already claimed. If you plan to allocate funds, always read this
* with a PESSIMISTIC_WRITE lock and modify it in the same transaction you create a FundingWithdrawal.
*
* This is a less-realtime, eventually consistent copy of the information that we store in Redis.
* Redis is the primary source of truth, for better performance and to avoid locking issues around fund allocation.
*
* @psalm-var numeric-string Use bcmath methods as in repository helpers to avoid doing float maths with decimals!
* @see CampaignFunding::$currencyCode
*
* Whenver writing to this also call self::logAdjustment to help us track what's going on.
*
*/
#[ORM\Column(type: 'decimal', precision: 18, scale: 2)]
protected string $amountAvailable;
/**
* Logs increments, decrements etc to this CampaignFunding - not currently used in business logic but
* intended to help us track what's wrong if the amountAvailable at any time does not match what it should
* based on adding up all fundingWithdrawals.
*
* Currently, we are not writing anything to this to avoid performance impacts, so we should
* either remove fully or bring back into use soon.
*
* @var list<array<string, string>>
*/
#[ORM\Column(type: 'json', nullable: true)]
protected array $adjustmentLog = [];
/**
* @var Collection<int, FundingWithdrawal>
* @psalm-suppress UnusedProperty Used for its mapping with ORM.
* We set cascade-remove for easier clearing of test data; Production should never remove a CampaignFunding via Doctrine.
*/
#[ORM\OneToMany(mappedBy: 'campaignFunding', targetEntity: FundingWithdrawal::class, cascade: ['remove'])]
private Collection $fundingWithdrawals;
/**
* @param numeric-string $amountAvailable
* @param numeric-string $amount
*/
public function __construct(
Fund $fund,
string $amount,
string $amountAvailable,
) {
$this->fund = $fund;
$this->currencyCode = $fund->getCurrencyCode();
$this->amount = $amount;
$this->amountAvailable = $amountAvailable;
$this->campaigns = new ArrayCollection();
$this->fundingWithdrawals = new ArrayCollection();
$this->createdNow();
$this->logAdjustmentNoOPForNow(
incrementAmount: $amountAvailable,
balance: $amountAvailable,
relatedDonationId: null,
at: new \DateTimeImmutable(),
comment: 'Constructing new record'
);
}
public function __toString(): string
{
return "CampaignFunding, ID #{$this->id}, created {$this->createdAt->format('c')} " .
"of fund SF ID {$this->fund->getSalesforceId()}";
}
/**
* Currently no-op, in past and potentially in future added to a record of how the fund was incremented and
* deceremtend. See doc on self:::$adjustmentLog
* @param numeric-string $incrementAmount - increase to amountAvailable. Will be more often than not be negative.
* @param numeric-string $balance - the amaount available at the time of saving this adjustment.
* @return void
*/
public function logAdjustmentNoOPForNow(string $incrementAmount, string $balance, ?int $relatedDonationId, \DateTimeImmutable $at, ?string $comment = null)
{
return;
// $this->adjustmentLog[] = [
// 'incr' => $incrementAmount,
// 'balance' => $balance,
// 'd-id' => $relatedDonationId,
// 'at' => $at->format(\DateTimeImmutable::ATOM),
// 'c' => $comment,
// ];
}
/**
* @return numeric-string
*/
public function getAmount(): string
{
return $this->amount;
}
/**
* @param numeric-string $amount
*/
public function setAmount(string $amount): void
{
$this->amount = $amount;
}
/**
* @return string
* @psalm-return numeric-string
*/
public function getAmountAvailable(): string
{
return $this->amountAvailable;
}
/**
* @psalm-param numeric-string $amountAvailable
* Also call x
*/
public function setAmountAvailable(string $amountAvailable): void
{
$this->amountAvailable = $amountAvailable;
}
/**
* Add a Campaign to those for which this CampaignFunding is available, *if* not already linked.
*
* @param Campaign $campaign
*/
public function addCampaign(Campaign $campaign): void
{
if ($this->campaigns->contains($campaign)) {
return;
}
$this->campaigns->add($campaign);
}
/**
* Currently only used in test
* @return list<Campaign>
*/
public function getCampaigns(): array
{
return array_values($this->campaigns->toArray());
}
public function getAllocationOrder(): int
{
return $this->getFund()->getAllocationOrder();
}
/**
* @return Fund
*/
public function getFund(): Fund
{
return $this->fund;
}
public function getCurrencyCode(): string
{
return $this->currencyCode;
}
}