|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare( strict_types = 1 ); |
|
4
|
|
|
|
|
5
|
|
|
namespace WMDE\Fundraising\Frontend\BucketTesting\Domain\Model; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Value object for defining campaigns |
|
9
|
|
|
* |
|
10
|
|
|
* @license GPL-2.0-or-later |
|
11
|
|
|
*/ |
|
12
|
|
|
class Campaign { |
|
13
|
|
|
|
|
14
|
|
|
private $name; |
|
15
|
|
|
private $active; |
|
16
|
|
|
private $startTimestamp; |
|
17
|
|
|
private $endTimestamp; |
|
18
|
|
|
private $buckets; |
|
19
|
|
|
private $urlKey; |
|
20
|
|
|
private $defaultBucketIndex; |
|
21
|
|
|
private $onlyActiveWithUrlKey; |
|
22
|
|
|
|
|
23
|
|
|
public const ACTIVE = true; |
|
24
|
|
|
public const INACTIVE = false; |
|
25
|
|
|
|
|
26
|
|
|
public const NEEDS_URL_KEY = true; |
|
27
|
|
|
public const NEEDS_NO_URL_KEY = false; |
|
28
|
|
|
|
|
29
|
|
|
public function __construct( string $name, string $urlKey, CampaignDate $startTimestamp, CampaignDate $endTimestamp, |
|
30
|
|
|
bool $isActive, bool $onlyActiveWithUrlKey = false ) { |
|
31
|
|
|
$this->name = $name; |
|
32
|
|
|
$this->urlKey = $urlKey; |
|
33
|
|
|
$this->active = $isActive; |
|
34
|
|
|
$this->startTimestamp = $startTimestamp; |
|
35
|
|
|
$this->endTimestamp = $endTimestamp; |
|
36
|
|
|
$this->buckets = []; |
|
37
|
|
|
$this->defaultBucketIndex = -1; |
|
38
|
|
|
$this->onlyActiveWithUrlKey = $onlyActiveWithUrlKey; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function isActive(): bool { |
|
42
|
|
|
return $this->active; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function getStartTimestamp(): CampaignDate { |
|
46
|
|
|
return $this->startTimestamp; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function getEndTimestamp(): CampaignDate { |
|
50
|
|
|
return $this->endTimestamp; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function getName(): string { |
|
54
|
|
|
return $this->name; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @return Bucket[] |
|
59
|
|
|
*/ |
|
60
|
|
|
public function getBuckets(): array { |
|
61
|
|
|
return $this->buckets; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function getUrlKey(): string { |
|
65
|
|
|
return $this->urlKey; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function getBucketByIndex( int $index ): ?Bucket { |
|
69
|
|
|
return $this->getBuckets()[$index] ?? null; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function getIndexByBucket( Bucket $bucket ): int { |
|
73
|
|
|
$index = array_search( $bucket, $this->getBuckets(), true ); |
|
74
|
|
|
if ( $index === false ) { |
|
75
|
|
|
throw new \OutOfBoundsException(); |
|
76
|
|
|
} |
|
77
|
|
|
return $index; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function addBucket( Bucket $bucket ): self { |
|
81
|
|
|
$this->buckets[] = $bucket; |
|
82
|
|
|
if ( $bucket->isDefaultBucket() ) { |
|
83
|
|
|
$this->defaultBucketIndex = count( $this->buckets ) - 1; |
|
84
|
|
|
} |
|
85
|
|
|
return $this; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function isExpired( CampaignDate $now ): bool { |
|
89
|
|
|
return $this->startTimestamp > $now || $this->endTimestamp < $now; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function getDefaultBucket(): Bucket { |
|
93
|
|
|
$bucket = $this->getBucketByIndex( $this->defaultBucketIndex ); |
|
94
|
|
|
if ( $bucket === null ) { |
|
95
|
|
|
throw new \LogicException( 'No default bucket was added to this campaign' ); |
|
96
|
|
|
} |
|
97
|
|
|
return $bucket; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public function isOnlyActiveWithUrlKey(): bool { |
|
101
|
|
|
return $this->onlyActiveWithUrlKey; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
} |
|
105
|
|
|
|