Passed
Push — master ( 7d7cf1...424d8c )
by Gabriel
37s queued 10s
created

BannerSelectionUseCase   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 41
ccs 23
cts 23
cp 1
rs 10
c 0
b 0
f 0
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A isRatioLimited() 0 2 1
A __construct() 0 4 1
A getCurrentCampaign() 0 5 2
A selectBanner() 0 16 5
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace WMDE\BannerServer\UseCase\BannerSelection;
6
7
use WMDE\BannerServer\Entity\BannerSelection\Campaign;
8
use WMDE\BannerServer\Entity\BannerSelection\CampaignCollection;
9
use WMDE\BannerServer\Entity\BannerSelection\ImpressionThreshold;
10
use WMDE\BannerServer\Entity\BannerSelection\RandomIntegerGenerator;
11
12
/**
13
 * @license GNU GPL v2+
14
 */
15
class BannerSelectionUseCase {
16
17
	private $campaignCollection;
18
	private $currentCampaign;
19
	private $impressionThreshold;
20
	private $rng;
21
22 3
	public function __construct( CampaignCollection $campaignCollection, ImpressionThreshold $impressionThreshold, RandomIntegerGenerator $rng ) {
23 3
		$this->campaignCollection = $campaignCollection;
24 3
		$this->impressionThreshold = $impressionThreshold;
25 3
		$this->rng = $rng;
26 3
	}
27
28 3
	public function selectBanner( Visitor $visitor ): BannerSelectionData {
29 3
		if ( $visitor->hasDonated() ||
30 3
			$this->getCurrentCampaign() === null ||
31 3
			$this->impressionThreshold->isThresholdReached( $visitor->getTotalImpressionCount() ) ||
32 3
			$this->isRatioLimited( $this->getCurrentCampaign()->getDisplayPercentage() ) ) {
33 1
			return new EmptyBannerSelectionData( $visitor );
34
		}
35
36 2
		$visitorBucket = $this->getCurrentCampaign()->selectBucket(
37 2
			$visitor->getBucketIdentifier()
38
		);
39
40 2
		return new ActiveBannerSelectionData(
41 2
			new Visitor( 1, $visitorBucket->getIdentifier(), false ),
42 2
			$visitorBucket->getBanner( $visitor->getTotalImpressionCount() ),
43 2
			$this->getCurrentCampaign()->getEnd()
44
		);
45
	}
46
47 3
	private function getCurrentCampaign(): ?Campaign {
48 3
		if ( $this->currentCampaign === null ) {
49 3
			$this->currentCampaign = $this->campaignCollection->getCampaign( new \DateTime() );
50
		}
51 3
		return $this->currentCampaign;
52
	}
53
54 3
	private function isRatioLimited( int $campaignDisplayPercent ): bool {
55 3
		return $this->rng->getRandomInteger( 1, 100 ) > $campaignDisplayPercent;
56
	}
57
}