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

Campaign::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 9
dl 0
loc 21
rs 9.9666
c 2
b 0
f 0
ccs 11
cts 11
cp 1
cc 1
nc 1
nop 10
crap 1

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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
}