|
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 provideBannerRequest( 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
|
|
|
} |