Bucket::getIdentifier()   A
last analyzed

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
/**
8
 * @license GPL-2.0-or-later
9
 */
10
class Bucket {
11
12
	private string $identifier;
13
14
	/**
15
	 * @var Banner[]
16
	 */
17
	private array $banners = [];
18
19 5
	public function __construct( string $identifier, Banner $firstBanner, Banner ...$additionalBanners ) {
20 5
		$this->identifier = $identifier;
21 5
		$this->banners = array_merge( [ $firstBanner ], $additionalBanners );
22
	}
23
24 1
	public function getIdentifier(): string {
25 1
		return $this->identifier;
26
	}
27
28 1
	private function getLastBanner(): Banner {
29 1
		return $this->banners[count( $this->banners ) - 1];
30
	}
31
32
	/**
33
	 * Decides which banner to return based on visitor's impression count
34
	 *
35
	 * @param int $visitorImpressionCount
36
	 *
37
	 * @return string
38
	 */
39 4
	public function getBanner( int $visitorImpressionCount ): string {
40 4
		if ( $visitorImpressionCount >= count( $this->banners ) ) {
41 1
			return $this->getLastBanner()->getIdentifier();
42
		}
43 3
		return $this->banners[$visitorImpressionCount]->getIdentifier();
44
	}
45
}
46