Test Setup Failed
Pull Request — master (#19)
by Tim
60:53
created

Campaign::getDisplayPercentage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace WMDE\BannerServer\Entity\BannerSelection;
6
7
/**
8
 * @license GNU GPL v2+
9
 */
10
class Campaign {
11
12
	private $identifier;
13
	private $start;
14
	private $end;
15
	private $rng;
16
	private $displayPercentage;
17
18
	/**
19
	 * @var Bucket[]
20
	 */
21 7
	private $buckets;
22 7
23 7
	public function __construct(
24 7
		string $identifier,
25 7
		\DateTime $start,
26 7
		\DateTime $end,
27
		int $displayPercentage,
28 1
		RandomIntegerGenerator $rng,
29 1
		Bucket $firstBucket,
30
		Bucket ...$additionalBuckets ) {
31
32
		$this->identifier = $identifier;
33
		$this->start = $start;
34
		$this->end = $end;
35
		$this->displayPercentage = $displayPercentage;
36 4
		$this->rng = $rng;
37 4
		$this->buckets = $additionalBuckets;
38 4
		array_unshift( $this->buckets, $firstBucket );
39
	}
40
41 3
	public function getIdentifier(): string {
42 3
		return $this->identifier;
43 3
	}
44 3
45
	public function getEnd(): \DateTime {
46
		return $this->end;
47 2
	}
48
49
	public function isInActiveDateRange( \DateTime $time ): bool {
50
		return $time->getTimestamp() >= $this->start->getTimestamp() &&
51
			$time->getTimestamp() <= $this->end->getTimestamp();
52
	}
53
54
	public function selectBucket( ?string $bucketId ): Bucket {
55
		foreach ( $this->buckets as $bucket ) {
56
			if ( $bucket->getIdentifier() === $bucketId ) {
57
				return $bucket;
58
			}
59
		}
60
		return $this->buckets[$this->rng->getRandomInteger( 0, count( $this->buckets ) - 1 )];
61
	}
62
63
	public function getDisplayPercentage(): int {
64
		return $this->displayPercentage;
65
	}
66
}