Completed
Pull Request — master (#19)
by Tim
03:20
created

Campaign::getEnd()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
ccs 2
cts 2
cp 1
crap 1
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 11
	public function __construct(
26
		string $identifier,
27
		\DateTime $start,
28
		\DateTime $end,
29
		float $displayRatio,
30
		RandomIntegerInterface $rng,
31
		Bucket $mainBucket,
32
		Bucket ...$additionalBuckets ) {
33
34 11
		$this->identifier = $identifier;
35 11
		$this->start = $start;
36 11
		$this->end = $end;
37 11
		$this->displayRatio = $displayRatio;
38 11
		$this->rng = $rng;
39 11
		$this->buckets = $additionalBuckets;
40 11
		array_unshift( $this->buckets, $mainBucket );
41 11
	}
42
43 1
	public function getIdentifier(): string {
44 1
		return $this->identifier;
45
	}
46
47 1
	public function getEnd(): \DateTime {
48 1
		return $this->end;
49
	}
50
51 5
	public function isInActiveDateRange( \DateTime $time ): bool {
52 5
		return $time->getTimestamp() >= $this->start->getTimestamp() &&
53 5
			$time->getTimestamp() <= $this->end->getTimestamp();
54
	}
55
56 4
	public function selectBucket( ?string $bucketId ): Bucket {
57 4
		foreach ( $this->buckets as $bucket ) {
58 4
			if ( $bucket->getIdentifier() === $bucketId ) {
59 4
				return $bucket;
60
			}
61
		}
62 3
		return $this->buckets[$this->rng->getRandomInteger( 0, count( $this->buckets ) - 1 )];
63
	}
64
65 3
	public function isRatioLimited(): bool {
66 3
		return ( $this->rng->getRandomInteger( 1, 100 ) / 100 ) > $this->displayRatio;
67
	}
68
}