Completed
Pull Request — master (#19)
by Tim
60:24
created

Campaign   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Test Coverage

Coverage 88.89%

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 55
ccs 16
cts 18
cp 0.8889
rs 10
c 0
b 0
f 0
wmc 9

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 1
A isRatioLimited() 0 2 1
A isInActiveDateRange() 0 3 2
A selectBucket() 0 7 3
A getIdentifier() 0 2 1
A getEnd() 0 2 1
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace WMDE\BannerServer\Entity\BannerSelection;
6
7
use WMDE\BannerServer\Utils\RandomIntegerInterface;
8
9
/**
10
 * @license GNU GPL v2+
11
 */
12
class Campaign {
13
14
	private $identifier;
15
	private $start;
16
	private $end;
17
	private $rng;
18
	private $displayRatio;
19
20
	/**
21 7
	 * @var Bucket[]
22 7
	 */
23 7
	private $buckets;
24 7
25 7
	public function __construct(
26 7
		string $identifier,
27
		\DateTime $start,
28 1
		\DateTime $end,
29 1
		float $displayRatio,
30
		RandomIntegerInterface $rng,
31
		Bucket $mainBucket,
32
		Bucket ...$additionalBuckets ) {
33
34
		$this->identifier = $identifier;
35
		$this->start = $start;
36 4
		$this->end = $end;
37 4
		$this->displayRatio = $displayRatio;
38 4
		$this->rng = $rng;
39
		$this->buckets = $additionalBuckets;
40
		array_unshift( $this->buckets, $mainBucket );
41 3
	}
42 3
43 3
	public function getIdentifier(): string {
44 3
		return $this->identifier;
45
	}
46
47 2
	public function getEnd(): \DateTime {
48
		return $this->end;
49
	}
50
51
	public function isInActiveDateRange( \DateTime $time ): bool {
52
		return $time->getTimestamp() >= $this->start->getTimestamp() &&
53
			$time->getTimestamp() <= $this->end->getTimestamp();
54
	}
55
56
	public function selectBucket( ?string $bucketId ): Bucket {
57
		foreach ( $this->buckets as $bucket ) {
58
			if ( $bucket->getIdentifier() === $bucketId ) {
59
				return $bucket;
60
			}
61
		}
62
		return $this->buckets[$this->rng->getRandomInteger( 0, count( $this->buckets ) - 1 )];
63
	}
64
65
	public function isRatioLimited(): bool {
66
		return ( $this->rng->getRandomInteger( 1, 100 ) / 100 ) > $this->displayRatio;
67
	}
68
}