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