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

Campaign::getEnd()   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 2
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
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
}