|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare( strict_types = 1 ); |
|
4
|
|
|
|
|
5
|
|
|
namespace WMDE\Fundraising\Frontend\Presentation; |
|
6
|
|
|
|
|
7
|
|
|
use Pimple\Container; |
|
8
|
|
|
use Pimple\ServiceProviderInterface; |
|
9
|
|
|
use Silex\Api\BootableProviderInterface; |
|
10
|
|
|
use Silex\Application; |
|
11
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
12
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
13
|
|
|
use WMDE\Fundraising\Frontend\Infrastructure\CookieBuilder; |
|
14
|
|
|
|
|
15
|
|
|
class SkinServiceProvider implements ServiceProviderInterface, BootableProviderInterface { |
|
16
|
|
|
|
|
17
|
|
|
private $skinSettings; |
|
18
|
|
|
private $cookieBuilder; |
|
19
|
|
|
|
|
20
|
|
|
private $updatedSkin; |
|
21
|
|
|
|
|
22
|
|
|
public function __construct( SkinSettings $skinSettings, CookieBuilder $cookieBuilder ) { |
|
23
|
|
|
$this->skinSettings = $skinSettings; |
|
24
|
|
|
$this->cookieBuilder = $cookieBuilder; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Ideally, SkinSettings would be registered to app as a service here - but $pimple['twig'] in FunFunFactory needs it |
|
29
|
|
|
* and does not have access to app |
|
30
|
|
|
*/ |
|
31
|
|
|
public function register( Container $app ): void { |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function boot( Application $app ): void { |
|
35
|
|
|
$app->before( function( Request $request ) { |
|
36
|
|
|
$skinFromCookie = $this->getSkinFromCookie( $request ); |
|
37
|
|
|
if ( $skinFromCookie ) { |
|
38
|
|
|
$this->skinSettings->setSkin( $skinFromCookie ); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
$skinFromQuery = $this->getSkinFromQuery( $request ); |
|
42
|
|
|
if ( $skinFromQuery && $skinFromQuery !== $this->skinSettings->getSkin() ) { |
|
43
|
|
|
$this->skinSettings->setSkin( $skinFromQuery ); |
|
44
|
|
|
$this->updatedSkin = $skinFromQuery; |
|
45
|
|
|
} |
|
46
|
|
|
}, Application::EARLY_EVENT ); |
|
47
|
|
|
|
|
48
|
|
|
$app->after( function( Request $request, Response $response ) { |
|
49
|
|
|
if ( !$this->updatedSkin ) { |
|
50
|
|
|
return; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
if ( $this->updatedSkin === $this->skinSettings->getDefaultSkin() ) { |
|
54
|
|
|
$cookie = $this->cookieBuilder->newCookie( |
|
55
|
|
|
SkinSettings::COOKIE_NAME, |
|
56
|
|
|
'', |
|
57
|
|
|
time() - 3600 |
|
58
|
|
|
); |
|
59
|
|
|
} else { |
|
60
|
|
|
$cookie = $this->cookieBuilder->newCookie( |
|
61
|
|
|
SkinSettings::COOKIE_NAME, |
|
62
|
|
|
$this->updatedSkin, |
|
63
|
|
|
time() + $this->skinSettings->getCookieLifetime() |
|
64
|
|
|
); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$response->headers->setCookie( $cookie ); |
|
68
|
|
|
} ); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
private function getSkinFromQuery( Request $request ): ?string { |
|
72
|
|
|
$skin = $request->query->get( SkinSettings::QUERY_PARAM_NAME, '' ); |
|
73
|
|
|
return $this->skinSettings->isValidSkin( $skin ) ? $skin : null; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
private function getSkinFromCookie( Request $request ): ?string { |
|
77
|
|
|
$skin = $request->cookies->get( SkinSettings::COOKIE_NAME, '' ); |
|
78
|
|
|
return $this->skinSettings->isValidSkin( $skin ) ? $skin : null; |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|