Passed
Pull Request — master (#19)
by Tim
05:32
created

Campaign   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Test Coverage

Coverage 73.91%

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 55
ccs 17
cts 23
cp 0.7391
rs 10
c 0
b 0
f 0
wmc 9

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 1
A isInActiveDateRange() 0 3 2
A selectBucket() 0 7 3
A getIdentifier() 0 2 1
A getEnd() 0 2 1
A getDisplayPercentage() 0 2 1
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
	private $buckets;
22
23 5
	public function __construct(
24
		string $identifier,
25
		\DateTime $start,
26
		\DateTime $end,
27
		int $displayPercentage,
28
		RandomIntegerGenerator $rng,
29
		Bucket $firstBucket,
30
		Bucket ...$additionalBuckets ) {
31
32 5
		$this->identifier = $identifier;
33 5
		$this->start = $start;
34 5
		$this->end = $end;
35 5
		$this->displayPercentage = $displayPercentage;
36 5
		$this->rng = $rng;
37 5
		$this->buckets = $additionalBuckets;
38 5
		array_unshift( $this->buckets, $firstBucket );
39 5
	}
40
41
	public function getIdentifier(): string {
42
		return $this->identifier;
43
	}
44
45
	public function getEnd(): \DateTime {
46
		return $this->end;
47
	}
48
49 2
	public function isInActiveDateRange( \DateTime $time ): bool {
50 2
		return $time->getTimestamp() >= $this->start->getTimestamp() &&
51 2
			$time->getTimestamp() <= $this->end->getTimestamp();
52
	}
53
54 3
	public function selectBucket( ?string $bucketId ): Bucket {
55 3
		foreach ( $this->buckets as $bucket ) {
56 3
			if ( $bucket->getIdentifier() === $bucketId ) {
57 3
				return $bucket;
58
			}
59
		}
60 2
		return $this->buckets[$this->rng->getRandomInteger( 0, count( $this->buckets ) - 1 )];
61
	}
62
63
	public function getDisplayPercentage(): int {
64
		return $this->displayPercentage;
65
	}
66
}