|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare( strict_types = 1 ); |
|
4
|
|
|
|
|
5
|
|
|
namespace WMDE\BannerServer\Controller; |
|
6
|
|
|
|
|
7
|
|
|
use Symfony\Component\HttpFoundation\Cookie; |
|
8
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
|
9
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
10
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
11
|
|
|
use WMDE\BannerServer\UseCase\BannerSelection\BannerSelectionUseCase; |
|
12
|
|
|
use WMDE\BannerServer\UseCase\BannerSelection\Visitor; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @license GNU GPL v2+ |
|
16
|
|
|
*/ |
|
17
|
|
|
class BannerSelectionController { |
|
18
|
|
|
|
|
19
|
|
|
public const IMPRESSION_COUNT_COOKIE = 'impCount'; |
|
20
|
|
|
public const BUCKET_COOKIE = 'b'; |
|
21
|
|
|
public const DONATED_COOKIE = 'd'; |
|
22
|
|
|
|
|
23
|
|
|
private $useCase; |
|
24
|
|
|
private $bannerDirectory; |
|
25
|
|
|
|
|
26
|
5 |
|
public function __construct( BannerSelectionUseCase $useCase, string $bannerDirectory ) { |
|
27
|
5 |
|
$this->useCase = $useCase; |
|
28
|
5 |
|
$this->bannerDirectory = $bannerDirectory; |
|
29
|
5 |
|
} |
|
30
|
|
|
|
|
31
|
5 |
|
public function selectBanner( Request $request ): Response { |
|
32
|
5 |
|
$bannerResponseData = $this->useCase->provideBannerRequest( $this->buildValuesFromRequest( $request ) ); |
|
33
|
5 |
|
if ( !$bannerResponseData->displayBanner() ) { |
|
34
|
3 |
|
return new Response( '', Response::HTTP_NO_CONTENT ); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
2 |
|
$response = new RedirectResponse( |
|
38
|
2 |
|
$this->getBannerUrl( $bannerResponseData->getBannerIdentifier() ), |
|
39
|
2 |
|
Response::HTTP_FOUND |
|
40
|
|
|
); |
|
41
|
|
|
|
|
42
|
2 |
|
foreach ( $this->getCookies( |
|
43
|
2 |
|
$bannerResponseData->getVisitorData(), |
|
44
|
2 |
|
$bannerResponseData->getCampaignEnd()->modify( '+2 week' ) |
|
45
|
|
|
) as $cookie ) { |
|
46
|
2 |
|
$response->headers->setCookie( $cookie ); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
2 |
|
return $response; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
5 |
|
private function buildValuesFromRequest( Request $request ): Visitor { |
|
53
|
5 |
|
return new Visitor( |
|
54
|
5 |
|
$request->cookies->getInt( self::IMPRESSION_COUNT_COOKIE, 0 ), |
|
55
|
5 |
|
$request->cookies->get( self::BUCKET_COOKIE, null ), |
|
56
|
5 |
|
$request->cookies->get( self::DONATED_COOKIE, false ) |
|
57
|
|
|
); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
2 |
|
private function getBannerUrl( string $bannerIdentifier ): string { |
|
61
|
2 |
|
return 'banners/' . $bannerIdentifier . '.js'; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
2 |
|
private function getCookies( Visitor $visitor, \DateTime $cookieExpirationDate ): array { |
|
65
|
|
|
return [ |
|
66
|
2 |
|
new Cookie( self::BUCKET_COOKIE, $visitor->getBucketIdentifier(), $cookieExpirationDate ), |
|
67
|
2 |
|
new Cookie( |
|
68
|
2 |
|
self::IMPRESSION_COUNT_COOKIE, |
|
69
|
2 |
|
(string)$visitor->getTotalImpressionCount(), |
|
70
|
2 |
|
new \DateTime( 'midnight first day of next year' ) |
|
71
|
|
|
) |
|
72
|
|
|
]; |
|
73
|
|
|
} |
|
74
|
|
|
} |