Test Setup Failed
Push — master ( b11e68...2d8347 )
by
unknown
06:21 queued 12s
created

Campaign::isInDisplayRange()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 9
ccs 0
cts 0
cp 0
rs 9.6111
cc 5
nc 3
nop 1
crap 30
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 string $identifier;
13
	private \DateTime $start;
14
	private \DateTime $end;
15
	private string $category;
16
	private RandomIntegerGenerator $rng;
17
	private int $displayPercentage;
18
	private ?int $minDisplayWidth;
19
	private ?int $maxDisplayWidth;
20
21
	/**
22
	 * @var Bucket[]
23 6
	 */
24
	private $buckets;
25
26
	public function __construct(
27
		string $identifier,
28
		\DateTime $start,
29
		\DateTime $end,
30
		int $displayPercentage,
31
		string $category,
32 6
		RandomIntegerGenerator $rng,
33 6
		?int $minDisplayWidth = null,
34 6
		?int $maxDisplayWidth = null,
35 6
		Bucket $firstBucket,
36 6
		Bucket ...$additionalBuckets ) {
37 6
38 6
		$this->identifier = $identifier;
39
		$this->start = $start;
40 1
		$this->end = $end;
41 1
		$this->category = $category;
42
		$this->displayPercentage = $displayPercentage;
43
		$this->rng = $rng;
44 1
		$this->minDisplayWidth = $minDisplayWidth;
45 1
		$this->maxDisplayWidth = $maxDisplayWidth;
46
		$this->buckets = array_merge( [$firstBucket], $additionalBuckets );
47
	}
48 2
49 2
	public function getIdentifier(): string {
50 2
		return $this->identifier;
51
	}
52
53 3
	public function getEnd(): \DateTime {
54 3
		return $this->end;
55 3
	}
56 1
57
	public function getCategory(): string {
58
		return $this->category;
59 2
	}
60
61
	public function isInActiveDateRange( \DateTime $time ): bool {
62 1
		return $time->getTimestamp() >= $this->start->getTimestamp() &&
63 1
			$time->getTimestamp() <= $this->end->getTimestamp();
64
	}
65
66
	public function selectBucket( ?string $bucketId ): Bucket {
67
		foreach ( $this->buckets as $bucket ) {
68
			if ( $bucket->getIdentifier() === $bucketId ) {
69
				return $bucket;
70
			}
71
		}
72
		return $this->buckets[$this->rng->getRandomInteger( 0, count( $this->buckets ) - 1 )];
73
	}
74
75
	public function getDisplayPercentage(): int {
76
		return $this->displayPercentage;
77
	}
78
79
	public function isInDisplayRange( int $width ): bool {
80
81
		if ( $this->minDisplayWidth !== null && $width < $this->minDisplayWidth ) {
82
			return false;
83
		}
84
		if( $this->maxDisplayWidth !== null && $width > $this->maxDisplayWidth ){
85
			return false;
86
		}
87
		return true;
88
	}
89
}