Test Setup Failed
Pull Request — master (#37)
by Gabriel
62:16
created

Campaign   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 25
c 4
b 0
f 0
dl 0
loc 61
ccs 22
cts 22
cp 1
rs 10
wmc 10

7 Methods

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