Passed
Pull Request — master (#19)
by Tim
03:11
created

Campaign   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Test Coverage

Coverage 90.91%

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 46
ccs 20
cts 22
cp 0.9091
rs 10
c 0
b 0
f 0
wmc 9

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 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
	 * @var Bucket[]
22
	 */
23
	private $buckets;
24
25 10
	public function __construct( string $identifier, \DateTime $start, \DateTime $end, array $buckets, float $displayRatio, RandomIntegerInterface $rng ) {
26 10
		$this->identifier = $identifier;
27 10
		$this->start = $start;
28 10
		$this->end = $end;
29 10
		$this->buckets = $buckets;
30 10
		$this->displayRatio = $displayRatio;
31 10
		$this->rng = $rng;
32 10
	}
33
34 1
	public function getIdentifier(): string {
35 1
		return $this->identifier;
36
	}
37
38
	public function getEnd(): \DateTime {
39
		return $this->end;
40
	}
41
42 4
	public function isInActiveDateRange( \DateTime $time ): bool {
43 4
		return $time->getTimestamp() >= $this->start->getTimestamp() &&
44 4
			$time->getTimestamp() <= $this->end->getTimestamp();
45
	}
46
47 3
	public function selectBucket( ?string $bucketId ): Bucket {
48 3
		foreach ( $this->buckets as $bucket ) {
49 3
			if ( $bucket->getIdentifier() === $bucketId ) {
50 3
				return $bucket;
51
			}
52
		}
53 2
		return $this->buckets[$this->rng->getRandomInteger( 0, count( $this->buckets ) - 1 )];
54
	}
55
56 3
	public function isRatioLimited(): bool {
57 3
		return ( $this->rng->getRandomInteger( 1, 100 ) / 100 ) > $this->displayRatio;
58
	}
59
}