Completed
Pull Request — master (#19)
by Tim
03:07
created

BannerSelectionUseCase::provideBannerRequest()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 2
nop 1
dl 0
loc 15
ccs 11
cts 11
cp 1
crap 4
rs 9.9332
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\Bucket;
8
use WMDE\BannerServer\Entity\BannerSelection\Campaign;
9
use WMDE\BannerServer\Entity\BannerSelection\CampaignCollection;
10
use WMDE\BannerServer\Entity\BannerSelection\ImpressionThreshold;
11
use WMDE\BannerServer\Utils\RandomIntegerInterface;
12
13
/**
14
 * @license GNU GPL v2+
15
 */
16
class BannerSelectionUseCase {
17
18
	private $campaignCollection;
19
	private $currentCampaign;
20
	private $impressionThreshold;
21
22 3
	public function __construct( CampaignCollection $campaignCollection, ImpressionThreshold $impressionThreshold ) {
23 3
		$this->campaignCollection = $campaignCollection;
24 3
		$this->impressionThreshold = $impressionThreshold;
25 3
	}
26
27 3
	public function provideBannerRequest( Visitor $visitor ): BannerSelection {
28 3
		if ( $visitor->hasDonated() ||
29 3
			$this->getCurrentCampaign() === null ||
30 3
			$this->impressionThreshold->isThresholdReached( $visitor->getTotalImpressionCount() ) ) {
31 1
			return BannerSelection::createEmptySelection( $visitor );
32
		}
33
34 2
		$visitorBucket = $this->getCurrentCampaign()->selectBucket(
35 2
			$visitor->getBucketIdentifier()
36
		);
37
38 2
		return BannerSelection::createBannerSelection(
39 2
			$visitorBucket->getBanner( $visitor->getTotalImpressionCount() ),
40 2
			new Visitor( 1, $visitorBucket->getIdentifier(), false ),
41 2
			$this->getCurrentCampaign()->getEnd()
42
		);
43
	}
44
45 3
	private function getCurrentCampaign(): ?Campaign {
46 3
		if ( $this->currentCampaign === null ) {
47 3
			$this->currentCampaign = $this->campaignCollection->getCampaign( new \DateTime() );
48
		}
49 3
		return $this->currentCampaign;
50
	}
51
}