Test Setup Failed
Pull Request — master (#37)
by Gabriel
62:16
created

Campaign::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 2
b 0
f 0
nc 1
nop 8
dl 0
loc 17
ccs 8
cts 8
cp 1
crap 1
rs 10

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 $identifier;
13
	private $start;
14
	private $end;
15
	private $category;
16
	private $rng;
17
	private $displayPercentage;
18
19
	/**
20
	 * @var Bucket[]
21
	 */
22
	private $buckets;
23 6
24
	public function __construct(
25
		string $identifier,
26
		\DateTime $start,
27
		\DateTime $end,
28
		int $displayPercentage,
29
		string $category,
30
		RandomIntegerGenerator $rng,
31
		Bucket $firstBucket,
32 6
		Bucket ...$additionalBuckets ) {
33 6
34 6
		$this->identifier = $identifier;
35 6
		$this->start = $start;
36 6
		$this->end = $end;
37 6
		$this->category = $category;
38 6
		$this->displayPercentage = $displayPercentage;
39
		$this->rng = $rng;
40 1
		$this->buckets = array_merge( [$firstBucket], $additionalBuckets );
41 1
	}
42
43
	public function getIdentifier(): string {
44 1
		return $this->identifier;
45 1
	}
46
47
	public function getEnd(): \DateTime {
48 2
		return $this->end;
49 2
	}
50 2
51
	public function getCategory(): string {
52
		return $this->category;
53 3
	}
54 3
55 3
	public function isInActiveDateRange( \DateTime $time ): bool {
56 1
		return $time->getTimestamp() >= $this->start->getTimestamp() &&
57
			$time->getTimestamp() <= $this->end->getTimestamp();
58
	}
59 2
60
	public function selectBucket( ?string $bucketId ): Bucket {
61
		foreach ( $this->buckets as $bucket ) {
62 1
			if ( $bucket->getIdentifier() === $bucketId ) {
63 1
				return $bucket;
64
			}
65
		}
66
		return $this->buckets[$this->rng->getRandomInteger( 0, count( $this->buckets ) - 1 )];
67
	}
68
69
	public function getDisplayPercentage(): int {
70
		return $this->displayPercentage;
71
	}
72
}