Completed
Pull Request — master (#19)
by Tim
86:17 queued 20:58
created

BannerSelectionUseCase   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 37
ccs 0
cts 10
cp 0
rs 10
c 0
b 0
f 0
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A selectRandomBucket() 0 2 1
A provideBannerRequest() 0 16 4
A __construct() 0 2 1
A getCurrentCampaign() 0 5 2
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace WMDE\BannerServer\UseCase\BannerSelection;
6
7
use WMDE\BannerServer\Entity\BannerSelection\Bucket;
8
use WMDE\BannerServer\Entity\BannerSelection\Campaign;
9
use WMDE\BannerServer\Entity\BannerSelection\CampaignCollection;
10
11
/**
12
 * @license GNU GPL v2+
13
 */
14
class BannerSelectionUseCase {
15
16
	private $campaignCollection;
17
	private $currentCampaign;
18
19
	public function __construct( CampaignCollection $campaignCollection ) {
20
		$this->campaignCollection = $campaignCollection;
21
	}
22
23
	public function provideBannerRequest( Visitor $visitor ): BannerSelection {
24
		if ( $visitor->hasDonated() ||
25
			$this->getCurrentCampaign() === null ||
26
			$this->getCurrentCampaign()->impressionThresholdReached( $visitor->getTotalImpressionCount() ) ) {
27
			return BannerSelection::createEmptySelection( $visitor );
28
		}
29
30
		$visitorBucket = $this->getCurrentCampaign()->selectBucket(
31
			$visitor->getBucketIdentifier(),
32
			[ self::class, 'selectRandomBucket' ]
33
		);
34
35
		return BannerSelection::createBannerSelection(
36
			$visitorBucket->getBanner( $visitor->getTotalImpressionCount() ),
37
			new Visitor( 1, $visitorBucket->getIdentifier(), false ),
38
			new \DateTime()
39
		);
40
	}
41
42
	private function getCurrentCampaign(): ?Campaign {
43
		if ( $this->currentCampaign === null ) {
44
			$this->currentCampaign = $this->campaignCollection->getCampaign( new \DateTime() );
45
		}
46
		return $this->currentCampaign;
47
	}
48
49
	public static function selectRandomBucket( Bucket ...$buckets ): Bucket {
50
		return $buckets[random_int( 0, count( $buckets ) )];
51
	}
52
}