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

BannerSelectionUseCase::getCurrentCampaign()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
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
}