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