|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare( strict_types = 1 ); |
|
4
|
|
|
|
|
5
|
|
|
namespace WMDE\Fundraising\Frontend\App\EventHandlers; |
|
6
|
|
|
|
|
7
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
8
|
|
|
use Symfony\Component\HttpKernel\Event\FilterResponseEvent; |
|
9
|
|
|
use Symfony\Component\HttpKernel\Event\KernelEvent; |
|
10
|
|
|
use Symfony\Component\HttpKernel\KernelEvents; |
|
11
|
|
|
use WMDE\Fundraising\Frontend\BucketTesting\Domain\Model\Bucket; |
|
12
|
|
|
use WMDE\Fundraising\Frontend\Factories\FunFunFactory; |
|
13
|
|
|
|
|
14
|
|
|
class StoreBucketSelection implements EventSubscriberInterface { |
|
15
|
|
|
|
|
16
|
|
|
private const PRIORITY = 256; |
|
17
|
|
|
private const COOKIE_NAME = 'spenden_ttg'; |
|
18
|
|
|
|
|
19
|
|
|
private FunFunFactory $factory; |
|
20
|
|
|
|
|
21
|
|
|
public function __construct( FunFunFactory $factory ) { |
|
22
|
|
|
$this->factory = $factory; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public static function getSubscribedEvents() { |
|
26
|
|
|
return [ |
|
27
|
|
|
KernelEvents::REQUEST => [ 'setSelectedBuckets', self::PRIORITY ], |
|
28
|
|
|
KernelEvents::RESPONSE => [ 'storeSelectedBuckets', self::PRIORITY ] |
|
29
|
|
|
]; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function setSelectedBuckets( KernelEvent $event ): void { |
|
33
|
|
|
$request = $event->getRequest(); |
|
34
|
|
|
parse_str( $request->cookies->get( self::COOKIE_NAME, '' ), $cookieValues ); |
|
35
|
|
|
$selector = $this->factory->getBucketSelector(); |
|
36
|
|
|
$this->factory->setSelectedBuckets( $selector->selectBuckets( $cookieValues, $request->query->all() ) ); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function storeSelectedBuckets( FilterResponseEvent $event ): void { |
|
40
|
|
|
$response = $event->getResponse(); |
|
41
|
|
|
$response->headers->setCookie( |
|
42
|
|
|
$this->factory->getCookieBuilder()->newCookie( |
|
43
|
|
|
self::COOKIE_NAME, |
|
44
|
|
|
$this->getCookieValue(), |
|
45
|
|
|
$this->getCookieLifetime() |
|
46
|
|
|
) |
|
47
|
|
|
); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
private function getCookieValue(): string { |
|
51
|
|
|
return http_build_query( |
|
52
|
|
|
// each Bucket returns one [ key => value ], they all need to be merged into one array |
|
53
|
|
|
array_merge( ...$this->getParameterArrayFromSelectedBuckets() ) |
|
54
|
|
|
); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
private function getParameterArrayFromSelectedBuckets(): array { |
|
58
|
|
|
return array_map( |
|
59
|
|
|
function ( Bucket $bucket ) { |
|
60
|
|
|
return $bucket->getParameters(); |
|
61
|
|
|
}, |
|
62
|
|
|
$this->factory->getSelectedBuckets() |
|
63
|
|
|
); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
private function getCookieLifetime(): ?int { |
|
67
|
|
|
$mostDistantCampaign = $this->factory->getCampaignCollection()->getMostDistantCampaign(); |
|
68
|
|
|
if ( $mostDistantCampaign === null ) { |
|
69
|
|
|
return null; |
|
70
|
|
|
} |
|
71
|
|
|
return $mostDistantCampaign->getEndTimestamp()->getTimestamp(); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
|