Passed
Pull Request — master (#1936)
by
unknown
63:36
created

OutputCookiePreference::getSubscribedEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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\KernelEvents;
10
11
/**
12
 * Modify all HTML responses, adding the current consent state as a data attribute.
13
 *
14
 * This helps us keeping the consent cookie HTTP-only.
15
 *
16
 * Manipulating the HTML output directly, relying on an existing attribute, is
17
 * brittle, but until we have a better way for rendering our application shell
18
 * (see https://phabricator.wikimedia.org/T248460 ), this will have to do.
19
 */
20
class OutputCookiePreference implements EventSubscriberInterface {
21
22
	public static function getSubscribedEvents() {
23
		return [
24
			KernelEvents::RESPONSE => [ 'addCookiePreference' ]
25
		];
26
	}
27
28
	public function addCookiePreference( FilterResponseEvent $event ): void {
29
		if ( !$event->isMasterRequest() ) {
30
			return;
31
		}
32
33
		$cookieConsent = $event->getRequest()->cookies->get( 'cookie_consent', 'unset' );
34
		$response = $event->getResponse();
35
36
		$response->setContent( str_replace(
37
			'data-application-vars=',
38
			'data-cookie-consent="' . $cookieConsent . '" data-application-vars=',
39
			$response->getContent()
40
		) );
41
	}
42
}
43