-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMetaCampaign.php
More file actions
348 lines (290 loc) · 10.2 KB
/
MetaCampaign.php
File metadata and controls
348 lines (290 loc) · 10.2 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
<?php
declare(strict_types=1);
namespace MatchBot\Domain;
use Doctrine\ORM\Mapping as ORM;
use Laminas\Diactoros\Uri;
use MatchBot\Application\Assertion;
use MatchBot\Client;
use Psr\Http\Message\UriInterface;
/**
* @psalm-import-type SFCampaignApiResponse from Client\Campaign
*
*/
#[ORM\Table]
#[ORM\Entity(
repositoryClass: null // we construct our own repository
)]
#[ORM\HasLifecycleCallbacks]
#[ORM\Index(name: 'slug', columns: ['slug'])]
#[ORM\Index(name: 'title', columns: ['title'])]
#[ORM\Index(name: 'status', columns: ['status'])]
#[ORM\Index(name: 'hidden', columns: ['hidden'])]
class MetaCampaign extends SalesforceReadProxy
{
use TimestampsTrait;
public const string STATUS_VIEW_CAMPAIGN = 'View campaign';
/** Metacampaigns from before this date may not have accurate data so should not be indexed */
public const string INDEX_FROM = '2019-12-01T00:00:00z';
/** Metacampaigns that don't open soon may also have incomplete data so again not yet useful to ask
* search engines to index.
*/
public const string INDEX_NEW_INTERVAL = 'P4W'; // i.e. four weeks.
#[ORM\Column(length: 64, unique: true, nullable: false)]
private string $slug;
#[ORM\Column()]
private string $title;
#[ORM\Column()]
private Currency $currency;
/**
* @var string|null Copy of Campaign_Status__c in Salesforce
*/
#[ORM\Column(nullable: true)]
private ?string $status;
/**
* @var string|null Copy of Master_Campaign_Status__c in Salesforce
*/
#[ORM\Column(nullable: true)]
private ?string $masterCampaignStatus;
#[ORM\Column()]
private bool $hidden;
#[ORM\Column(length: 1_000, nullable: true)]
private ?string $summary;
#[ORM\Column(nullable: true)]
private ?string $bannerURI;
#[ORM\Column(type: 'datetime_immutable')]
private \DateTimeImmutable $startDate;
#[ORM\Column(type: 'datetime_immutable')]
private \DateTimeImmutable $endDate;
/**
* CCampaign__c.Total_Adjustment__c from Salesforce - Adjustment amount (positive to add to the displayed grand
* total) for a Master Campaign
*
* Total of any "offline" payments which need to be included in the campaign e.g. Award Payments, Offline Payments,
* Gift Aid on Pledge Assumptions.
*
* @var Money
*/
#[ORM\Embedded(columnPrefix: 'total_adjustment_')]
private Money $totalAdjustment;
/**
* All associated charity campaigns must match this, accepting either regular or ad-hoc
* donations, not a mixture.
*
* If true also implies that the charity campaigns will share funds - see doc for
* {@see self::$isEmergencyIMF}
*
*/
#[ORM\Column()]
private bool $isRegularGiving;
/**
* Is this an emergency campaign with shared, un-ringfenced champion funds?
*
* This is needed alongside isRegularGiving for calculating match funds available for
* associated charity campaigns, as well as to render `usesSharedFunds` and
* `parentUsesSharedFunds` when we render metacampaigns and charity campaigns
* respectively to FE.
*
* {@see self::$isRegularGiving}
*
*/
#[ORM\Column()]
private bool $isEmergencyIMF;
/**
* Ientifies the metacampaign as part of a series or type of campaigns, often with one member per year, e.g.
* Christmas Challenge, Earth Raise etc. Will be used for setting colours, in future may also be used for homepage highlight cards and
* perhaps other features.
*/
#[ORM\Column(nullable: true)]
private ?CampaignFamily $campaignFamily;
/**
* @param Salesforce18Id<self> $salesforceId
*/
public function __construct(
MetaCampaignSlug $slug,
Salesforce18Id $salesforceId,
string $title,
Currency $currency,
string $status,
string $masterCampaignStatus,
bool $hidden,
?string $summary,
?UriInterface $bannerURI,
\DateTimeImmutable $startDate,
\DateTimeImmutable $endDate,
bool $isRegularGiving,
bool $isEmergencyIMF,
Money $totalAdjustment,
?CampaignFamily $campaignFamily,
) {
Assertion::same($totalAdjustment->currency, $currency);
$this->createdNow();
$this->slug = $slug->slug;
$this->setSalesforceId($salesforceId->value);
$this->title = $title;
$this->currency = $currency;
$this->status = $status;
$this->masterCampaignStatus = $masterCampaignStatus;
$this->hidden = $hidden;
$this->summary = $summary;
$this->bannerURI = $bannerURI?->__toString();
$this->startDate = $startDate;
$this->endDate = $endDate;
$this->isRegularGiving = $isRegularGiving;
$this->isEmergencyIMF = $isEmergencyIMF;
$this->totalAdjustment = $totalAdjustment;
$this->campaignFamily = $campaignFamily;
}
/**
* @param SFCampaignApiResponse $data
* Constructs an instance using the data from SF API (shared with Charity Campaign) and the given slug
*
* Slug is not sent in SF response but that's OK as we can assume we already know the slug when calling this.
*/
public static function fromSfCampaignData(MetaCampaignSlug $slug, array $data): self
{
// other than salesforceId which should never change just filling in all placeholder
// values here for a microsecond - will be replaced in the call to updateFromSfData.
// I'm sure there's a more elegant way to do this but at least this should look OK
// from outside the class.
$metaCampaign = new self(
slug: $slug,
salesforceId: Salesforce18Id::ofMetaCampaign($data['id']),
title: '',
currency: Currency::GBP,
status: '',
masterCampaignStatus: '',
hidden: false,
summary: '',
bannerURI: null,
startDate: new \DateTimeImmutable('1970-01-01'),
endDate: new \DateTimeImmutable('1970-01-01'),
isRegularGiving: false,
isEmergencyIMF: false,
totalAdjustment: Money::zero(),
campaignFamily: CampaignFamily::from($data['campaignFamily']),
);
$metaCampaign->updateFromSfData($data);
return $metaCampaign;
}
/**
* @param SFCampaignApiResponse $data
*/
public function updateFromSfData(array $data): void
{
Assertion::true($data['isMetaCampaign'] ?? true);
$bannerUri = $data['bannerUri'];
$isRegularGiving = $data['isRegularGiving'] ?? null;
Assertion::boolean($isRegularGiving);
$startDate = $data['startDate'];
$endDate = $data['endDate'];
$title = $data['title'];
// status may be null for now.
$status = $data['campaignStatus'] ?? null;
$masterCampaignStatus = $data['masterCampaignStatus'] ?? null;
Assertion::notNull($startDate);
Assertion::notNull($endDate);
Assertion::notNull($title);
$currency = Currency::fromIsoCode($data['currencyCode']);
$totalAdjustment = (string)($data['totalAdjustment'] ?? '0.00');
/** @psalm-suppress TypeDoesNotContainType */
if ($totalAdjustment === '') {
$totalAdjustment = '0.0';
}
Assertion::numeric($totalAdjustment);
$this->status = $status;
$this->masterCampaignStatus = $masterCampaignStatus;
$this->bannerURI = \is_string($bannerUri) ? (new Uri($bannerUri))->__toString() : null;
$this->isRegularGiving = $isRegularGiving;
$this->title = $title;
$this->currency = $currency;
$this->hidden = $data['hidden'];
$this->summary = $data['summary'];
$this->startDate = new \DateTimeImmutable($startDate);
$this->endDate = new \DateTimeImmutable($endDate);
$this->isEmergencyIMF = $data['isEmergencyIMF'];
$this->totalAdjustment = Money::fromNumericString($totalAdjustment, $currency);
}
/**
* Returns true if the campaigns within this meta-campaign should have access to a shared match funding pot, rather than
* individual pots.
*/
public function usesSharedFunds(): bool
{
return $this->isRegularGiving || $this->isEmergencyIMF;
}
public function getSlug(): MetaCampaignSlug
{
return MetaCampaignSlug::of($this->slug);
}
public function getTotalAdjustment(): Money
{
return $this->totalAdjustment;
}
public function getTitle(): string
{
return $this->title;
}
public function getCurrency(): Currency
{
return $this->currency;
}
public function getStatusAt(\DateTimeImmutable $date): ?string
{
if ($this->masterCampaignStatus !== self::STATUS_VIEW_CAMPAIGN) {
return $this->status;
}
if ($date < $this->startDate) {
return 'Preview';
}
if ($date > $this->endDate) {
return 'Expired';
}
return 'Active';
}
public function isHidden(): bool
{
return $this->hidden;
}
public function getSummary(): ?string
{
return $this->summary;
}
public function getBannerUri(): ?UriInterface
{
if ($this->bannerURI === null) {
return null;
}
return new Uri($this->bannerURI);
}
public function getStartDate(): \DateTimeImmutable
{
return $this->startDate;
}
public function getEndDate(): \DateTimeImmutable
{
return $this->endDate;
}
public function isEmergencyIMF(): bool
{
return $this->isEmergencyIMF;
}
public function getFamily(): ?CampaignFamily
{
return $this->campaignFamily;
}
public function isOpen(\DateTimeImmutable $at): bool
{
return $at >= $this->startDate && $at <= $this->endDate;
}
/**
* Whether to include in e.g. a sitemap for google search or publish *without* a noindex tag.
*/
public function shouldBeIndexed(\DateTimeImmutable $at): bool
{
if ($this->hidden) {
return false;
}
return $this->startDate > new \DateTimeImmutable(self::INDEX_FROM) &&
$this->startDate < $at->add(new \DateInterval(self::INDEX_NEW_INTERVAL));
}
}